context_models.py 23KB

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