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