context_models.py 22KB

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