content.py 45KB

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