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@544 | 25 | |
paulb@554 | 26 | def deploy(resource, authenticator=None, address=None, handle_errors=1): |
paulb@554 | 27 | |
paulb@554 | 28 | """ |
paulb@554 | 29 | Deploy the given 'resource', employing the optional 'authenticator' to |
paulb@554 | 30 | protect it. The optional 'handle_errors' parameter (if true) causes handlers |
paulb@554 | 31 | to deal with uncaught exceptions cleanly. |
paulb@554 | 32 | |
paulb@554 | 33 | The optional 'address' parameter is deliberately ignored. |
paulb@554 | 34 | """ |
paulb@554 | 35 | |
paulb@554 | 36 | def _deploy(request, *args, **kw): |
paulb@554 | 37 | return respond(request, resource, authenticator=authenticator, |
paulb@554 | 38 | virtual_path_info=kw.get("vp"), handle_errors=handle_errors) |
paulb@554 | 39 | return _deploy |
paulb@554 | 40 | |
paulb@554 | 41 | def respond(request, resource, authenticator=None, virtual_path_info=None, handle_errors=1): |
paulb@544 | 42 | |
paulb@544 | 43 | """ |
paulb@544 | 44 | Dispatch to the root application-specific 'resource'. Employ the optional |
paulb@554 | 45 | 'authenticator' to control access to the resource. Define the optional |
paulb@554 | 46 | 'virtual_path_info' as the path information considered to belong to the |
paulb@554 | 47 | given 'resource'. |
paulb@554 | 48 | |
paulb@554 | 49 | The optional 'handle_errors' parameter (if true) causes handlers to deal |
paulb@554 | 50 | with uncaught exceptions cleanly. |
paulb@544 | 51 | """ |
paulb@544 | 52 | |
paulb@544 | 53 | trans = WebStack.Django.Transaction(request) |
paulb@554 | 54 | if virtual_path_info: |
paulb@554 | 55 | trans.set_virtual_path_info(virtual_path_info) |
paulb@544 | 56 | |
paulb@544 | 57 | try: |
paulb@544 | 58 | if authenticator is None or authenticator.authenticate(trans): |
paulb@544 | 59 | try: |
paulb@544 | 60 | resource.respond(trans) |
paulb@544 | 61 | except EndOfResponse: |
paulb@544 | 62 | pass |
paulb@544 | 63 | except Exception, exc: |
paulb@544 | 64 | if handle_errors: |
paulb@544 | 65 | print "H" |
paulb@544 | 66 | trans.set_response_code(500) # Internal error |
paulb@544 | 67 | else: |
paulb@544 | 68 | print "R", exc |
paulb@544 | 69 | raise |
paulb@544 | 70 | else: |
paulb@544 | 71 | trans.set_response_code(401) # Unauthorized |
paulb@544 | 72 | trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@544 | 73 | authenticator.get_auth_type(), authenticator.get_realm())) |
paulb@544 | 74 | finally: |
paulb@544 | 75 | trans.commit() |
paulb@544 | 76 | return trans.response |
paulb@544 | 77 | |
paulb@544 | 78 | # vim: tabstop=4 expandtab shiftwidth=4 |