context_models.py 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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: str,
  57. ):
  58. self.label = label
  59. self.content_type = content_type
  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 public_name(self) -> str:
  77. return self.display_name
  78. @property
  79. def display_name(self) -> str:
  80. return self.user.display_name
  81. @property
  82. def created(self) -> datetime:
  83. return self.user.created
  84. @property
  85. def is_active(self) -> bool:
  86. return self.user.is_active
  87. @property
  88. def timezone(self) -> str:
  89. return self.user.timezone
  90. @property
  91. def profile(self) -> Profile:
  92. return self.user.profile.name
  93. # Context related
  94. @property
  95. def calendar_url(self) -> typing.Optional[str]:
  96. # TODO - G-M - 20-04-2018 - [Calendar] Replace calendar code to get
  97. # url calendar url.
  98. #
  99. # from tracim.lib.calendar import CalendarManager
  100. # calendar_manager = CalendarManager(None)
  101. # return calendar_manager.get_workspace_calendar_url(self.workspace_id)
  102. return None
  103. @property
  104. def avatar_url(self) -> typing.Optional[str]:
  105. # TODO - G-M - 20-04-2018 - [Avatar] Add user avatar feature
  106. return None
  107. class WorkspaceInContext(object):
  108. """
  109. Interface to get Workspace data and Workspace data related to context.
  110. """
  111. def __init__(self, workspace: Workspace, dbsession: Session, config: CFG):
  112. self.workspace = workspace
  113. self.dbsession = dbsession
  114. self.config = config
  115. @property
  116. def workspace_id(self) -> int:
  117. """
  118. numeric id of the workspace.
  119. """
  120. return self.workspace.workspace_id
  121. @property
  122. def id(self) -> int:
  123. """
  124. alias of workspace_id
  125. """
  126. return self.workspace_id
  127. @property
  128. def label(self) -> str:
  129. """
  130. get workspace label
  131. """
  132. return self.workspace.label
  133. @property
  134. def description(self) -> str:
  135. """
  136. get workspace description
  137. """
  138. return self.workspace.description
  139. @property
  140. def slug(self) -> str:
  141. """
  142. get workspace slug
  143. """
  144. return slugify(self.workspace.label)
  145. @property
  146. def sidebar_entries(self) -> typing.List[WorkspaceMenuEntry]:
  147. """
  148. get sidebar entries, those depends on activated apps.
  149. """
  150. # TODO - G.M - 22-05-2018 - Rework on this in
  151. # order to not use hardcoded list
  152. # list should be able to change (depending on activated/disabled
  153. # apps)
  154. return default_workspace_menu_entry(self.workspace)
  155. class UserRoleWorkspaceInContext(object):
  156. """
  157. Interface to get UserRoleInWorkspace data and related content
  158. """
  159. def __init__(
  160. self,
  161. user_role: UserRoleInWorkspace,
  162. dbsession: Session,
  163. config: CFG,
  164. )-> None:
  165. self.user_role = user_role
  166. self.dbsession = dbsession
  167. self.config = config
  168. @property
  169. def user_id(self) -> int:
  170. """
  171. User who has the role has this id
  172. :return: user id as integer
  173. """
  174. return self.user_role.user_id
  175. @property
  176. def workspace_id(self) -> int:
  177. """
  178. This role apply only on the workspace with this workspace_id
  179. :return: workspace id as integer
  180. """
  181. return self.user_role.workspace_id
  182. # TODO - G.M - 23-05-2018 - Check the API spec for this this !
  183. @property
  184. def role_id(self) -> int:
  185. """
  186. role as int id, each value refer to a different role.
  187. """
  188. return self.user_role.role
  189. @property
  190. def role(self) -> str:
  191. return self.role_slug
  192. @property
  193. def role_slug(self) -> str:
  194. """
  195. simple name of the role of the user.
  196. can be anything from UserRoleInWorkspace SLUG, like
  197. 'not_applicable', 'reader',
  198. 'contributor', 'content-manager', 'workspace-manager'
  199. :return: user workspace role as slug.
  200. """
  201. return UserRoleInWorkspace.SLUG[self.user_role.role]
  202. @property
  203. def user(self) -> UserInContext:
  204. """
  205. User who has this role, with context data
  206. :return: UserInContext object
  207. """
  208. return UserInContext(
  209. self.user_role.user,
  210. self.dbsession,
  211. self.config
  212. )
  213. @property
  214. def workspace(self) -> WorkspaceInContext:
  215. """
  216. Workspace related to this role, with his context data
  217. :return: WorkspaceInContext object
  218. """
  219. return WorkspaceInContext(
  220. self.user_role.workspace,
  221. self.dbsession,
  222. self.config
  223. )
  224. class ContentInContext(object):
  225. """
  226. Interface to get Content data and Content data related to context.
  227. """
  228. def __init__(self, content: Content, dbsession: Session, config: CFG):
  229. self.content = content
  230. self.dbsession = dbsession
  231. self.config = config
  232. # Default
  233. @property
  234. def content_id(self) -> int:
  235. return self.content.content_id
  236. @property
  237. def id(self) -> int:
  238. return self.content_id
  239. @property
  240. def parent_id(self) -> int:
  241. """
  242. Return parent_id of the content
  243. """
  244. return self.content.parent_id
  245. @property
  246. def workspace_id(self) -> int:
  247. return self.content.workspace_id
  248. @property
  249. def label(self) -> str:
  250. return self.content.label
  251. @property
  252. def content_type(self) -> str:
  253. return self.content.type
  254. @property
  255. def sub_content_types(self) -> typing.List[str]:
  256. return [type.slug for type in self.content.get_allowed_content_types()]
  257. @property
  258. def status(self) -> str:
  259. return self.content.status
  260. @property
  261. def is_archived(self):
  262. return self.content.is_archived
  263. @property
  264. def is_deleted(self):
  265. return self.content.is_deleted
  266. # Context-related
  267. @property
  268. def show_in_ui(self):
  269. # TODO - G.M - 31-05-2018 - Enable Show_in_ui params
  270. # if false, then do not show content in the treeview.
  271. # This may his maybe used for specific contents or for sub-contents.
  272. # Default is True.
  273. # In first version of the API, this field is always True
  274. return True
  275. @property
  276. def slug(self):
  277. return slugify(self.content.label)