context_models.py 16KB

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