context_models.py 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. # coding=utf-8
  2. import typing
  3. from datetime import datetime
  4. from enum import Enum
  5. from slugify import slugify
  6. from sqlalchemy.orm import Session
  7. from tracim_backend import CFG
  8. from tracim_backend.config import PreviewDim
  9. from tracim_backend.models import User
  10. from tracim_backend.models.auth import Profile
  11. from tracim_backend.models.data import Content
  12. from tracim_backend.models.data import ContentRevisionRO
  13. from tracim_backend.models.data import Workspace
  14. from tracim_backend.models.data import UserRoleInWorkspace
  15. from tracim_backend.models.roles import WorkspaceRoles
  16. from tracim_backend.models.workspace_menu_entries import default_workspace_menu_entry
  17. from tracim_backend.models.workspace_menu_entries import WorkspaceMenuEntry
  18. from tracim_backend.models.contents import CONTENT_TYPES
  19. class PreviewAllowedDim(object):
  20. def __init__(
  21. self,
  22. restricted:bool,
  23. dimensions: typing.List[PreviewDim]
  24. ) -> None:
  25. self.restricted = restricted
  26. self.dimensions = dimensions
  27. class MoveParams(object):
  28. """
  29. Json body params for move action model
  30. """
  31. def __init__(self, new_parent_id: str, new_workspace_id: str = None) -> None: # nopep8
  32. self.new_parent_id = new_parent_id
  33. self.new_workspace_id = new_workspace_id
  34. class LoginCredentials(object):
  35. """
  36. Login credentials model for login model
  37. """
  38. def __init__(self, email: str, password: str) -> None:
  39. self.email = email
  40. self.password = password
  41. class SetEmail(object):
  42. """
  43. Just an email
  44. """
  45. def __init__(self, loggedin_user_password: str, email: str) -> None:
  46. self.loggedin_user_password = loggedin_user_password
  47. self.email = email
  48. class SetPassword(object):
  49. """
  50. Just an password
  51. """
  52. def __init__(self,
  53. loggedin_user_password: str,
  54. new_password: str,
  55. new_password2: str
  56. ) -> None:
  57. self.loggedin_user_password = loggedin_user_password
  58. self.new_password = new_password
  59. self.new_password2 = new_password2
  60. class UserInfos(object):
  61. """
  62. Just some user infos
  63. """
  64. def __init__(self, timezone: str, public_name: str) -> None:
  65. self.timezone = timezone
  66. self.public_name = public_name
  67. class UserProfile(object):
  68. """
  69. Just some user infos
  70. """
  71. def __init__(self, profile: str) -> None:
  72. self.profile = profile
  73. class UserCreation(object):
  74. """
  75. Just some user infos
  76. """
  77. def __init__(
  78. self,
  79. email: str,
  80. password: str,
  81. public_name: str,
  82. timezone: str,
  83. profile: str,
  84. email_notification: str,
  85. ) -> None:
  86. self.email = email
  87. self.password = password
  88. self.public_name = public_name
  89. self.timezone = timezone
  90. self.profile = profile
  91. self.email_notification = email_notification
  92. class WorkspaceAndContentPath(object):
  93. """
  94. Paths params with workspace id and content_id model
  95. """
  96. def __init__(self, workspace_id: int, content_id: int) -> None:
  97. self.content_id = content_id
  98. self.workspace_id = workspace_id
  99. class WorkspaceAndContentRevisionPath(object):
  100. """
  101. Paths params with workspace id and content_id model
  102. """
  103. def __init__(self, workspace_id: int, content_id: int, revision_id) -> None:
  104. self.content_id = content_id
  105. self.revision_id = revision_id
  106. self.workspace_id = workspace_id
  107. class ContentPreviewSizedPath(object):
  108. """
  109. Paths params with workspace id and content_id, width, heigth
  110. """
  111. def __init__(self, workspace_id: int, content_id: int, width: int, height: int) -> None: # nopep8
  112. self.content_id = content_id
  113. self.workspace_id = workspace_id
  114. self.width = width
  115. self.height = height
  116. class RevisionPreviewSizedPath(object):
  117. """
  118. Paths params with workspace id and content_id, revision_id width, heigth
  119. """
  120. def __init__(self, workspace_id: int, content_id: int, revision_id: int, width: int, height: int) -> None: # nopep8
  121. self.content_id = content_id
  122. self.revision_id = revision_id
  123. self.workspace_id = workspace_id
  124. self.width = width
  125. self.height = height
  126. class WorkspaceAndUserPath(object):
  127. """
  128. Paths params with workspace id and user_id
  129. """
  130. def __init__(self, workspace_id: int, user_id: int):
  131. self.workspace_id = workspace_id
  132. self.user_id = workspace_id
  133. class UserWorkspaceAndContentPath(object):
  134. """
  135. Paths params with user_id, workspace id and content_id model
  136. """
  137. def __init__(self, user_id: int, workspace_id: int, content_id: int) -> None: # nopep8
  138. self.content_id = content_id
  139. self.workspace_id = workspace_id
  140. self.user_id = user_id
  141. class CommentPath(object):
  142. """
  143. Paths params with workspace id and content_id and comment_id model
  144. """
  145. def __init__(
  146. self,
  147. workspace_id: int,
  148. content_id: int,
  149. comment_id: int
  150. ) -> None:
  151. self.content_id = content_id
  152. self.workspace_id = workspace_id
  153. self.comment_id = comment_id
  154. class AutocompleteQuery(object):
  155. """
  156. Autocomplete query model
  157. """
  158. def __init__(self, acp: str):
  159. self.acp = acp
  160. class PageQuery(object):
  161. """
  162. Page query model
  163. """
  164. def __init__(
  165. self,
  166. page: int = 0
  167. ):
  168. self.page = page
  169. class ContentFilter(object):
  170. """
  171. Content filter model
  172. """
  173. def __init__(
  174. self,
  175. workspace_id: int = None,
  176. parent_id: int = None,
  177. show_archived: int = 0,
  178. show_deleted: int = 0,
  179. show_active: int = 1,
  180. content_type: str = None,
  181. offset: int = None,
  182. limit: int = None,
  183. ) -> None:
  184. self.parent_id = parent_id
  185. self.workspace_id = workspace_id
  186. self.show_archived = bool(show_archived)
  187. self.show_deleted = bool(show_deleted)
  188. self.show_active = bool(show_active)
  189. self.limit = limit
  190. self.offset = offset
  191. self.content_type = content_type
  192. class ActiveContentFilter(object):
  193. def __init__(
  194. self,
  195. limit: int = None,
  196. before_content_id: datetime = None,
  197. ):
  198. self.limit = limit
  199. self.before_content_id = before_content_id
  200. class ContentIdsQuery(object):
  201. def __init__(
  202. self,
  203. contents_ids: typing.List[int] = None,
  204. ):
  205. self.contents_ids = contents_ids
  206. class RoleUpdate(object):
  207. """
  208. Update role
  209. """
  210. def __init__(
  211. self,
  212. role: str,
  213. ):
  214. self.role = role
  215. class WorkspaceMemberInvitation(object):
  216. """
  217. Workspace Member Invitation
  218. """
  219. def __init__(
  220. self,
  221. user_id: int,
  222. user_email_or_public_name: str,
  223. role: str,
  224. ):
  225. self.role = role
  226. self.user_email_or_public_name = user_email_or_public_name
  227. self.user_id = user_id
  228. class WorkspaceUpdate(object):
  229. """
  230. Update workspace
  231. """
  232. def __init__(
  233. self,
  234. label: str,
  235. description: str,
  236. ):
  237. self.label = label
  238. self.description = description
  239. class ContentCreation(object):
  240. """
  241. Content creation model
  242. """
  243. def __init__(
  244. self,
  245. label: str,
  246. content_type: str,
  247. parent_id: typing.Optional[int] = None,
  248. ) -> None:
  249. self.label = label
  250. self.content_type = content_type
  251. self.parent_id = parent_id or None
  252. class CommentCreation(object):
  253. """
  254. Comment creation model
  255. """
  256. def __init__(
  257. self,
  258. raw_content: str,
  259. ) -> None:
  260. self.raw_content = raw_content
  261. class SetContentStatus(object):
  262. """
  263. Set content status
  264. """
  265. def __init__(
  266. self,
  267. status: str,
  268. ) -> None:
  269. self.status = status
  270. class TextBasedContentUpdate(object):
  271. """
  272. TextBasedContent update model
  273. """
  274. def __init__(
  275. self,
  276. label: str,
  277. raw_content: str,
  278. ) -> None:
  279. self.label = label
  280. self.raw_content = raw_content
  281. class TypeUser(Enum):
  282. """Params used to find user"""
  283. USER_ID = 'found_id'
  284. EMAIL = 'found_email'
  285. PUBLIC_NAME = 'found_public_name'
  286. class UserInContext(object):
  287. """
  288. Interface to get User data and User data related to context.
  289. """
  290. def __init__(self, user: User, dbsession: Session, config: CFG):
  291. self.user = user
  292. self.dbsession = dbsession
  293. self.config = config
  294. # Default
  295. @property
  296. def email(self) -> str:
  297. return self.user.email
  298. @property
  299. def user_id(self) -> int:
  300. return self.user.user_id
  301. @property
  302. def public_name(self) -> str:
  303. return self.display_name
  304. @property
  305. def display_name(self) -> str:
  306. return self.user.display_name
  307. @property
  308. def created(self) -> datetime:
  309. return self.user.created
  310. @property
  311. def is_active(self) -> bool:
  312. return self.user.is_active
  313. @property
  314. def timezone(self) -> str:
  315. return self.user.timezone
  316. @property
  317. def profile(self) -> Profile:
  318. return self.user.profile.name
  319. # Context related
  320. @property
  321. def calendar_url(self) -> typing.Optional[str]:
  322. # TODO - G-M - 20-04-2018 - [Calendar] Replace calendar code to get
  323. # url calendar url.
  324. #
  325. # from tracim.lib.calendar import CalendarManager
  326. # calendar_manager = CalendarManager(None)
  327. # return calendar_manager.get_workspace_calendar_url(self.workspace_id)
  328. return None
  329. @property
  330. def avatar_url(self) -> typing.Optional[str]:
  331. # TODO - G-M - 20-04-2018 - [Avatar] Add user avatar feature
  332. return None
  333. class WorkspaceInContext(object):
  334. """
  335. Interface to get Workspace data and Workspace data related to context.
  336. """
  337. def __init__(self, workspace: Workspace, dbsession: Session, config: CFG):
  338. self.workspace = workspace
  339. self.dbsession = dbsession
  340. self.config = config
  341. @property
  342. def workspace_id(self) -> int:
  343. """
  344. numeric id of the workspace.
  345. """
  346. return self.workspace.workspace_id
  347. @property
  348. def id(self) -> int:
  349. """
  350. alias of workspace_id
  351. """
  352. return self.workspace_id
  353. @property
  354. def label(self) -> str:
  355. """
  356. get workspace label
  357. """
  358. return self.workspace.label
  359. @property
  360. def description(self) -> str:
  361. """
  362. get workspace description
  363. """
  364. return self.workspace.description
  365. @property
  366. def slug(self) -> str:
  367. """
  368. get workspace slug
  369. """
  370. return slugify(self.workspace.label)
  371. @property
  372. def sidebar_entries(self) -> typing.List[WorkspaceMenuEntry]:
  373. """
  374. get sidebar entries, those depends on activated apps.
  375. """
  376. # TODO - G.M - 22-05-2018 - Rework on this in
  377. # order to not use hardcoded list
  378. # list should be able to change (depending on activated/disabled
  379. # apps)
  380. return default_workspace_menu_entry(self.workspace)
  381. class UserRoleWorkspaceInContext(object):
  382. """
  383. Interface to get UserRoleInWorkspace data and related content
  384. """
  385. def __init__(
  386. self,
  387. user_role: UserRoleInWorkspace,
  388. dbsession: Session,
  389. config: CFG,
  390. # Extended params
  391. newly_created: bool = None,
  392. email_sent: bool = None
  393. )-> None:
  394. self.user_role = user_role
  395. self.dbsession = dbsession
  396. self.config = config
  397. # Extended params
  398. self.newly_created = newly_created
  399. self.email_sent = email_sent
  400. @property
  401. def user_id(self) -> int:
  402. """
  403. User who has the role has this id
  404. :return: user id as integer
  405. """
  406. return self.user_role.user_id
  407. @property
  408. def workspace_id(self) -> int:
  409. """
  410. This role apply only on the workspace with this workspace_id
  411. :return: workspace id as integer
  412. """
  413. return self.user_role.workspace_id
  414. # TODO - G.M - 23-05-2018 - Check the API spec for this this !
  415. @property
  416. def role_id(self) -> int:
  417. """
  418. role as int id, each value refer to a different role.
  419. """
  420. return self.user_role.role
  421. @property
  422. def role(self) -> str:
  423. return self.role_slug
  424. @property
  425. def role_slug(self) -> str:
  426. """
  427. simple name of the role of the user.
  428. can be anything from UserRoleInWorkspace SLUG, like
  429. 'not_applicable', 'reader',
  430. 'contributor', 'content-manager', 'workspace-manager'
  431. :return: user workspace role as slug.
  432. """
  433. return WorkspaceRoles.get_role_from_level(self.user_role.role).slug
  434. @property
  435. def is_active(self) -> bool:
  436. return self.user.is_active
  437. @property
  438. def user(self) -> UserInContext:
  439. """
  440. User who has this role, with context data
  441. :return: UserInContext object
  442. """
  443. return UserInContext(
  444. self.user_role.user,
  445. self.dbsession,
  446. self.config
  447. )
  448. @property
  449. def workspace(self) -> WorkspaceInContext:
  450. """
  451. Workspace related to this role, with his context data
  452. :return: WorkspaceInContext object
  453. """
  454. return WorkspaceInContext(
  455. self.user_role.workspace,
  456. self.dbsession,
  457. self.config
  458. )
  459. class ContentInContext(object):
  460. """
  461. Interface to get Content data and Content data related to context.
  462. """
  463. def __init__(self, content: Content, dbsession: Session, config: CFG, user: User=None): # nopep8
  464. self.content = content
  465. self.dbsession = dbsession
  466. self.config = config
  467. self._user = user
  468. # Default
  469. @property
  470. def content_id(self) -> int:
  471. return self.content.content_id
  472. @property
  473. def parent_id(self) -> int:
  474. """
  475. Return parent_id of the content
  476. """
  477. return self.content.parent_id
  478. @property
  479. def workspace_id(self) -> int:
  480. return self.content.workspace_id
  481. @property
  482. def label(self) -> str:
  483. return self.content.label
  484. @property
  485. def content_type(self) -> str:
  486. content_type = CONTENT_TYPES.get_one_by_slug(self.content.type)
  487. return content_type.slug
  488. @property
  489. def sub_content_types(self) -> typing.List[str]:
  490. return [_type.slug for _type in self.content.get_allowed_content_types()] # nopep8
  491. @property
  492. def status(self) -> str:
  493. return self.content.status
  494. @property
  495. def is_archived(self):
  496. return self.content.is_archived
  497. @property
  498. def is_deleted(self):
  499. return self.content.is_deleted
  500. @property
  501. def raw_content(self):
  502. return self.content.description
  503. @property
  504. def author(self):
  505. return UserInContext(
  506. dbsession=self.dbsession,
  507. config=self.config,
  508. user=self.content.first_revision.owner
  509. )
  510. @property
  511. def current_revision_id(self):
  512. return self.content.revision_id
  513. @property
  514. def created(self):
  515. return self.content.created
  516. @property
  517. def modified(self):
  518. return self.updated
  519. @property
  520. def updated(self):
  521. return self.content.updated
  522. @property
  523. def last_modifier(self):
  524. return UserInContext(
  525. dbsession=self.dbsession,
  526. config=self.config,
  527. user=self.content.last_revision.owner
  528. )
  529. # Context-related
  530. @property
  531. def show_in_ui(self):
  532. # TODO - G.M - 31-05-2018 - Enable Show_in_ui params
  533. # if false, then do not show content in the treeview.
  534. # This may his maybe used for specific contents or for sub-contents.
  535. # Default is True.
  536. # In first version of the API, this field is always True
  537. return True
  538. @property
  539. def slug(self):
  540. return slugify(self.content.label)
  541. @property
  542. def read_by_user(self):
  543. assert self._user
  544. return not self.content.has_new_information_for(self._user)
  545. class RevisionInContext(object):
  546. """
  547. Interface to get Content data and Content data related to context.
  548. """
  549. def __init__(self, content_revision: ContentRevisionRO, dbsession: Session, config: CFG):
  550. assert content_revision is not None
  551. self.revision = content_revision
  552. self.dbsession = dbsession
  553. self.config = config
  554. # Default
  555. @property
  556. def content_id(self) -> int:
  557. return self.revision.content_id
  558. @property
  559. def parent_id(self) -> int:
  560. """
  561. Return parent_id of the content
  562. """
  563. return self.revision.parent_id
  564. @property
  565. def workspace_id(self) -> int:
  566. return self.revision.workspace_id
  567. @property
  568. def label(self) -> str:
  569. return self.revision.label
  570. @property
  571. def revision_type(self) -> str:
  572. return self.revision.revision_type
  573. @property
  574. def content_type(self) -> str:
  575. return CONTENT_TYPES.get_one_by_slug(self.revision.type).slug
  576. @property
  577. def sub_content_types(self) -> typing.List[str]:
  578. return [_type.slug for _type
  579. in self.revision.node.get_allowed_content_types()]
  580. @property
  581. def status(self) -> str:
  582. return self.revision.status
  583. @property
  584. def is_archived(self) -> bool:
  585. return self.revision.is_archived
  586. @property
  587. def is_deleted(self) -> bool:
  588. return self.revision.is_deleted
  589. @property
  590. def raw_content(self) -> str:
  591. return self.revision.description
  592. @property
  593. def author(self) -> UserInContext:
  594. return UserInContext(
  595. dbsession=self.dbsession,
  596. config=self.config,
  597. user=self.revision.owner
  598. )
  599. @property
  600. def revision_id(self) -> int:
  601. return self.revision.revision_id
  602. @property
  603. def created(self) -> datetime:
  604. return self.updated
  605. @property
  606. def modified(self) -> datetime:
  607. return self.updated
  608. @property
  609. def updated(self) -> datetime:
  610. return self.revision.updated
  611. @property
  612. def next_revision(self) -> typing.Optional[ContentRevisionRO]:
  613. """
  614. Get next revision (later revision)
  615. :return: next_revision
  616. """
  617. next_revision = None
  618. revisions = self.revision.node.revisions
  619. # INFO - G.M - 2018-06-177 - Get revisions more recent that
  620. # current one
  621. next_revisions = [
  622. revision for revision in revisions
  623. if revision.revision_id > self.revision.revision_id
  624. ]
  625. if next_revisions:
  626. # INFO - G.M - 2018-06-177 -sort revisions by date
  627. sorted_next_revisions = sorted(
  628. next_revisions,
  629. key=lambda revision: revision.updated
  630. )
  631. # INFO - G.M - 2018-06-177 - return only next revision
  632. return sorted_next_revisions[0]
  633. else:
  634. return None
  635. @property
  636. def comment_ids(self) -> typing.List[int]:
  637. """
  638. Get list of ids of all current revision related comments
  639. :return: list of comments ids
  640. """
  641. comments = self.revision.node.get_comments()
  642. # INFO - G.M - 2018-06-177 - Get comments more recent than revision.
  643. revision_comments = [
  644. comment for comment in comments
  645. if comment.created > self.revision.updated
  646. ]
  647. if self.next_revision:
  648. # INFO - G.M - 2018-06-177 - if there is a revision more recent
  649. # than current remove comments from theses rev (comments older
  650. # than next_revision.)
  651. revision_comments = [
  652. comment for comment in revision_comments
  653. if comment.created < self.next_revision.updated
  654. ]
  655. sorted_revision_comments = sorted(
  656. revision_comments,
  657. key=lambda revision: revision.created
  658. )
  659. comment_ids = []
  660. for comment in sorted_revision_comments:
  661. comment_ids.append(comment.content_id)
  662. return comment_ids
  663. # Context-related
  664. @property
  665. def show_in_ui(self) -> bool:
  666. # TODO - G.M - 31-05-2018 - Enable Show_in_ui params
  667. # if false, then do not show content in the treeview.
  668. # This may his maybe used for specific contents or for sub-contents.
  669. # Default is True.
  670. # In first version of the API, this field is always True
  671. return True
  672. @property
  673. def slug(self) -> str:
  674. return slugify(self.revision.label)