context_models.py 22KB

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