1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/WebStack/Adapters/Django.py Sat Jan 14 01:52:49 2006 +0000
1.3 @@ -0,0 +1,58 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Django adapter.
1.8 +
1.9 +Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This library is free software; you can redistribute it and/or
1.12 +modify it under the terms of the GNU Lesser General Public
1.13 +License as published by the Free Software Foundation; either
1.14 +version 2.1 of the License, or (at your option) any later version.
1.15 +
1.16 +This library is distributed in the hope that it will be useful,
1.17 +but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.19 +Lesser General Public License for more details.
1.20 +
1.21 +You should have received a copy of the GNU Lesser General Public
1.22 +License along with this library; if not, write to the Free Software
1.23 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
1.24 +"""
1.25 +
1.26 +import WebStack.Django
1.27 +from WebStack.Generic import EndOfResponse
1.28 +
1.29 +def respond(request, resource, authenticator=None, handle_errors=1):
1.30 +
1.31 + """
1.32 + Dispatch to the root application-specific 'resource'. Employ the optional
1.33 + 'authenticator' to control access to the resource. The optional
1.34 + 'handle_errors' parameter (if true) causes handlers to deal with uncaught
1.35 + exceptions cleanly.
1.36 + """
1.37 +
1.38 + trans = WebStack.Django.Transaction(request)
1.39 +
1.40 + try:
1.41 + if authenticator is None or authenticator.authenticate(trans):
1.42 + try:
1.43 + resource.respond(trans)
1.44 + except EndOfResponse:
1.45 + pass
1.46 + except Exception, exc:
1.47 + if handle_errors:
1.48 + print "H"
1.49 + trans.set_response_code(500) # Internal error
1.50 + else:
1.51 + print "R", exc
1.52 + raise
1.53 + else:
1.54 + trans.set_response_code(401) # Unauthorized
1.55 + trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
1.56 + authenticator.get_auth_type(), authenticator.get_realm()))
1.57 + finally:
1.58 + trans.commit()
1.59 + return trans.response
1.60 +
1.61 +# vim: tabstop=4 expandtab shiftwidth=4