context_models.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # coding=utf-8
  2. import typing
  3. from datetime import datetime
  4. from slugify import slugify
  5. from sqlalchemy.orm import Session
  6. from tracim import CFG
  7. from tracim.models import User
  8. from tracim.models.auth import Profile
  9. from tracim.models.data import Workspace, UserRoleInWorkspace
  10. from tracim.models.workspace_menu_entries import default_workspace_menu_entry
  11. from tracim.models.workspace_menu_entries import WorkspaceMenuEntry
  12. class LoginCredentials(object):
  13. """
  14. Login credentials model for login
  15. """
  16. def __init__(self, email: str, password: str):
  17. self.email = email
  18. self.password = password
  19. class UserInContext(object):
  20. """
  21. Interface to get User data and User data related to context.
  22. """
  23. def __init__(self, user: User, dbsession: Session, config: CFG):
  24. self.user = user
  25. self.dbsession = dbsession
  26. self.config = config
  27. # Default
  28. @property
  29. def email(self) -> str:
  30. return self.user.email
  31. @property
  32. def user_id(self) -> int:
  33. return self.user.user_id
  34. @property
  35. def display_name(self) -> str:
  36. return self.user.display_name
  37. @property
  38. def created(self) -> datetime:
  39. return self.user.created
  40. @property
  41. def is_active(self) -> bool:
  42. return self.user.is_active
  43. @property
  44. def timezone(self) -> str:
  45. return self.user.timezone
  46. @property
  47. def profile(self) -> Profile:
  48. return self.user.profile
  49. # Context related
  50. @property
  51. def calendar_url(self) -> typing.Optional[str]:
  52. # TODO - G-M - 20-04-2018 - [Calendar] Replace calendar code to get
  53. # url calendar url.
  54. #
  55. # from tracim.lib.calendar import CalendarManager
  56. # calendar_manager = CalendarManager(None)
  57. # return calendar_manager.get_workspace_calendar_url(self.workspace_id)
  58. return None
  59. @property
  60. def avatar_url(self) -> typing.Optional[str]:
  61. # TODO - G-M - 20-04-2018 - [Avatar] Add user avatar feature
  62. return None
  63. class WorkspaceInContext(object):
  64. """
  65. Interface to get Workspace data and Workspace data related to context.
  66. """
  67. def __init__(self, workspace: Workspace, dbsession: Session, config: CFG):
  68. self.workspace = workspace
  69. self.dbsession = dbsession
  70. self.config = config
  71. @property
  72. def workspace_id(self) -> int:
  73. """
  74. numeric id of the workspace.
  75. """
  76. return self.workspace.workspace_id
  77. @property
  78. def id(self) -> int:
  79. """
  80. alias of workspace_id
  81. """
  82. return self.workspace_id
  83. @property
  84. def label(self) -> str:
  85. """
  86. get workspace label
  87. """
  88. return self.workspace.label
  89. @property
  90. def description(self) -> str:
  91. """
  92. get workspace description
  93. """
  94. return self.workspace.description
  95. @property
  96. def slug(self) -> str:
  97. """
  98. get workspace slug
  99. """
  100. return slugify(self.workspace.label)
  101. @property
  102. def sidebar_entries(self) -> typing.List[WorkspaceMenuEntry]:
  103. """
  104. get sidebar entries, those depends on activated apps.
  105. """
  106. # TODO - G.M - 22-05-2018 - Rework on this in
  107. # order to not use hardcoded list
  108. # list should be able to change (depending on activated/disabled
  109. # apps)
  110. return default_workspace_menu_entry(self.workspace)
  111. class UserRoleWorkspaceInContext(object):
  112. """
  113. Interface to get UserRoleInWorkspace data and related content
  114. """
  115. def __init__(
  116. self,
  117. user_role: UserRoleInWorkspace,
  118. dbsession: Session,
  119. config: CFG,
  120. )-> None:
  121. self.user_role = user_role
  122. self.dbsession = dbsession
  123. self.config = config
  124. @property
  125. def user_id(self) -> int:
  126. """
  127. User who has the role has this id
  128. :return: user id as integer
  129. """
  130. return self.user_role.user_id
  131. @property
  132. def workspace_id(self) -> int:
  133. """
  134. This role apply only on the workspace with this workspace_id
  135. :return: workspace id as integer
  136. """
  137. return self.user_role.workspace_id
  138. # TODO - G.M - 23-05-2018 - Check the API spec for this this !
  139. @property
  140. def role_id(self) -> int:
  141. """
  142. role as int id, each value refer to a different role.
  143. """
  144. return self.user_role.role
  145. @property
  146. def role_slug(self) -> str:
  147. """
  148. simple name of the role of the user.
  149. can be anything from UserRoleInWorkspace SLUG, like
  150. 'not_applicable', 'reader',
  151. 'contributor', 'content_manager', 'workspace_manager'
  152. :return: user workspace role as slug.
  153. """
  154. return UserRoleInWorkspace.SLUG[self.user_role.role]
  155. @property
  156. def user(self) -> UserInContext:
  157. """
  158. User who has this role, with context data
  159. :return: UserInContext object
  160. """
  161. return UserInContext(
  162. self.user_role.user,
  163. self.dbsession,
  164. self.config
  165. )
  166. @property
  167. def workspace(self) -> WorkspaceInContext:
  168. """
  169. Workspace related to this role, with his context data
  170. :return: WorkspaceInContext object
  171. """
  172. return WorkspaceInContext(
  173. self.user_role.workspace,
  174. self.dbsession,
  175. self.config
  176. )