context_models.py 8.0KB

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