base.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # encoding=utf-8
  2. import tornado.web
  3. from terroroftinytown.tracker.model import User
  4. ACCOUNT_COOKIE_NAME = 'tottu'
  5. ACCOUNT_TOKEN_COOKIE_NAME = 'tottt'
  6. class BaseHandler(tornado.web.RequestHandler):
  7. def get_current_user(self):
  8. username_raw = self.get_secure_cookie(ACCOUNT_COOKIE_NAME)
  9. token = self.get_secure_cookie(ACCOUNT_TOKEN_COOKIE_NAME)
  10. if username_raw and token:
  11. username = username_raw.decode('ascii')
  12. if username and User.check_account_session(username, token):
  13. return username
  14. def prepare(self):
  15. if self.application.is_maintenance_in_progress():
  16. self._show_maintenance_page()
  17. def _show_maintenance_page(self):
  18. self.set_status(512, 'Export is in progress. We\'ll be back soon!')
  19. self.render('maintenance.html')
  20. raise tornado.web.Finish()
  21. def user_audit_text(self, text):
  22. return '[{username} - {ip_address}] {text}'.format(
  23. username=self.current_user,
  24. ip_address=self.request.remote_ip,
  25. text=text,
  26. )