# HG changeset patch # User paulb # Date 1137203569 0 # Node ID 8e0f0ebf60366fc332274c9f717d125d810d8aed # Parent 0ce82d7530bf5ec94868e0d75c159a41275402c3 [project @ 2006-01-14 01:52:49 by paulb] Added Django support. diff -r 0ce82d7530bf -r 8e0f0ebf6036 WebStack/Adapters/Django.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebStack/Adapters/Django.py Sat Jan 14 01:52:49 2006 +0000 @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +""" +Django adapter. + +Copyright (C) 2006 Paul Boddie + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +""" + +import WebStack.Django +from WebStack.Generic import EndOfResponse + +def respond(request, resource, authenticator=None, handle_errors=1): + + """ + Dispatch to the root application-specific 'resource'. Employ the optional + 'authenticator' to control access to the resource. The optional + 'handle_errors' parameter (if true) causes handlers to deal with uncaught + exceptions cleanly. + """ + + trans = WebStack.Django.Transaction(request) + + try: + if authenticator is None or authenticator.authenticate(trans): + try: + resource.respond(trans) + except EndOfResponse: + pass + except Exception, exc: + if handle_errors: + print "H" + trans.set_response_code(500) # Internal error + else: + print "R", exc + raise + else: + trans.set_response_code(401) # Unauthorized + trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( + authenticator.get_auth_type(), authenticator.get_realm())) + finally: + trans.commit() + return trans.response + +# vim: tabstop=4 expandtab shiftwidth=4