class Handler(tornado.web.RequestHandler): need_db_session = True def __init__(self, *args, **kwargs): super(Handler, self).__init__(*args, **kwargs) self._session = Session() def get_template_path(self): path = self.application.settings.get('template_path') if path[0] != '/': path = os.path.join( os.path.dirname(__file__), path ) return path def render(self, template, **kwargs): def _escape(value): return escape.xhtml_escape(unicode(value)) kwargs['escape'] = _escape kwargs['quote'] = quote return super(Handler, self).render(template, **kwargs) def get_current_user(self): username = self.get_secure_cookie('session') if username: return get_user_by_username(username, self._session) return None def finish(self, *args, **kwargs): super(Handler, self).finish(*args, **kwargs) if self._session is not None: self._session.commit() self._session.close() def send_error(self, *args, **kwargs): if self._session is not None: self._session.rollback() self._session.close() self._session = None super(Handler, self).send_error(*args, **kwargs)
class Handler(tornado.web.RequestHandler): need_db_session = True def __init__(self, *args, **kwargs): super(Handler, self).__init__(*args, **kwargs) self._session = Session() def get_template_path(self): path = self.application.settings.get('template_path') if path[0] != '/': path = os.path.join(os.path.dirname(__file__), path) return path def render(self, template, **kwargs): def _escape(value): return escape.xhtml_escape(unicode(value)) kwargs['escape'] = _escape kwargs['quote'] = quote return super(Handler, self).render(template, **kwargs) def get_current_user(self): username = self.get_secure_cookie('session') if username: return get_user_by_username(username, self._session) return None def finish(self, *args, **kwargs): super(Handler, self).finish(*args, **kwargs) if self._session is not None: self._session.commit() self._session.close() def send_error(self, *args, **kwargs): if self._session is not None: self._session.rollback() self._session.close() self._session = None super(Handler, self).send_error(*args, **kwargs)
from microblog.db import Session from microblog.models import * from microblog.server import init init() s = Session()
def __init__(self, *args, **kwargs): super(Handler, self).__init__(*args, **kwargs) self._session = Session()