1 #!/usr/bin/env python 2 3 """ 4 Django adapter. 5 6 Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 Lesser General Public License for more details. 17 18 You should have received a copy of the GNU Lesser General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 21 """ 22 23 import WebStack.Django 24 from WebStack.Generic import EndOfResponse 25 from WebStack.Adapters.Helpers.Error import ErrorResource 26 27 def deploy(resource, authenticator=None, address=None, handle_errors=1, error_resource=None): 28 29 """ 30 Deploy the given 'resource', employing the optional 'authenticator' to 31 protect it. The optional 'handle_errors' parameter (if true) causes handlers 32 to deal with uncaught exceptions cleanly, and the optional 'error_resource' 33 specifies an alternative error message generation resource, if desired. 34 35 The optional 'address' parameter is deliberately ignored. 36 """ 37 38 def _deploy(request, *args, **kw): 39 return respond(request, resource, authenticator=authenticator, 40 virtual_path_info=kw.get("vp"), handle_errors=handle_errors, 41 error_resource=error_resource) 42 return _deploy 43 44 def respond(request, resource, authenticator=None, virtual_path_info=None, handle_errors=1, 45 error_resource=None): 46 47 """ 48 Dispatch to the root application-specific 'resource'. Employ the optional 49 'authenticator' to control access to the resource. Define the optional 50 'virtual_path_info' as the path information considered to belong to the 51 given 'resource'. 52 53 The optional 'handle_errors' parameter (if true) causes handlers to deal 54 with uncaught exceptions cleanly, and the optional 'error_resource' 55 specifies an alternative error message generation resource. 56 """ 57 58 error_resource = error_resource or ErrorResource() 59 60 trans = WebStack.Django.Transaction(request) 61 if virtual_path_info: 62 trans.set_virtual_path_info(virtual_path_info) 63 64 try: 65 if authenticator is None or authenticator.authenticate(trans): 66 try: 67 resource.respond(trans) 68 except EndOfResponse: 69 pass 70 except Exception, exc: 71 if handle_errors: 72 trans.rollback() 73 trans.set_response_code(500) # Internal error 74 error_resource.respond(trans) 75 else: 76 raise 77 else: 78 trans.set_response_code(401) # Unauthorized 79 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( 80 authenticator.get_auth_type(), authenticator.get_realm())) 81 finally: 82 trans.commit() 83 return trans.response 84 85 # vim: tabstop=4 expandtab shiftwidth=4