content.py 46KB

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