context_models.py 8.1KB

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