context_models.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, Content
  10. from tracim.models.workspace_menu_entries import default_workspace_menu_entry, \
  11. 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 ContentFilter(object):
  20. """
  21. Content filter model
  22. """
  23. def __init__(self,
  24. parent_id: int = None,
  25. show_archived: int = 0,
  26. show_deleted: int = 0,
  27. show_active: int = 1,
  28. ):
  29. self.parent_id = parent_id
  30. self.show_archived = bool(show_archived)
  31. self.show_deleted = bool(show_deleted)
  32. self.show_active = bool(show_active)
  33. class UserInContext(object):
  34. """
  35. Interface to get User data and User data related to context.
  36. """
  37. def __init__(self, user: User, dbsession: Session, config: CFG):
  38. self.user = user
  39. self.dbsession = dbsession
  40. self.config = config
  41. # Default
  42. @property
  43. def email(self) -> str:
  44. return self.user.email
  45. @property
  46. def user_id(self) -> int:
  47. return self.user.user_id
  48. @property
  49. def display_name(self) -> str:
  50. return self.user.display_name
  51. @property
  52. def created(self) -> datetime:
  53. return self.user.created
  54. @property
  55. def is_active(self) -> bool:
  56. return self.user.is_active
  57. @property
  58. def timezone(self) -> str:
  59. return self.user.timezone
  60. @property
  61. def profile(self) -> Profile:
  62. return self.user.profile
  63. # Context related
  64. @property
  65. def calendar_url(self) -> typing.Optional[str]:
  66. # TODO - G-M - 20-04-2018 - [Calendar] Replace calendar code to get
  67. # url calendar url.
  68. #
  69. # from tracim.lib.calendar import CalendarManager
  70. # calendar_manager = CalendarManager(None)
  71. # return calendar_manager.get_workspace_calendar_url(self.workspace_id)
  72. return None
  73. @property
  74. def avatar_url(self) -> typing.Optional[str]:
  75. # TODO - G-M - 20-04-2018 - [Avatar] Add user avatar feature
  76. return None
  77. class WorkspaceInContext(object):
  78. """
  79. Interface to get Workspace data and Workspace data related to context.
  80. """
  81. def __init__(self, workspace: Workspace, dbsession: Session, config: CFG):
  82. self.workspace = workspace
  83. self.dbsession = dbsession
  84. self.config = config
  85. @property
  86. def workspace_id(self) -> int:
  87. """
  88. numeric id of the workspace.
  89. """
  90. return self.workspace.workspace_id
  91. @property
  92. def id(self) -> int:
  93. """
  94. alias of workspace_id
  95. """
  96. return self.workspace_id
  97. @property
  98. def label(self) -> str:
  99. """
  100. get workspace label
  101. """
  102. return self.workspace.label
  103. @property
  104. def description(self) -> str:
  105. """
  106. get workspace description
  107. """
  108. return self.workspace.description
  109. @property
  110. def slug(self) -> str:
  111. """
  112. get workspace slug
  113. """
  114. return slugify(self.workspace.label)
  115. @property
  116. def sidebar_entries(self) -> typing.List[WorkspaceMenuEntry]:
  117. """
  118. get sidebar entries, those depends on activated apps.
  119. """
  120. # TODO - G.M - 22-05-2018 - Rework on this in
  121. # order to not use hardcoded list
  122. # list should be able to change (depending on activated/disabled
  123. # apps)
  124. return default_workspace_menu_entry(self.workspace)
  125. class UserRoleWorkspaceInContext(object):
  126. """
  127. Interface to get UserRoleInWorkspace data and related content
  128. """
  129. def __init__(
  130. self,
  131. user_role: UserRoleInWorkspace,
  132. dbsession: Session,
  133. config: CFG,
  134. )-> None:
  135. self.user_role = user_role
  136. self.dbsession = dbsession
  137. self.config = config
  138. @property
  139. def user_id(self) -> int:
  140. """
  141. User who has the role has this id
  142. :return: user id as integer
  143. """
  144. return self.user_role.user_id
  145. @property
  146. def workspace_id(self) -> int:
  147. """
  148. This role apply only on the workspace with this workspace_id
  149. :return: workspace id as integer
  150. """
  151. return self.user_role.workspace_id
  152. # TODO - G.M - 23-05-2018 - Check the API spec for this this !
  153. @property
  154. def role_id(self) -> int:
  155. """
  156. role as int id, each value refer to a different role.
  157. """
  158. return self.user_role.role
  159. @property
  160. def role_slug(self) -> str:
  161. """
  162. simple name of the role of the user.
  163. can be anything from UserRoleInWorkspace SLUG, like
  164. 'not_applicable', 'reader',
  165. 'contributor', 'content_manager', 'workspace_manager'
  166. :return: user workspace role as slug.
  167. """
  168. return UserRoleInWorkspace.SLUG[self.user_role.role]
  169. @property
  170. def user(self) -> UserInContext:
  171. """
  172. User who has this role, with context data
  173. :return: UserInContext object
  174. """
  175. return UserInContext(
  176. self.user_role.user,
  177. self.dbsession,
  178. self.config
  179. )
  180. @property
  181. def workspace(self) -> WorkspaceInContext:
  182. """
  183. Workspace related to this role, with his context data
  184. :return: WorkspaceInContext object
  185. """
  186. return WorkspaceInContext(
  187. self.user_role.workspace,
  188. self.dbsession,
  189. self.config
  190. )
  191. class ContentInContext(object):
  192. """
  193. Interface to get Content data and Content data related to context.
  194. """
  195. def __init__(self, content: Content, dbsession: Session, config: CFG):
  196. self.content = content
  197. self.dbsession = dbsession
  198. self.config = config
  199. # Default
  200. @property
  201. def id(self) -> int:
  202. return self.content.content_id
  203. @property
  204. def parent_id(self) -> int:
  205. return self.content.parent_id
  206. @property
  207. def workspace_id(self) -> int:
  208. return self.content.workspace_id
  209. @property
  210. def label(self) -> str:
  211. return self.content.label
  212. @property
  213. def content_type_slug(self) -> str:
  214. return self.content.type
  215. @property
  216. def sub_content_type_slug(self) -> typing.List[str]:
  217. return [type.slug for type in self.content.get_allowed_content_types()]
  218. @property
  219. def status_slug(self) -> str:
  220. return self.content.status
  221. @property
  222. def is_archived(self):
  223. return self.content.is_archived
  224. @property
  225. def is_deleted(self):
  226. return self.content.is_deleted
  227. # Context-related
  228. @property
  229. def show_in_ui(self):
  230. # TODO - G.M - 31-05-2018 - Enable Show_in_ui params
  231. # if false, then do not show content in the treeview.
  232. # This may his maybe used for specific contents or for sub-contents.
  233. # Default is True.
  234. # In first version of the API, this field is always True
  235. return True
  236. @property
  237. def slug(self):
  238. return slugify(self.content.label)