context_models.py 23KB

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