content.py 47KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. # -*- coding: utf-8 -*-
  2. from contextlib import contextmanager
  3. import os
  4. import datetime
  5. import re
  6. import typing
  7. from operator import itemgetter
  8. from operator import not_
  9. import transaction
  10. from sqlalchemy import func
  11. from sqlalchemy.orm import Query
  12. from depot.manager import DepotManager
  13. from depot.io.utils import FileIntent
  14. import sqlalchemy
  15. from sqlalchemy.orm import aliased
  16. from sqlalchemy.orm import joinedload
  17. from sqlalchemy.orm.attributes import get_history
  18. from sqlalchemy.orm.exc import NoResultFound
  19. from sqlalchemy.orm.session import Session
  20. from sqlalchemy import desc
  21. from sqlalchemy import distinct
  22. from sqlalchemy import or_
  23. from sqlalchemy.sql.elements import and_
  24. from tracim.lib.utils.utils import cmp_to_key
  25. from tracim.lib.core.notifications import NotifierFactory
  26. from tracim.exceptions import SameValueError
  27. from tracim.exceptions import ContentNotFound
  28. from tracim.exceptions import WorkspacesDoNotMatch
  29. from tracim.lib.utils.utils import current_date_for_filename
  30. from tracim.models.revision_protection import new_revision
  31. from tracim.models.auth import User
  32. from tracim.models.data import ActionDescription
  33. from tracim.models.data import ContentStatus
  34. from tracim.models.data import ContentRevisionRO
  35. from tracim.models.data import Content
  36. from tracim.models.data import ContentType
  37. from tracim.models.data import NodeTreeItem
  38. from tracim.models.data import RevisionReadStatus
  39. from tracim.models.data import UserRoleInWorkspace
  40. from tracim.models.data import Workspace
  41. from tracim.lib.utils.translation import fake_translator as _
  42. from tracim.models.context_models import RevisionInContext
  43. from tracim.models.context_models import ContentInContext
  44. __author__ = 'damien'
  45. def compare_content_for_sorting_by_type_and_name(
  46. content1: Content,
  47. content2: Content
  48. ) -> int:
  49. """
  50. :param content1:
  51. :param content2:
  52. :return: 1 if content1 > content2
  53. -1 if content1 < content2
  54. 0 if content1 = content2
  55. """
  56. if content1.type == content2.type:
  57. if content1.get_label().lower()>content2.get_label().lower():
  58. return 1
  59. elif content1.get_label().lower()<content2.get_label().lower():
  60. return -1
  61. return 0
  62. else:
  63. # TODO - D.A. - 2014-12-02 - Manage Content Types Dynamically
  64. content_type_order = [
  65. ContentType.Folder,
  66. ContentType.Page,
  67. ContentType.Thread,
  68. ContentType.File,
  69. ]
  70. content_1_type_index = content_type_order.index(content1.type)
  71. content_2_type_index = content_type_order.index(content2.type)
  72. result = content_1_type_index - content_2_type_index
  73. if result < 0:
  74. return -1
  75. elif result > 0:
  76. return 1
  77. else:
  78. return 0
  79. def compare_tree_items_for_sorting_by_type_and_name(
  80. item1: NodeTreeItem,
  81. item2: NodeTreeItem
  82. ) -> int:
  83. return compare_content_for_sorting_by_type_and_name(item1.node, item2.node)
  84. class ContentApi(object):
  85. SEARCH_SEPARATORS = ',| '
  86. SEARCH_DEFAULT_RESULT_NB = 50
  87. DISPLAYABLE_CONTENTS = (
  88. ContentType.Folder,
  89. ContentType.File,
  90. ContentType.Comment,
  91. ContentType.Thread,
  92. ContentType.Page,
  93. ContentType.MarkdownPage,
  94. )
  95. def __init__(
  96. self,
  97. session: Session,
  98. current_user: typing.Optional[User],
  99. config,
  100. show_archived: bool = False,
  101. show_deleted: bool = False,
  102. show_temporary: bool = False,
  103. show_active: bool = True,
  104. all_content_in_treeview: bool = True,
  105. force_show_all_types: bool = False,
  106. disable_user_workspaces_filter: bool = False,
  107. ) -> None:
  108. self._session = session
  109. self._user = current_user
  110. self._config = config
  111. self._user_id = current_user.user_id if current_user else None
  112. self._show_archived = show_archived
  113. self._show_deleted = show_deleted
  114. self._show_temporary = show_temporary
  115. self._show_active = show_active
  116. self._show_all_type_of_contents_in_treeview = all_content_in_treeview
  117. self._force_show_all_types = force_show_all_types
  118. self._disable_user_workspaces_filter = disable_user_workspaces_filter
  119. @contextmanager
  120. def show(
  121. self,
  122. show_archived: bool=False,
  123. show_deleted: bool=False,
  124. show_temporary: bool=False,
  125. ) -> typing.Generator['ContentApi', None, None]:
  126. """
  127. Use this method as context manager to update show_archived,
  128. show_deleted and show_temporary properties during context.
  129. :param show_archived: show archived contents
  130. :param show_deleted: show deleted contents
  131. :param show_temporary: show temporary contents
  132. """
  133. previous_show_archived = self._show_archived
  134. previous_show_deleted = self._show_deleted
  135. previous_show_temporary = self._show_temporary
  136. try:
  137. self._show_archived = show_archived
  138. self._show_deleted = show_deleted
  139. self._show_temporary = show_temporary
  140. yield self
  141. finally:
  142. self._show_archived = previous_show_archived
  143. self._show_deleted = previous_show_deleted
  144. self._show_temporary = previous_show_temporary
  145. def get_content_in_context(self, content: Content):
  146. return ContentInContext(content, self._session, self._config)
  147. def get_revision_in_context(self, revision: ContentRevisionRO):
  148. # TODO - G.M - 2018-06-173 - create revision in context object
  149. return RevisionInContext(revision, self._session, self._config)
  150. def get_revision_join(self) -> sqlalchemy.sql.elements.BooleanClauseList:
  151. """
  152. Return the Content/ContentRevision query join condition
  153. :return: Content/ContentRevision query join condition
  154. """
  155. return and_(Content.id == ContentRevisionRO.content_id,
  156. ContentRevisionRO.revision_id == self._session.query(
  157. ContentRevisionRO.revision_id)
  158. .filter(ContentRevisionRO.content_id == Content.id)
  159. .order_by(ContentRevisionRO.revision_id.desc())
  160. .limit(1)
  161. .correlate(Content))
  162. def get_canonical_query(self) -> Query:
  163. """
  164. Return the Content/ContentRevision base query who join these table on the last revision.
  165. :return: Content/ContentRevision Query
  166. """
  167. return self._session.query(Content)\
  168. .join(ContentRevisionRO, self.get_revision_join())
  169. @classmethod
  170. def sort_tree_items(
  171. cls,
  172. content_list: typing.List[NodeTreeItem],
  173. )-> typing.List[NodeTreeItem]:
  174. news = []
  175. for item in content_list:
  176. news.append(item)
  177. content_list.sort(key=cmp_to_key(
  178. compare_tree_items_for_sorting_by_type_and_name,
  179. ))
  180. return content_list
  181. @classmethod
  182. def sort_content(
  183. cls,
  184. content_list: typing.List[Content],
  185. ) -> typing.List[Content]:
  186. content_list.sort(key=cmp_to_key(compare_content_for_sorting_by_type_and_name))
  187. return content_list
  188. def __real_base_query(
  189. self,
  190. workspace: Workspace = None,
  191. ) -> Query:
  192. result = self.get_canonical_query()
  193. # Exclude non displayable types
  194. if not self._force_show_all_types:
  195. result = result.filter(Content.type.in_(self.DISPLAYABLE_CONTENTS))
  196. if workspace:
  197. result = result.filter(Content.workspace_id == workspace.workspace_id)
  198. # Security layer: if user provided, filter
  199. # with user workspaces privileges
  200. if self._user and not self._disable_user_workspaces_filter:
  201. user = self._session.query(User).get(self._user_id)
  202. # Filter according to user workspaces
  203. workspace_ids = [r.workspace_id for r in user.roles \
  204. if r.role>=UserRoleInWorkspace.READER]
  205. result = result.filter(or_(
  206. Content.workspace_id.in_(workspace_ids),
  207. # And allow access to non workspace document when he is owner
  208. and_(
  209. Content.workspace_id == None,
  210. Content.owner_id == self._user_id,
  211. )
  212. ))
  213. return result
  214. def _base_query(self, workspace: Workspace=None) -> Query:
  215. result = self.__real_base_query(workspace)
  216. if not self._show_active:
  217. result = result.filter(or_(
  218. Content.is_deleted==True,
  219. Content.is_archived==True,
  220. ))
  221. if not self._show_deleted:
  222. result = result.filter(Content.is_deleted==False)
  223. if not self._show_archived:
  224. result = result.filter(Content.is_archived==False)
  225. if not self._show_temporary:
  226. result = result.filter(Content.is_temporary==False)
  227. return result
  228. def __revisions_real_base_query(
  229. self,
  230. workspace: Workspace=None,
  231. ) -> Query:
  232. result = self._session.query(ContentRevisionRO)
  233. # Exclude non displayable types
  234. if not self._force_show_all_types:
  235. result = result.filter(Content.type.in_(self.DISPLAYABLE_CONTENTS))
  236. if workspace:
  237. result = result.filter(ContentRevisionRO.workspace_id==workspace.workspace_id)
  238. if self._user:
  239. user = self._session.query(User).get(self._user_id)
  240. # Filter according to user workspaces
  241. workspace_ids = [r.workspace_id for r in user.roles \
  242. if r.role>=UserRoleInWorkspace.READER]
  243. result = result.filter(ContentRevisionRO.workspace_id.in_(workspace_ids))
  244. return result
  245. def _revisions_base_query(
  246. self,
  247. workspace: Workspace=None,
  248. ) -> Query:
  249. result = self.__revisions_real_base_query(workspace)
  250. if not self._show_deleted:
  251. result = result.filter(ContentRevisionRO.is_deleted==False)
  252. if not self._show_archived:
  253. result = result.filter(ContentRevisionRO.is_archived==False)
  254. if not self._show_temporary:
  255. result = result.filter(Content.is_temporary==False)
  256. return result
  257. def _hard_filtered_base_query(
  258. self,
  259. workspace: Workspace=None,
  260. ) -> Query:
  261. """
  262. If set to True, then filterign on is_deleted and is_archived will also
  263. filter parent properties. This is required for search() function which
  264. also search in comments (for example) which may be 'not deleted' while
  265. the associated content is deleted
  266. :param hard_filtering:
  267. :return:
  268. """
  269. result = self.__real_base_query(workspace)
  270. if not self._show_deleted:
  271. parent = aliased(Content)
  272. result = result.join(parent, Content.parent).\
  273. filter(Content.is_deleted==False).\
  274. filter(parent.is_deleted==False)
  275. if not self._show_archived:
  276. parent = aliased(Content)
  277. result = result.join(parent, Content.parent).\
  278. filter(Content.is_archived==False).\
  279. filter(parent.is_archived==False)
  280. if not self._show_temporary:
  281. parent = aliased(Content)
  282. result = result.join(parent, Content.parent). \
  283. filter(Content.is_temporary == False). \
  284. filter(parent.is_temporary == False)
  285. return result
  286. def get_base_query(
  287. self,
  288. workspace: Workspace,
  289. ) -> Query:
  290. return self._base_query(workspace)
  291. def get_child_folders(self, parent: Content=None, workspace: Workspace=None, filter_by_allowed_content_types: list=[], removed_item_ids: list=[], allowed_node_types=None) -> typing.List[Content]:
  292. """
  293. This method returns child items (folders or items) for left bar treeview.
  294. :param parent:
  295. :param workspace:
  296. :param filter_by_allowed_content_types:
  297. :param removed_item_ids:
  298. :param allowed_node_types: This parameter allow to hide folders for which the given type of content is not allowed.
  299. For example, if you want to move a Page from a folder to another, you should show only folders that accept pages
  300. :return:
  301. """
  302. filter_by_allowed_content_types = filter_by_allowed_content_types or [] # FDV
  303. removed_item_ids = removed_item_ids or [] # FDV
  304. if not allowed_node_types:
  305. allowed_node_types = [ContentType.Folder]
  306. elif allowed_node_types==ContentType.Any:
  307. allowed_node_types = ContentType.all()
  308. parent_id = parent.content_id if parent else None
  309. folders = self._base_query(workspace).\
  310. filter(Content.parent_id==parent_id).\
  311. filter(Content.type.in_(allowed_node_types)).\
  312. filter(Content.content_id.notin_(removed_item_ids)).\
  313. all()
  314. if not filter_by_allowed_content_types or \
  315. len(filter_by_allowed_content_types)<=0:
  316. # Standard case for the left treeview: we want to show all contents
  317. # in the left treeview... so we still filter because for example
  318. # comments must not appear in the treeview
  319. return [folder for folder in folders \
  320. if folder.type in ContentType.allowed_types_for_folding()]
  321. # Now this is a case of Folders only (used for moving content)
  322. # When moving a content, you must get only folders that allow to be filled
  323. # with the type of content you want to move
  324. result = []
  325. for folder in folders:
  326. for allowed_content_type in filter_by_allowed_content_types:
  327. is_folder = folder.type == ContentType.Folder
  328. content_type__allowed = folder.properties['allowed_content'][allowed_content_type] == True
  329. if is_folder and content_type__allowed:
  330. result.append(folder)
  331. break
  332. return result
  333. def create(self, content_type: str, workspace: Workspace, parent: Content=None, label:str ='', do_save=False, is_temporary: bool=False, do_notify=True) -> Content:
  334. assert content_type in ContentType.allowed_types()
  335. if content_type == ContentType.Folder and not label:
  336. label = self.generate_folder_label(workspace, parent)
  337. content = Content()
  338. content.owner = self._user
  339. content.parent = parent
  340. content.workspace = workspace
  341. content.type = content_type
  342. content.label = label
  343. content.is_temporary = is_temporary
  344. content.revision_type = ActionDescription.CREATION
  345. if content.type in (
  346. ContentType.Page,
  347. ContentType.Thread,
  348. ):
  349. content.file_extension = '.html'
  350. if do_save:
  351. self._session.add(content)
  352. self.save(content, ActionDescription.CREATION, do_notify=do_notify)
  353. return content
  354. def create_comment(self, workspace: Workspace=None, parent: Content=None, content:str ='', do_save=False) -> Content:
  355. assert parent and parent.type!=ContentType.Folder
  356. item = Content()
  357. item.owner = self._user
  358. item.parent = parent
  359. if parent and not workspace:
  360. workspace = item.parent.workspace
  361. item.workspace = workspace
  362. item.type = ContentType.Comment
  363. item.description = content
  364. item.label = ''
  365. item.revision_type = ActionDescription.COMMENT
  366. if do_save:
  367. self.save(item, ActionDescription.COMMENT)
  368. return item
  369. def get_one_from_revision(self, content_id: int, content_type: str, workspace: Workspace=None, revision_id=None) -> Content:
  370. """
  371. This method is a hack to convert a node revision item into a node
  372. :param content_id:
  373. :param content_type:
  374. :param workspace:
  375. :param revision_id:
  376. :return:
  377. """
  378. content = self.get_one(content_id, content_type, workspace)
  379. revision = self._session.query(ContentRevisionRO).filter(ContentRevisionRO.revision_id==revision_id).one()
  380. if revision.content_id==content.content_id:
  381. content.revision_to_serialize = revision.revision_id
  382. else:
  383. raise ValueError('Revision not found for given content')
  384. return content
  385. def get_one(self, content_id: int, content_type: str, workspace: Workspace=None, parent: Content=None) -> Content:
  386. if not content_id:
  387. return None
  388. base_request = self._base_query(workspace).filter(Content.content_id==content_id)
  389. if content_type!=ContentType.Any:
  390. base_request = base_request.filter(Content.type==content_type)
  391. if parent:
  392. base_request = base_request.filter(Content.parent_id==parent.content_id) # nopep8
  393. try:
  394. content = base_request.one()
  395. except NoResultFound as exc:
  396. raise ContentNotFound('Content "{}" not found in database'.format(content_id)) from exc # nopep8
  397. return content
  398. def get_one_revision(self, revision_id: int = None) -> ContentRevisionRO:
  399. """
  400. This method allow us to get directly any revision with its id
  401. :param revision_id: The content's revision's id that we want to return
  402. :return: An item Content linked with the correct revision
  403. """
  404. assert revision_id is not None# DYN_REMOVE
  405. revision = self._session.query(ContentRevisionRO).filter(ContentRevisionRO.revision_id == revision_id).one()
  406. return revision
  407. # INFO - A.P - 2017-07-03 - python file object getter
  408. # in case of we cook a version of preview manager that allows a pythonic
  409. # access to files
  410. # def get_one_revision_file(self, revision_id: int = None):
  411. # """
  412. # This function allows us to directly get a Python file object from its
  413. # revision identifier.
  414. # :param revision_id: The revision id of the file we want to return
  415. # :return: The corresponding Python file object
  416. # """
  417. # revision = self.get_one_revision(revision_id)
  418. # return DepotManager.get().get(revision.depot_file)
  419. def get_one_revision_filepath(self, revision_id: int = None) -> str:
  420. """
  421. This method allows us to directly get a file path from its revision
  422. identifier.
  423. :param revision_id: The revision id of the filepath we want to return
  424. :return: The corresponding filepath
  425. """
  426. revision = self.get_one_revision(revision_id)
  427. depot = DepotManager.get()
  428. depot_stored_file = depot.get(revision.depot_file) # type: StoredFile
  429. depot_file_path = depot_stored_file._file_path # type: str
  430. return depot_file_path
  431. def get_one_by_label_and_parent(
  432. self,
  433. content_label: str,
  434. content_parent: Content=None,
  435. ) -> Content:
  436. """
  437. This method let us request the database to obtain a Content with its name and parent
  438. :param content_label: Either the content's label or the content's filename if the label is None
  439. :param content_parent: The parent's content
  440. :param workspace: The workspace's content
  441. :return The corresponding Content
  442. """
  443. workspace = content_parent.workspace if content_parent else None
  444. query = self._base_query(workspace)
  445. parent_id = content_parent.content_id if content_parent else None
  446. query = query.filter(Content.parent_id == parent_id)
  447. file_name, file_extension = os.path.splitext(content_label)
  448. return query.filter(
  449. or_(
  450. and_(
  451. Content.type == ContentType.File,
  452. Content.label == file_name,
  453. Content.file_extension == file_extension,
  454. ),
  455. and_(
  456. Content.type == ContentType.Thread,
  457. Content.label == file_name,
  458. ),
  459. and_(
  460. Content.type == ContentType.Page,
  461. Content.label == file_name,
  462. ),
  463. and_(
  464. Content.type == ContentType.Folder,
  465. Content.label == content_label,
  466. ),
  467. )
  468. ).one()
  469. def get_one_by_label_and_parent_labels(
  470. self,
  471. content_label: str,
  472. workspace: Workspace,
  473. content_parent_labels: [str]=None,
  474. ):
  475. """
  476. Return content with it's label, workspace and parents labels (optional)
  477. :param content_label: label of content (label or file_name)
  478. :param workspace: workspace containing all of this
  479. :param content_parent_labels: Ordered list of labels representing path
  480. of folder (without workspace label).
  481. E.g.: ['foo', 'bar'] for complete path /Workspace1/foo/bar folder
  482. :return: Found Content
  483. """
  484. query = self._base_query(workspace)
  485. parent_folder = None
  486. # Grab content parent folder if parent path given
  487. if content_parent_labels:
  488. parent_folder = self.get_folder_with_workspace_path_labels(
  489. content_parent_labels,
  490. workspace,
  491. )
  492. # Build query for found content by label
  493. content_query = self.filter_query_for_content_label_as_path(
  494. query=query,
  495. content_label_as_file=content_label,
  496. )
  497. # Modify query to apply parent folder filter if any
  498. if parent_folder:
  499. content_query = content_query.filter(
  500. Content.parent_id == parent_folder.content_id,
  501. )
  502. else:
  503. content_query = content_query.filter(
  504. Content.parent_id == None,
  505. )
  506. # Filter with workspace
  507. content_query = content_query.filter(
  508. Content.workspace_id == workspace.workspace_id,
  509. )
  510. # Return the content
  511. return content_query\
  512. .order_by(
  513. Content.revision_id.desc(),
  514. )\
  515. .one()
  516. def get_folder_with_workspace_path_labels(
  517. self,
  518. path_labels: [str],
  519. workspace: Workspace,
  520. ) -> Content:
  521. """
  522. Return a Content folder for given relative path.
  523. TODO BS 20161124: Not safe if web interface allow folder duplicate names
  524. :param path_labels: List of labels representing path of folder
  525. (without workspace label).
  526. E.g.: ['foo', 'bar'] for complete path /Workspace1/foo/bar folder
  527. :param workspace: workspace of folders
  528. :return: Content folder
  529. """
  530. query = self._base_query(workspace)
  531. folder = None
  532. for label in path_labels:
  533. # Filter query on label
  534. folder_query = query \
  535. .filter(
  536. Content.type == ContentType.Folder,
  537. Content.label == label,
  538. Content.workspace_id == workspace.workspace_id,
  539. )
  540. # Search into parent folder (if already deep)
  541. if folder:
  542. folder_query = folder_query\
  543. .filter(
  544. Content.parent_id == folder.content_id,
  545. )
  546. else:
  547. folder_query = folder_query \
  548. .filter(Content.parent_id == None)
  549. # Get thirst corresponding folder
  550. folder = folder_query \
  551. .order_by(Content.revision_id.desc()) \
  552. .one()
  553. return folder
  554. def filter_query_for_content_label_as_path(
  555. self,
  556. query: Query,
  557. content_label_as_file: str,
  558. is_case_sensitive: bool = False,
  559. ) -> Query:
  560. """
  561. Apply normalised filters to found Content corresponding as given label.
  562. :param query: query to modify
  563. :param content_label_as_file: label in this
  564. FILE version, use Content.get_label_as_file().
  565. :param is_case_sensitive: Take care about case or not
  566. :return: modified query
  567. """
  568. file_name, file_extension = os.path.splitext(content_label_as_file)
  569. label_filter = Content.label == content_label_as_file
  570. file_name_filter = Content.label == file_name
  571. file_extension_filter = Content.file_extension == file_extension
  572. if not is_case_sensitive:
  573. label_filter = func.lower(Content.label) == \
  574. func.lower(content_label_as_file)
  575. file_name_filter = func.lower(Content.label) == \
  576. func.lower(file_name)
  577. file_extension_filter = func.lower(Content.file_extension) == \
  578. func.lower(file_extension)
  579. return query.filter(or_(
  580. and_(
  581. Content.type == ContentType.File,
  582. file_name_filter,
  583. file_extension_filter,
  584. ),
  585. and_(
  586. Content.type == ContentType.Thread,
  587. file_name_filter,
  588. file_extension_filter,
  589. ),
  590. and_(
  591. Content.type == ContentType.Page,
  592. file_name_filter,
  593. file_extension_filter,
  594. ),
  595. and_(
  596. Content.type == ContentType.Folder,
  597. label_filter,
  598. ),
  599. ))
  600. def get_all(self, parent_id: int=None, content_type: str=ContentType.Any, workspace: Workspace=None) -> typing.List[Content]:
  601. assert parent_id is None or isinstance(parent_id, int) # DYN_REMOVE
  602. assert content_type is not None# DYN_REMOVE
  603. assert isinstance(content_type, str) # DYN_REMOVE
  604. resultset = self._base_query(workspace)
  605. if content_type!=ContentType.Any:
  606. resultset = resultset.filter(Content.type==content_type)
  607. if parent_id:
  608. resultset = resultset.filter(Content.parent_id==parent_id)
  609. if parent_id == 0 or parent_id is False:
  610. resultset = resultset.filter(Content.parent_id == None)
  611. # parent_id == None give all contents
  612. return resultset.all()
  613. def get_children(self, parent_id: int, content_types: list, workspace: Workspace=None) -> typing.List[Content]:
  614. """
  615. Return parent_id childs of given content_types
  616. :param parent_id: parent id
  617. :param content_types: list of types
  618. :param workspace: workspace filter
  619. :return: list of content
  620. """
  621. resultset = self._base_query(workspace)
  622. resultset = resultset.filter(Content.type.in_(content_types))
  623. if parent_id:
  624. resultset = resultset.filter(Content.parent_id==parent_id)
  625. if parent_id is False:
  626. resultset = resultset.filter(Content.parent_id == None)
  627. return resultset.all()
  628. # TODO find an other name to filter on is_deleted / is_archived
  629. def get_all_with_filter(self, parent_id: int=None, content_type: str=ContentType.Any, workspace: Workspace=None) -> typing.List[Content]:
  630. assert parent_id is None or isinstance(parent_id, int) # DYN_REMOVE
  631. assert content_type is not None# DYN_REMOVE
  632. assert isinstance(content_type, str) # DYN_REMOVE
  633. resultset = self._base_query(workspace)
  634. if content_type != ContentType.Any:
  635. resultset = resultset.filter(Content.type==content_type)
  636. resultset = resultset.filter(Content.is_deleted == self._show_deleted)
  637. resultset = resultset.filter(Content.is_archived == self._show_archived)
  638. resultset = resultset.filter(Content.is_temporary == self._show_temporary)
  639. resultset = resultset.filter(Content.parent_id==parent_id)
  640. return resultset.all()
  641. def get_all_without_exception(self, content_type: str, workspace: Workspace=None) -> typing.List[Content]:
  642. assert content_type is not None# DYN_REMOVE
  643. resultset = self._base_query(workspace)
  644. if content_type != ContentType.Any:
  645. resultset = resultset.filter(Content.type==content_type)
  646. return resultset.all()
  647. def get_last_active(self, parent_id: int, content_type: str, workspace: Workspace=None, limit=10) -> typing.List[Content]:
  648. assert parent_id is None or isinstance(parent_id, int) # DYN_REMOVE
  649. assert content_type is not None# DYN_REMOVE
  650. assert isinstance(content_type, str) # DYN_REMOVE
  651. resultset = self._base_query(workspace) \
  652. .filter(Content.workspace_id == Workspace.workspace_id) \
  653. .filter(Workspace.is_deleted.is_(False)) \
  654. .order_by(desc(Content.updated))
  655. if content_type!=ContentType.Any:
  656. resultset = resultset.filter(Content.type==content_type)
  657. if parent_id:
  658. resultset = resultset.filter(Content.parent_id==parent_id)
  659. result = []
  660. for item in resultset:
  661. new_item = None
  662. if ContentType.Comment == item.type:
  663. new_item = item.parent
  664. else:
  665. new_item = item
  666. # INFO - D.A. - 2015-05-20
  667. # We do not want to show only one item if the last 10 items are
  668. # comments about one thread for example
  669. if new_item not in result:
  670. result.append(new_item)
  671. if len(result) >= limit:
  672. break
  673. return result
  674. def get_last_unread(self, parent_id: int, content_type: str,
  675. workspace: Workspace=None, limit=10) -> typing.List[Content]:
  676. assert parent_id is None or isinstance(parent_id, int) # DYN_REMOVE
  677. assert content_type is not None# DYN_REMOVE
  678. assert isinstance(content_type, str) # DYN_REMOVE
  679. read_revision_ids = self._session.query(RevisionReadStatus.revision_id) \
  680. .filter(RevisionReadStatus.user_id==self._user_id)
  681. not_read_revisions = self._revisions_base_query(workspace) \
  682. .filter(~ContentRevisionRO.revision_id.in_(read_revision_ids)) \
  683. .filter(ContentRevisionRO.workspace_id == Workspace.workspace_id) \
  684. .filter(Workspace.is_deleted.is_(False)) \
  685. .subquery()
  686. not_read_content_ids_query = self._session.query(
  687. distinct(not_read_revisions.c.content_id)
  688. )
  689. not_read_content_ids = list(map(
  690. itemgetter(0),
  691. not_read_content_ids_query,
  692. ))
  693. not_read_contents = self._base_query(workspace) \
  694. .filter(Content.content_id.in_(not_read_content_ids)) \
  695. .order_by(desc(Content.updated))
  696. if content_type != ContentType.Any:
  697. not_read_contents = not_read_contents.filter(
  698. Content.type==content_type)
  699. else:
  700. not_read_contents = not_read_contents.filter(
  701. Content.type!=ContentType.Folder)
  702. if parent_id:
  703. not_read_contents = not_read_contents.filter(
  704. Content.parent_id==parent_id)
  705. result = []
  706. for item in not_read_contents:
  707. new_item = None
  708. if ContentType.Comment == item.type:
  709. new_item = item.parent
  710. else:
  711. new_item = item
  712. # INFO - D.A. - 2015-05-20
  713. # We do not want to show only one item if the last 10 items are
  714. # comments about one thread for example
  715. if new_item not in result:
  716. result.append(new_item)
  717. if len(result) >= limit:
  718. break
  719. return result
  720. def set_allowed_content(self, folder: Content, allowed_content_dict:dict):
  721. """
  722. :param folder: the given folder instance
  723. :param allowed_content_dict: must be something like this:
  724. dict(
  725. folder = True
  726. thread = True,
  727. file = False,
  728. page = True
  729. )
  730. :return:
  731. """
  732. properties = dict(allowed_content = allowed_content_dict)
  733. folder.properties = properties
  734. def set_status(self, content: Content, new_status: str):
  735. if new_status in ContentStatus.allowed_values():
  736. content.status = new_status
  737. content.revision_type = ActionDescription.STATUS_UPDATE
  738. else:
  739. raise ValueError('The given value {} is not allowed'.format(new_status))
  740. def move(self,
  741. item: Content,
  742. new_parent: Content,
  743. must_stay_in_same_workspace: bool=True,
  744. new_workspace: Workspace=None,
  745. ):
  746. if must_stay_in_same_workspace:
  747. if new_parent and new_parent.workspace_id != item.workspace_id:
  748. raise ValueError('the item should stay in the same workspace')
  749. item.parent = new_parent
  750. if new_workspace:
  751. item.workspace = new_workspace
  752. if new_parent and \
  753. new_parent.workspace_id != new_workspace.workspace_id:
  754. raise WorkspacesDoNotMatch(
  755. 'new parent workspace and new workspace should be the same.'
  756. )
  757. else:
  758. if new_parent:
  759. item.workspace = new_parent.workspace
  760. item.revision_type = ActionDescription.MOVE
  761. def copy(
  762. self,
  763. item: Content,
  764. new_parent: Content=None,
  765. new_label: str=None,
  766. do_save: bool=True,
  767. do_notify: bool=True,
  768. ) -> Content:
  769. """
  770. Copy nearly all content, revision included. Children not included, see
  771. "copy_children" for this.
  772. :param item: Item to copy
  773. :param new_parent: new parent of the new copied item
  774. :param new_label: new label of the new copied item
  775. :param do_notify: notify copy or not
  776. :return: Newly copied item
  777. """
  778. if (not new_parent and not new_label) or (new_parent == item.parent and new_label == item.label): # nopep8
  779. # TODO - G.M - 08-03-2018 - Use something else than value error
  780. raise ValueError("You can't copy file into itself")
  781. if new_parent:
  782. workspace = new_parent.workspace
  783. parent = new_parent
  784. else:
  785. workspace = item.workspace
  786. parent = item.parent
  787. label = new_label or item.label
  788. content = item.copy(parent)
  789. # INFO - GM - 15-03-2018 - add "copy" revision
  790. with new_revision(
  791. session=self._session,
  792. tm=transaction.manager,
  793. content=content,
  794. force_create_new_revision=True
  795. ) as rev:
  796. rev.parent = parent
  797. rev.workspace = workspace
  798. rev.label = label
  799. rev.revision_type = ActionDescription.COPY
  800. rev.properties['origin'] = {
  801. 'content': item.id,
  802. 'revision': item.last_revision.revision_id,
  803. }
  804. if do_save:
  805. self.save(content, ActionDescription.COPY, do_notify=do_notify)
  806. return content
  807. def copy_children(self, origin_content: Content, new_content: Content):
  808. for child in origin_content.children:
  809. self.copy(child, new_content)
  810. def move_recursively(self, item: Content,
  811. new_parent: Content, new_workspace: Workspace):
  812. self.move(item, new_parent, False, new_workspace)
  813. self.save(item, do_notify=False)
  814. for child in item.children:
  815. with new_revision(child):
  816. self.move_recursively(child, item, new_workspace)
  817. return
  818. def update_content(self, item: Content, new_label: str, new_content: str=None) -> Content:
  819. if item.label==new_label and item.description==new_content:
  820. # TODO - G.M - 20-03-2018 - Fix internatization for webdav access.
  821. # Internatization disabled in libcontent for now.
  822. raise SameValueError('The content did not changed')
  823. item.owner = self._user
  824. item.label = new_label
  825. item.description = new_content if new_content else item.description # TODO: convert urls into links
  826. item.revision_type = ActionDescription.EDITION
  827. return item
  828. def update_file_data(self, item: Content, new_filename: str, new_mimetype: str, new_content: bytes) -> Content:
  829. if new_mimetype == item.file_mimetype and \
  830. new_content == item.depot_file.file.read():
  831. raise SameValueError('The content did not changed')
  832. item.owner = self._user
  833. item.file_name = new_filename
  834. item.file_mimetype = new_mimetype
  835. item.depot_file = FileIntent(
  836. new_content,
  837. new_filename,
  838. new_mimetype,
  839. )
  840. item.revision_type = ActionDescription.REVISION
  841. return item
  842. def archive(self, content: Content):
  843. content.owner = self._user
  844. content.is_archived = True
  845. # TODO - G.M - 12-03-2018 - Inspect possible label conflict problem
  846. # INFO - G.M - 12-03-2018 - Set label name to avoid trouble when
  847. # un-archiving file.
  848. content.label = '{label}-{action}-{date}'.format(
  849. label=content.label,
  850. action='archived',
  851. date=current_date_for_filename()
  852. )
  853. content.revision_type = ActionDescription.ARCHIVING
  854. def unarchive(self, content: Content):
  855. content.owner = self._user
  856. content.is_archived = False
  857. content.revision_type = ActionDescription.UNARCHIVING
  858. def delete(self, content: Content):
  859. content.owner = self._user
  860. content.is_deleted = True
  861. # TODO - G.M - 12-03-2018 - Inspect possible label conflict problem
  862. # INFO - G.M - 12-03-2018 - Set label name to avoid trouble when
  863. # un-deleting file.
  864. content.label = '{label}-{action}-{date}'.format(
  865. label=content.label,
  866. action='deleted',
  867. date=current_date_for_filename()
  868. )
  869. content.revision_type = ActionDescription.DELETION
  870. def undelete(self, content: Content):
  871. content.owner = self._user
  872. content.is_deleted = False
  873. content.revision_type = ActionDescription.UNDELETION
  874. def mark_read__all(self,
  875. read_datetime: datetime=None,
  876. do_flush: bool=True,
  877. recursive: bool=True
  878. ):
  879. itemset = self.get_last_unread(None, ContentType.Any)
  880. for item in itemset:
  881. self.mark_read(item, read_datetime, do_flush, recursive)
  882. def mark_read__workspace(self,
  883. workspace : Workspace,
  884. read_datetime: datetime=None,
  885. do_flush: bool=True,
  886. recursive: bool=True
  887. ):
  888. itemset = self.get_last_unread(None, ContentType.Any, workspace)
  889. for item in itemset:
  890. self.mark_read(item, read_datetime, do_flush, recursive)
  891. def mark_read(self, content: Content,
  892. read_datetime: datetime=None,
  893. do_flush: bool=True, recursive: bool=True) -> Content:
  894. assert self._user
  895. assert content
  896. # The algorithm is:
  897. # 1. define the read datetime
  898. # 2. update all revisions related to current Content
  899. # 3. do the same for all child revisions
  900. # (ie parent_id is content_id of current content)
  901. if not read_datetime:
  902. read_datetime = datetime.datetime.now()
  903. viewed_revisions = self._session.query(ContentRevisionRO) \
  904. .filter(ContentRevisionRO.content_id==content.content_id).all()
  905. for revision in viewed_revisions:
  906. revision.read_by[self._user] = read_datetime
  907. if recursive:
  908. # mark read :
  909. # - all children
  910. # - parent stuff (if you mark a comment as read,
  911. # then you have seen the parent)
  912. # - parent comments
  913. for child in content.get_valid_children():
  914. self.mark_read(child, read_datetime=read_datetime,
  915. do_flush=False)
  916. if ContentType.Comment == content.type:
  917. self.mark_read(content.parent, read_datetime=read_datetime,
  918. do_flush=False, recursive=False)
  919. for comment in content.parent.get_comments():
  920. if comment != content:
  921. self.mark_read(comment, read_datetime=read_datetime,
  922. do_flush=False, recursive=False)
  923. if do_flush:
  924. self.flush()
  925. return content
  926. def mark_unread(self, content: Content, do_flush=True) -> Content:
  927. assert self._user
  928. assert content
  929. revisions = self._session.query(ContentRevisionRO) \
  930. .filter(ContentRevisionRO.content_id==content.content_id).all()
  931. for revision in revisions:
  932. del revision.read_by[self._user]
  933. for child in content.get_valid_children():
  934. self.mark_unread(child, do_flush=False)
  935. if do_flush:
  936. self.flush()
  937. return content
  938. def flush(self):
  939. self._session.flush()
  940. def save(self, content: Content, action_description: str=None, do_flush=True, do_notify=True):
  941. """
  942. Save an object, flush the session and set the revision_type property
  943. :param content:
  944. :param action_description:
  945. :return:
  946. """
  947. assert action_description is None or action_description in ActionDescription.allowed_values()
  948. if not action_description:
  949. # See if the last action has been modified
  950. if content.revision_type==None or len(get_history(content.revision, 'revision_type'))<=0:
  951. # The action has not been modified, so we set it to default edition
  952. action_description = ActionDescription.EDITION
  953. if action_description:
  954. content.revision_type = action_description
  955. if do_flush:
  956. # INFO - 2015-09-03 - D.A.
  957. # There are 2 flush because of the use
  958. # of triggers for content creation
  959. #
  960. # (when creating a content, actually this is an insert of a new
  961. # revision in content_revisions ; so the mark_read operation need
  962. # to get full real data from database before to be prepared.
  963. self._session.add(content)
  964. self._session.flush()
  965. # TODO - 2015-09-03 - D.A. - Do not use triggers
  966. # We should create a new ContentRevisionRO object instead of Content
  967. # This would help managing view/not viewed status
  968. self.mark_read(content, do_flush=True)
  969. if do_notify:
  970. self.do_notify(content)
  971. def do_notify(self, content: Content):
  972. """
  973. Allow to force notification for a given content. By default, it is
  974. called during the .save() operation
  975. :param content:
  976. :return:
  977. """
  978. NotifierFactory.create(
  979. config=self._config,
  980. current_user=self._user,
  981. session=self._session,
  982. ).notify_content_update(content)
  983. def get_keywords(self, search_string, search_string_separators=None) -> [str]:
  984. """
  985. :param search_string: a list of coma-separated keywords
  986. :return: a list of str (each keyword = 1 entry
  987. """
  988. search_string_separators = search_string_separators or ContentApi.SEARCH_SEPARATORS
  989. keywords = []
  990. if search_string:
  991. keywords = [keyword.strip() for keyword in re.split(search_string_separators, search_string)]
  992. return keywords
  993. def search(self, keywords: [str]) -> Query:
  994. """
  995. :return: a sorted list of Content items
  996. """
  997. if len(keywords)<=0:
  998. return None
  999. filter_group_label = list(Content.label.ilike('%{}%'.format(keyword)) for keyword in keywords)
  1000. filter_group_desc = list(Content.description.ilike('%{}%'.format(keyword)) for keyword in keywords)
  1001. title_keyworded_items = self._hard_filtered_base_query().\
  1002. filter(or_(*(filter_group_label+filter_group_desc))).\
  1003. options(joinedload('children_revisions')).\
  1004. options(joinedload('parent'))
  1005. return title_keyworded_items
  1006. def get_all_types(self) -> typing.List[ContentType]:
  1007. labels = ContentType.all()
  1008. content_types = []
  1009. for label in labels:
  1010. content_types.append(ContentType(label))
  1011. return ContentType.sorted(content_types)
  1012. def exclude_unavailable(
  1013. self,
  1014. contents: typing.List[Content],
  1015. ) -> typing.List[Content]:
  1016. """
  1017. Update and return list with content under archived/deleted removed.
  1018. :param contents: List of contents to parse
  1019. """
  1020. for content in contents[:]:
  1021. if self.content_under_deleted(content) or self.content_under_archived(content):
  1022. contents.remove(content)
  1023. return contents
  1024. def content_under_deleted(self, content: Content) -> bool:
  1025. if content.parent:
  1026. if content.parent.is_deleted:
  1027. return True
  1028. if content.parent.parent:
  1029. return self.content_under_deleted(content.parent)
  1030. return False
  1031. def content_under_archived(self, content: Content) -> bool:
  1032. if content.parent:
  1033. if content.parent.is_archived:
  1034. return True
  1035. if content.parent.parent:
  1036. return self.content_under_archived(content.parent)
  1037. return False
  1038. def find_one_by_unique_property(
  1039. self,
  1040. property_name: str,
  1041. property_value: str,
  1042. workspace: Workspace=None,
  1043. ) -> Content:
  1044. """
  1045. Return Content who contains given property.
  1046. Raise sqlalchemy.orm.exc.MultipleResultsFound if more than one Content
  1047. contains this property value.
  1048. :param property_name: Name of property
  1049. :param property_value: Value of property
  1050. :param workspace: Workspace who contains Content
  1051. :return: Found Content
  1052. """
  1053. # TODO - 20160602 - Bastien: Should be JSON type query
  1054. # see https://www.compose.io/articles/using-json-extensions-in-\
  1055. # postgresql-from-python-2/
  1056. query = self._base_query(workspace=workspace).filter(
  1057. Content._properties.like(
  1058. '%"{property_name}": "{property_value}"%'.format(
  1059. property_name=property_name,
  1060. property_value=property_value,
  1061. )
  1062. )
  1063. )
  1064. return query.one()
  1065. def generate_folder_label(
  1066. self,
  1067. workspace: Workspace,
  1068. parent: Content=None,
  1069. ) -> str:
  1070. """
  1071. Generate a folder label
  1072. :param workspace: Future folder workspace
  1073. :param parent: Parent of foture folder (can be None)
  1074. :return: Generated folder name
  1075. """
  1076. query = self._base_query(workspace=workspace)\
  1077. .filter(Content.label.ilike('{0}%'.format(
  1078. _('New folder'),
  1079. )))
  1080. if parent:
  1081. query = query.filter(Content.parent == parent)
  1082. return _('New folder {0}').format(
  1083. query.count() + 1,
  1084. )