python - Django user in tornado -
i have been using code below django user in tornado:
from django.conf import settings import django.contrib.auth import django.utils.importlib import tornado tornado.options import options import tornado.web import tornado.ioloop import sockjs.tornado class routerconnection(sockjs.tornado.sockjsconnection): def get_current_user(self, info): engine = django.utils.importlib.import_module(django.conf.settings.session_engine) session_key = str(info.get_cookie(django.conf.settings.session_cookie_name)).split('=')[1] class dummy(object): pass django_request = dummy() django_request.session = engine.sessionstore(session_key) user = django.contrib.auth.get_user(django_request) return user def on_open(self, info): user = self.get_current_user(info=info) if __name__ == "__main__": import logging router = sockjs.tornado.sockjsrouter(routerconnection) app = tornado.web.application(router.urls, debug=true, ) app.listen(settings.tornado_port) tornado.options.parse_command_line() tornado.ioloop.ioloop.instance().start()
my problem follows:
django changes logging configuration , can no longer see log output tornado. how can reinitialize tornado logging? there other way integrate django tornado?
many in advance.
to reset django logging use following:
logger = logging.getlogger('') handler in logger.handlers: logger.removehandler(handler) tornado.options.parse_command_line()
when using django.conf module lazysettings class initialized initialized django logger. had rewrite code using initialized settings class:
from django.conf import settings import django.contrib.auth import django.utils.importlib import tornado tornado.options import options import tornado.web import tornado.ioloop import sockjs.tornado tornado_port = settings.tornado_port class routerconnection(sockjs.tornado.sockjsconnection): def get_current_user(self, info): engine = django.utils.importlib.import_module(settings.session_engine) session_key = str(info.get_cookie(settings.session_cookie_name)).split('=')[1] class dummy(object): pass django_request = dummy() django_request.session = engine.sessionstore(session_key) user = django.contrib.auth.get_user(django_request) return user def on_open(self, info): user = self.get_current_user(info=info) if __name__ == "__main__": import logging logger = logging.getlogger('') handler in logger.handlers: logger.removehandler(handler) tornado.options.parse_command_line() router = sockjs.tornado.sockjsrouter(routerconnection) app = tornado.web.application(router.urls, debug=settings.debug) app.listen(settings.tornado_port) tornado.options.parse_command_line() tornado.ioloop.ioloop.instance().start()
Comments
Post a Comment