content.py 46KB

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