context_models.py 22KB

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