request.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from contextlib import contextmanager
  2. from pyramid.decorator import reify
  3. from pyramid.request import Request
  4. from tracim.models import User
  5. from tracim.models.data import Workspace
  6. from tracim.lib.utils.auth import get_safe_user
  7. from tracim.lib.utils.auth import get_workspace
  8. class TracimRequest(Request):
  9. def __init__(
  10. self,
  11. environ,
  12. charset=None,
  13. unicode_errors=None,
  14. decode_param_names=None,
  15. **kw
  16. ):
  17. super().__init__(
  18. environ,
  19. charset,
  20. unicode_errors,
  21. decode_param_names,
  22. **kw
  23. )
  24. self._current_workspace = None # type: Workspace
  25. self._current_user = None # type: User
  26. @property
  27. def current_workspace(self) -> Workspace:
  28. if self._current_workspace is None:
  29. self.current_workspace = get_workspace(self.current_user, self)
  30. return self._current_workspace
  31. @current_workspace.setter
  32. def current_workspace(self, workspace: Workspace) -> None:
  33. assert self._current_workspace is None
  34. self._current_workspace = workspace
  35. @property
  36. def current_user(self) -> User:
  37. if self._current_user is None:
  38. self.current_user = get_safe_user(self)
  39. return self._current_user
  40. @current_user.setter
  41. def current_user(self, user: User) -> None:
  42. assert self._current_user is None
  43. self._current_user = user