Created
October 29, 2010 18:59
-
-
Save bdarnell/654157 to your computer and use it in GitHub Desktop.
django_tornado_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTE: This code was extracted from a larger class and has not been | |
# tested in this form. Caveat emptor. | |
import django.conf | |
import django.contrib.auth | |
import django.core.handlers.wsgi | |
import django.db | |
import django.utils.importlib | |
import httplib | |
import json | |
import logging | |
import tornado.options | |
import tornado.template as template | |
import tornado.web | |
import traceback | |
from tornado.options import options, define | |
class BaseHandler(tornado.web.RequestHandler): | |
def prepare(self): | |
super(BaseHandler, self).prepare() | |
# Prepare ORM connections | |
django.db.connection.queries = [] | |
def finish(self, chunk = None): | |
super(BaseHandler, self).finish(chunk = chunk) | |
# Clean up django ORM connections | |
django.db.connection.close() | |
if options.debug: | |
logging.info('%d sql queries' % len(django.db.connection.queries)) | |
for query in django.db.connection.queries: | |
logging.debug('%s [%s seconds]' % (query['sql'], query['time'])) | |
# Clean up after python-memcached | |
from django.core.cache import cache | |
if hasattr(cache, 'close'): | |
cache.close() | |
def get_django_session(self): | |
if not hasattr(self, '_session'): | |
engine = django.utils.importlib.import_module( | |
django.conf.settings.SESSION_ENGINE) | |
session_key = self.get_cookie(django.conf.settings.SESSION_COOKIE_NAME) | |
self._session = engine.SessionStore(session_key) | |
return self._session | |
def get_user_locale(self): | |
# locale.get will use the first non-empty argument that matches a | |
# supported language. | |
return tornado.locale.get( | |
self.get_argument('lang', None), | |
self.get_django_session().get('django_language', None), | |
self.get_cookie('django_language', None)) | |
def get_current_user(self): | |
# get_user needs a django request object, but only looks at the session | |
class Dummy(object): pass | |
django_request = Dummy() | |
django_request.session = self.get_django_session() | |
user = django.contrib.auth.get_user(django_request) | |
if user.is_authenticated(): | |
return user | |
else: | |
# try basic auth | |
if not self.request.headers.has_key('Authorization'): | |
return None | |
kind, data = self.request.headers['Authorization'].split(' ') | |
if kind != 'Basic': | |
return None | |
(username, _, password) = data.decode('base64').partition(':') | |
user = django.contrib.auth.authenticate(username = username, | |
password = password) | |
if user is not None and user.is_authenticated(): | |
return user | |
return None | |
def get_django_request(self): | |
request = django.core.handlers.wsgi.WSGIRequest( | |
tornado.wsgi.WSGIContainer.environ(self.request)) | |
request.session = self.get_django_session() | |
if self.current_user: | |
request.user = self.current_user | |
else: | |
request.user = django.contrib.auth.models.AnonymousUser() | |
return request |
I think it was written for tornado 1.0, but should work with any
version. For this error you just need to call
tornado.options.define('debug', default=False) in your main module (or
replace the options.debug check with your preferred configuration
mechanism)
…On Wed, Apr 25, 2012 at 4:53 AM, Mike Stoddart ***@***.*** wrote:
I'm getting:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/tornado/web.py", line 988, in _execute
getattr(self, self.request.method.lower())(_args, *_kwargs)
File "./provider.py", line 148, in post
self.finish()
File "/home/mike/dev/test/handler.py", line 28, in finish
if options.debug:
File "/usr/lib/python2.7/site-packages/tornado/options.py", line 192, in __getattr__
raise AttributeError("Unrecognized option %r" % name)
AttributeError: Unrecognized option 'debug'
ERROR:root:Cannot send error response after headers written
What version of Tornado does this support? I realise the comment says "it's taken from a larger class". Thanks
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/654157
In this code, there is a change in two years? Can be taken as is, and use?
As with asynchronous?
This code still works great, with a few minor changes, with Django 1.9. I'm very happy that I saw this post!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! thanks.