PageRenderTime 23ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/mgen/web/__init__.py

https://github.com/stan1y/MrHide
Python | 50 lines | 29 code | 10 blank | 11 comment | 2 complexity | 3cc0c9da8a5434189f888933f245fc4e MD5 | raw file
  1. """
  2. Web intefaces for MGEN.
  3. It exposes module level make_app function for mgen.app module
  4. """
  5. import os
  6. import json
  7. import logging
  8. import tornado.web
  9. import mgen.model
  10. import mgen.error
  11. import mgen.util
  12. import sqlalchemy.orm.exc
  13. log = logging.getLogger(__name__)
  14. class BaseRequestHandler(tornado.web.RequestHandler):
  15. def get_current_user(self):
  16. '''
  17. Returns current auth principal (token) json
  18. received from external auth provider and stored in
  19. a secure cookie on client
  20. '''
  21. prn = self.get_secure_cookie("mgen-auth-principal")
  22. if prn:
  23. return json.loads(prn.decode('utf-8'))
  24. return None
  25. def get_profile(self):
  26. '''Returns profile id (email) stored in secure cookie'''
  27. profile = self.get_secure_cookie("mgen-auth-profile")
  28. if profile:
  29. return mgen.model.ProfileStore(json.loads(profile.decode('utf-8')))
  30. return None
  31. @property
  32. def current_profile(self):
  33. '''Returns a Profile model of the current user'''
  34. if hasattr(self, '__cached_profile'):
  35. return getattr(self, '__cached_profile')
  36. self.__cached_profile = self.get_profile()
  37. log.debug('current profile: %s' % self.__cached_profile.email)
  38. return self.__cached_profile