context_models.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 ContentRevisionRO
  11. from tracim.models.data import Workspace, UserRoleInWorkspace
  12. from tracim.models.workspace_menu_entries import default_workspace_menu_entry
  13. from tracim.models.workspace_menu_entries import WorkspaceMenuEntry
  14. from tracim.models.contents import ContentTypeLegacy as ContentType
  15. class MoveParams(object):
  16. """
  17. Json body params for move action model
  18. """
  19. def __init__(self, new_parent_id: str, new_workspace_id: str = None) -> None: # nopep8
  20. self.new_parent_id = new_parent_id
  21. self.new_workspace_id = new_workspace_id
  22. class LoginCredentials(object):
  23. """
  24. Login credentials model for login model
  25. """
  26. def __init__(self, email: str, password: str) -> None:
  27. self.email = email
  28. self.password = password
  29. class WorkspaceAndContentPath(object):
  30. """
  31. Paths params with workspace id and content_id model
  32. """
  33. def __init__(self, workspace_id: int, content_id: int) -> None:
  34. self.content_id = content_id
  35. self.workspace_id = workspace_id
  36. class CommentPath(object):
  37. """
  38. Paths params with workspace id and content_id and comment_id model
  39. """
  40. def __init__(
  41. self,
  42. workspace_id: int,
  43. content_id: int,
  44. comment_id: int
  45. ) -> None:
  46. self.content_id = content_id
  47. self.workspace_id = workspace_id
  48. self.comment_id = comment_id
  49. class ContentFilter(object):
  50. """
  51. Content filter model
  52. """
  53. def __init__(
  54. self,
  55. parent_id: int = None,
  56. show_archived: int = 0,
  57. show_deleted: int = 0,
  58. show_active: int = 1,
  59. ) -> None:
  60. self.parent_id = parent_id
  61. self.show_archived = bool(show_archived)
  62. self.show_deleted = bool(show_deleted)
  63. self.show_active = bool(show_active)
  64. class ContentCreation(object):
  65. """
  66. Content creation model
  67. """
  68. def __init__(
  69. self,
  70. label: str,
  71. content_type: str,
  72. ) -> None:
  73. self.label = label
  74. self.content_type = content_type
  75. class CommentCreation(object):
  76. """
  77. Comment creation model
  78. """
  79. def __init__(
  80. self,
  81. raw_content: str,
  82. ) -> None:
  83. self.raw_content = raw_content
  84. class SetContentStatus(object):
  85. """
  86. Set content status
  87. """
  88. def __init__(
  89. self,
  90. status: str,
  91. ) -> None:
  92. self.status = status
  93. class HTMLDocumentUpdate(object):
  94. """
  95. Html Document update model
  96. """
  97. def __init__(
  98. self,
  99. label: str,
  100. raw_content: str,
  101. ) -> None:
  102. self.label = label
  103. self.raw_content = raw_content
  104. class ThreadUpdate(object):
  105. """
  106. Thread update model
  107. """
  108. def __init__(
  109. self,
  110. label: str,
  111. raw_content: str,
  112. ) -> None:
  113. self.label = label
  114. self.raw_content = raw_content
  115. class UserInContext(object):
  116. """
  117. Interface to get User data and User data related to context.
  118. """
  119. def __init__(self, user: User, dbsession: Session, config: CFG):
  120. self.user = user
  121. self.dbsession = dbsession
  122. self.config = config
  123. # Default
  124. @property
  125. def email(self) -> str:
  126. return self.user.email
  127. @property
  128. def user_id(self) -> int:
  129. return self.user.user_id
  130. @property
  131. def public_name(self) -> str:
  132. return self.display_name
  133. @property
  134. def display_name(self) -> str:
  135. return self.user.display_name
  136. @property
  137. def created(self) -> datetime:
  138. return self.user.created
  139. @property
  140. def is_active(self) -> bool:
  141. return self.user.is_active
  142. @property
  143. def timezone(self) -> str:
  144. return self.user.timezone
  145. @property
  146. def profile(self) -> Profile:
  147. return self.user.profile.name
  148. # Context related
  149. @property
  150. def calendar_url(self) -> typing.Optional[str]:
  151. # TODO - G-M - 20-04-2018 - [Calendar] Replace calendar code to get
  152. # url calendar url.
  153. #
  154. # from tracim.lib.calendar import CalendarManager
  155. # calendar_manager = CalendarManager(None)
  156. # return calendar_manager.get_workspace_calendar_url(self.workspace_id)
  157. return None
  158. @property
  159. def avatar_url(self) -> typing.Optional[str]:
  160. # TODO - G-M - 20-04-2018 - [Avatar] Add user avatar feature
  161. return None
  162. class WorkspaceInContext(object):
  163. """
  164. Interface to get Workspace data and Workspace data related to context.
  165. """
  166. def __init__(self, workspace: Workspace, dbsession: Session, config: CFG):
  167. self.workspace = workspace
  168. self.dbsession = dbsession
  169. self.config = config
  170. @property
  171. def workspace_id(self) -> int:
  172. """
  173. numeric id of the workspace.
  174. """
  175. return self.workspace.workspace_id
  176. @property
  177. def id(self) -> int:
  178. """
  179. alias of workspace_id
  180. """
  181. return self.workspace_id
  182. @property
  183. def label(self) -> str:
  184. """
  185. get workspace label
  186. """
  187. return self.workspace.label
  188. @property
  189. def description(self) -> str:
  190. """
  191. get workspace description
  192. """
  193. return self.workspace.description
  194. @property
  195. def slug(self) -> str:
  196. """
  197. get workspace slug
  198. """
  199. return slugify(self.workspace.label)
  200. @property
  201. def sidebar_entries(self) -> typing.List[WorkspaceMenuEntry]:
  202. """
  203. get sidebar entries, those depends on activated apps.
  204. """
  205. # TODO - G.M - 22-05-2018 - Rework on this in
  206. # order to not use hardcoded list
  207. # list should be able to change (depending on activated/disabled
  208. # apps)
  209. return default_workspace_menu_entry(self.workspace)
  210. class UserRoleWorkspaceInContext(object):
  211. """
  212. Interface to get UserRoleInWorkspace data and related content
  213. """
  214. def __init__(
  215. self,
  216. user_role: UserRoleInWorkspace,
  217. dbsession: Session,
  218. config: CFG,
  219. )-> None:
  220. self.user_role = user_role
  221. self.dbsession = dbsession
  222. self.config = config
  223. @property
  224. def user_id(self) -> int:
  225. """
  226. User who has the role has this id
  227. :return: user id as integer
  228. """
  229. return self.user_role.user_id
  230. @property
  231. def workspace_id(self) -> int:
  232. """
  233. This role apply only on the workspace with this workspace_id
  234. :return: workspace id as integer
  235. """
  236. return self.user_role.workspace_id
  237. # TODO - G.M - 23-05-2018 - Check the API spec for this this !
  238. @property
  239. def role_id(self) -> int:
  240. """
  241. role as int id, each value refer to a different role.
  242. """
  243. return self.user_role.role
  244. @property
  245. def role(self) -> str:
  246. return self.role_slug
  247. @property
  248. def role_slug(self) -> str:
  249. """
  250. simple name of the role of the user.
  251. can be anything from UserRoleInWorkspace SLUG, like
  252. 'not_applicable', 'reader',
  253. 'contributor', 'content-manager', 'workspace-manager'
  254. :return: user workspace role as slug.
  255. """
  256. return UserRoleInWorkspace.SLUG[self.user_role.role]
  257. @property
  258. def user(self) -> UserInContext:
  259. """
  260. User who has this role, with context data
  261. :return: UserInContext object
  262. """
  263. return UserInContext(
  264. self.user_role.user,
  265. self.dbsession,
  266. self.config
  267. )
  268. @property
  269. def workspace(self) -> WorkspaceInContext:
  270. """
  271. Workspace related to this role, with his context data
  272. :return: WorkspaceInContext object
  273. """
  274. return WorkspaceInContext(
  275. self.user_role.workspace,
  276. self.dbsession,
  277. self.config
  278. )
  279. class ContentInContext(object):
  280. """
  281. Interface to get Content data and Content data related to context.
  282. """
  283. def __init__(self, content: Content, dbsession: Session, config: CFG):
  284. self.content = content
  285. self.dbsession = dbsession
  286. self.config = config
  287. # Default
  288. @property
  289. def content_id(self) -> int:
  290. return self.content.content_id
  291. @property
  292. def id(self) -> int:
  293. return self.content_id
  294. @property
  295. def parent_id(self) -> int:
  296. """
  297. Return parent_id of the content
  298. """
  299. return self.content.parent_id
  300. @property
  301. def workspace_id(self) -> int:
  302. return self.content.workspace_id
  303. @property
  304. def label(self) -> str:
  305. return self.content.label
  306. @property
  307. def content_type(self) -> str:
  308. content_type = ContentType(self.content.type)
  309. return content_type.slug
  310. @property
  311. def sub_content_types(self) -> typing.List[str]:
  312. return [_type.slug for _type in self.content.get_allowed_content_types()] # nopep8
  313. @property
  314. def status(self) -> str:
  315. return self.content.status
  316. @property
  317. def is_archived(self):
  318. return self.content.is_archived
  319. @property
  320. def is_deleted(self):
  321. return self.content.is_deleted
  322. @property
  323. def raw_content(self):
  324. return self.content.description
  325. @property
  326. def author(self):
  327. return UserInContext(
  328. dbsession=self.dbsession,
  329. config=self.config,
  330. user=self.content.first_revision.owner
  331. )
  332. @property
  333. def current_revision_id(self):
  334. return self.content.revision_id
  335. @property
  336. def created(self):
  337. return self.content.created
  338. @property
  339. def modified(self):
  340. return self.updated
  341. @property
  342. def updated(self):
  343. return self.content.updated
  344. @property
  345. def last_modifier(self):
  346. return UserInContext(
  347. dbsession=self.dbsession,
  348. config=self.config,
  349. user=self.content.last_revision.owner
  350. )
  351. # Context-related
  352. @property
  353. def show_in_ui(self):
  354. # TODO - G.M - 31-05-2018 - Enable Show_in_ui params
  355. # if false, then do not show content in the treeview.
  356. # This may his maybe used for specific contents or for sub-contents.
  357. # Default is True.
  358. # In first version of the API, this field is always True
  359. return True
  360. @property
  361. def slug(self):
  362. return slugify(self.content.label)
  363. class RevisionInContext(object):
  364. """
  365. Interface to get Content data and Content data related to context.
  366. """
  367. def __init__(self, content_revision: ContentRevisionRO, dbsession: Session, config: CFG):
  368. assert content_revision is not None
  369. self.revision = content_revision
  370. self.dbsession = dbsession
  371. self.config = config
  372. # Default
  373. @property
  374. def content_id(self) -> int:
  375. return self.revision.content_id
  376. @property
  377. def id(self) -> int:
  378. return self.content_id
  379. @property
  380. def parent_id(self) -> int:
  381. """
  382. Return parent_id of the content
  383. """
  384. return self.revision.parent_id
  385. @property
  386. def workspace_id(self) -> int:
  387. return self.revision.workspace_id
  388. @property
  389. def label(self) -> str:
  390. return self.revision.label
  391. @property
  392. def content_type(self) -> str:
  393. content_type = ContentType(self.revision.type)
  394. if content_type:
  395. return content_type.slug
  396. else:
  397. return None
  398. @property
  399. def sub_content_types(self) -> typing.List[str]:
  400. return [_type.slug for _type
  401. in self.revision.node.get_allowed_content_types()]
  402. @property
  403. def status(self) -> str:
  404. return self.revision.status
  405. @property
  406. def is_archived(self) -> bool:
  407. return self.revision.is_archived
  408. @property
  409. def is_deleted(self) -> bool:
  410. return self.revision.is_deleted
  411. @property
  412. def raw_content(self) -> str:
  413. return self.revision.description
  414. @property
  415. def author(self) -> UserInContext:
  416. return UserInContext(
  417. dbsession=self.dbsession,
  418. config=self.config,
  419. user=self.revision.owner
  420. )
  421. @property
  422. def revision_id(self) -> int:
  423. return self.revision.revision_id
  424. @property
  425. def created(self) -> datetime:
  426. return self.updated
  427. @property
  428. def modified(self) -> datetime:
  429. return self.updated
  430. @property
  431. def updated(self) -> datetime:
  432. return self.revision.updated
  433. @property
  434. def next_revision(self) -> typing.Optional[ContentRevisionRO]:
  435. """
  436. Get next revision (later revision)
  437. :return: next_revision
  438. """
  439. next_revision = None
  440. revisions = self.revision.node.revisions
  441. # INFO - G.M - 2018-06-177 - Get revisions more recent that
  442. # current one
  443. next_revisions = [
  444. revision for revision in revisions
  445. if revision.revision_id > self.revision.revision_id
  446. ]
  447. if next_revisions:
  448. # INFO - G.M - 2018-06-177 -sort revisions by date
  449. sorted_next_revisions = sorted(
  450. next_revisions,
  451. key=lambda revision: revision.updated
  452. )
  453. # INFO - G.M - 2018-06-177 - return only next revision
  454. return sorted_next_revisions[0]
  455. else:
  456. return None
  457. @property
  458. def comment_ids(self) -> typing.List[int]:
  459. """
  460. Get list of ids of all current revision related comments
  461. :return: list of comments ids
  462. """
  463. comments = self.revision.node.get_comments()
  464. # INFO - G.M - 2018-06-177 - Get comments more recent than revision.
  465. revision_comments = [
  466. comment for comment in comments
  467. if comment.created > self.revision.updated
  468. ]
  469. if self.next_revision:
  470. # INFO - G.M - 2018-06-177 - if there is a revision more recent
  471. # than current remove comments from theses rev (comments older
  472. # than next_revision.)
  473. revision_comments = [
  474. comment for comment in revision_comments
  475. if comment.created < self.next_revision.updated
  476. ]
  477. sorted_revision_comments = sorted(
  478. revision_comments,
  479. key=lambda revision: revision.created
  480. )
  481. comment_ids = []
  482. for comment in sorted_revision_comments:
  483. comment_ids.append(comment.content_id)
  484. return comment_ids
  485. # Context-related
  486. @property
  487. def show_in_ui(self) -> bool:
  488. # TODO - G.M - 31-05-2018 - Enable Show_in_ui params
  489. # if false, then do not show content in the treeview.
  490. # This may his maybe used for specific contents or for sub-contents.
  491. # Default is True.
  492. # In first version of the API, this field is always True
  493. return True
  494. @property
  495. def slug(self) -> str:
  496. return slugify(self.revision.label)