class BaseSocketHandler(tornado.websocket.WebSocketHandler): def __init__(self, *args, **kwargs): super(BaseSocketHandler, self).__init__(*args, **kwargs) self.loader = tornado.template.Loader( self.application.settings['template_path']) @property def redis(self): return self.application.redis @property def db(self): return self.application.db def open(self): session_id = self.get_secure_cookie("_session_id") self.session = RedisSession(self.redis, session_id) def render(self, template_name, *args, **kwargs): return self.loader.load(template_name).generate(*args, **kwargs) def get_current_user(self): return self.session.get('user') def on_close(self): pass
class BaseHandler(tornado.web.RequestHandler): @property def redis(self): return self.application.redis @property def db(self): return self.application.db def initialize(self): session_id = self.get_secure_cookie("_session_id") self.session = RedisSession(self.redis, session_id) def get_current_user(self): return self.session.get('user') def on_finish(self): pass def write_error(self, status_code, **kwargs): if status_code == 404: self.render('errors/404.html', kwargs=kwargs) elif status_code == 403: self.render('errors/403.html', kwargs=kwargs) elif status_code == 503: self.render('errors/503.html', kwargs=kwargs) else: super(BaseHandler, self).write_error(status_code, **kwargs)
def pagination_processor(): def pagination(url, pager, template=None, params={}): template = template or 'mod/pagination.html' pager._dict['current'] = int( math.ceil((pager.offset + 1) / float(pager.limit))) pager._dict['total_page'] = int( math.ceil(pager.rows_found / float(pager.limit))) prev_offset = pager.offset - pager.limit pager._dict['prev_offset'] = prev_offset if prev_offset >= 0 else 0 pager._dict['params'] = params pager._dict['url'] = url return Markup(render_template(template, data=pager)) return dict(pagination=pagination) @app.template_filter('datetime') def datetime_filter(n, format='%Y-%m-%d %H:%M'): arr = time.localtime(n) return time.strftime(format, arr) from website import init_app from utils.session import RedisSession init_app(app) RedisSession.init_app(app) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=15204)
return dict(pjax=get_template) @app.context_processor def pagination_processor(): def pagination(url, pager, template=None, params={}): template = template or 'mod/pagination.html' pager._dict['current'] = int(math.ceil(( pager.offset + 1 ) / float(pager.limit))) pager._dict['total_page'] = int(math.ceil(pager.rows_found / float(pager.limit))) prev_offset = pager.offset - pager.limit pager._dict['prev_offset'] = prev_offset if prev_offset >= 0 else 0 pager._dict['params'] = params pager._dict['url'] = url return Markup(render_template(template, data=pager)) return dict(pagination=pagination) @app.template_filter('datetime') def datetime_filter(n, format='%Y-%m-%d %H:%M'): arr = time.localtime(n) return time.strftime(format, arr) init_app(app) RedisSession.init_app(app) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=61001)
def open(self): session_id = self.get_secure_cookie("_session_id") self.session = RedisSession(self.redis, session_id)
def initialize(self): session_id = self.get_secure_cookie("_session_id") self.session = RedisSession(self.redis, session_id)