context_models.py 7.4KB

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