context_models.py 8.2KB

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