content.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # -*- coding: utf-8 -*-
  2. from depot.io.utils import FileIntent
  3. import transaction
  4. from tracim_backend import models
  5. from tracim_backend.fixtures import Fixture
  6. from tracim_backend.fixtures.users_and_groups import Test
  7. from tracim_backend.lib.core.content import ContentApi
  8. from tracim_backend.lib.core.userworkspace import RoleApi
  9. from tracim_backend.lib.core.workspace import WorkspaceApi
  10. from tracim_backend.models.data import ContentType
  11. from tracim_backend.models.data import UserRoleInWorkspace
  12. from tracim_backend.models.revision_protection import new_revision
  13. class Content(Fixture):
  14. require = [Test]
  15. def insert(self):
  16. admin = self._session.query(models.User) \
  17. .filter(models.User.email == 'admin@admin.admin') \
  18. .one()
  19. bob = self._session.query(models.User) \
  20. .filter(models.User.email == 'bob@fsf.local') \
  21. .one()
  22. john_the_reader = self._session.query(models.User) \
  23. .filter(models.User.email == 'john-the-reader@reader.local') \
  24. .one()
  25. admin_workspace_api = WorkspaceApi(
  26. current_user=admin,
  27. session=self._session,
  28. config=self._config,
  29. )
  30. bob_workspace_api = WorkspaceApi(
  31. current_user=bob,
  32. session=self._session,
  33. config=self._config
  34. )
  35. content_api = ContentApi(
  36. current_user=admin,
  37. session=self._session,
  38. config=self._config
  39. )
  40. bob_content_api = ContentApi(
  41. current_user=bob,
  42. session=self._session,
  43. config=self._config
  44. )
  45. reader_content_api = ContentApi(
  46. current_user=john_the_reader,
  47. session=self._session,
  48. config=self._config
  49. )
  50. role_api = RoleApi(
  51. current_user=admin,
  52. session=self._session,
  53. config=self._config,
  54. )
  55. # Workspaces
  56. business_workspace = admin_workspace_api.create_workspace(
  57. 'Business',
  58. description='All importants documents',
  59. save_now=True,
  60. )
  61. recipe_workspace = admin_workspace_api.create_workspace(
  62. 'Recipes',
  63. description='Our best recipes',
  64. save_now=True,
  65. )
  66. other_workspace = bob_workspace_api.create_workspace(
  67. 'Others',
  68. description='Other Workspace',
  69. save_now=True,
  70. )
  71. # Workspaces roles
  72. role_api.create_one(
  73. user=bob,
  74. workspace=recipe_workspace,
  75. role_level=UserRoleInWorkspace.CONTENT_MANAGER,
  76. with_notif=False,
  77. )
  78. role_api.create_one(
  79. user=john_the_reader,
  80. workspace=recipe_workspace,
  81. role_level=UserRoleInWorkspace.READER,
  82. with_notif=False,
  83. )
  84. # Folders
  85. tool_workspace = content_api.create(
  86. content_type=ContentType.Folder,
  87. workspace=business_workspace,
  88. label='Tools',
  89. do_save=True,
  90. do_notify=False,
  91. )
  92. menu_workspace = content_api.create(
  93. content_type=ContentType.Folder,
  94. workspace=business_workspace,
  95. label='Menus',
  96. do_save=True,
  97. do_notify=False,
  98. )
  99. dessert_folder = content_api.create(
  100. content_type=ContentType.Folder,
  101. workspace=recipe_workspace,
  102. label='Desserts',
  103. do_save=True,
  104. do_notify=False,
  105. )
  106. salads_folder = content_api.create(
  107. content_type=ContentType.Folder,
  108. workspace=recipe_workspace,
  109. label='Salads',
  110. do_save=True,
  111. do_notify=False,
  112. )
  113. other_folder = content_api.create(
  114. content_type=ContentType.Folder,
  115. workspace=other_workspace,
  116. label='Infos',
  117. do_save=True,
  118. do_notify=False,
  119. )
  120. # Pages, threads, ..
  121. tiramisu_page = content_api.create(
  122. content_type=ContentType.Page,
  123. workspace=recipe_workspace,
  124. parent=dessert_folder,
  125. label='Tiramisu Recipes!!!',
  126. do_save=True,
  127. do_notify=False,
  128. )
  129. with new_revision(
  130. session=self._session,
  131. tm=transaction.manager,
  132. content=tiramisu_page,
  133. ):
  134. content_api.update_content(
  135. item=tiramisu_page,
  136. new_content='<p>To cook a greet Tiramisu, you need many ingredients.</p>', # nopep8
  137. new_label='Tiramisu Recipes!!!',
  138. )
  139. content_api.save(tiramisu_page)
  140. best_cake_thread = content_api.create(
  141. content_type=ContentType.Thread,
  142. workspace=recipe_workspace,
  143. parent=dessert_folder,
  144. label='Best Cake',
  145. do_save=False,
  146. do_notify=False,
  147. )
  148. best_cake_thread.description = 'Which is the best cake?'
  149. self._session.add(best_cake_thread)
  150. apple_pie_recipe = content_api.create(
  151. content_type=ContentType.File,
  152. workspace=recipe_workspace,
  153. parent=dessert_folder,
  154. label='Apple_Pie',
  155. do_save=False,
  156. do_notify=False,
  157. )
  158. apple_pie_recipe.file_extension = '.txt'
  159. apple_pie_recipe.depot_file = FileIntent(
  160. b'Apple pie Recipe',
  161. 'apple_Pie.txt',
  162. 'text/plain',
  163. )
  164. self._session.add(apple_pie_recipe)
  165. Brownie_recipe = content_api.create(
  166. content_type=ContentType.File,
  167. workspace=recipe_workspace,
  168. parent=dessert_folder,
  169. label='Brownie Recipe',
  170. do_save=False,
  171. do_notify=False,
  172. )
  173. Brownie_recipe.file_extension = '.html'
  174. Brownie_recipe.depot_file = FileIntent(
  175. b'<p>Brownie Recipe</p>',
  176. 'brownie_recipe.html',
  177. 'text/html',
  178. )
  179. self._session.add(Brownie_recipe)
  180. fruits_desserts_folder = content_api.create(
  181. content_type=ContentType.Folder,
  182. workspace=recipe_workspace,
  183. label='Fruits Desserts',
  184. parent=dessert_folder,
  185. do_save=True,
  186. )
  187. menu_page = content_api.create(
  188. content_type=ContentType.Page,
  189. workspace=business_workspace,
  190. parent=menu_workspace,
  191. label='Current Menu',
  192. do_save=True,
  193. )
  194. new_fruit_salad = content_api.create(
  195. content_type=ContentType.Page,
  196. workspace=recipe_workspace,
  197. parent=fruits_desserts_folder,
  198. label='New Fruit Salad',
  199. do_save=True,
  200. )
  201. old_fruit_salad = content_api.create(
  202. content_type=ContentType.Page,
  203. workspace=recipe_workspace,
  204. parent=fruits_desserts_folder,
  205. label='Fruit Salad',
  206. do_save=True,
  207. do_notify=False,
  208. )
  209. with new_revision(
  210. session=self._session,
  211. tm=transaction.manager,
  212. content=old_fruit_salad,
  213. ):
  214. content_api.archive(old_fruit_salad)
  215. content_api.save(old_fruit_salad)
  216. bad_fruit_salad = content_api.create(
  217. content_type=ContentType.Page,
  218. workspace=recipe_workspace,
  219. parent=fruits_desserts_folder,
  220. label='Bad Fruit Salad',
  221. do_save=True,
  222. do_notify=False,
  223. )
  224. with new_revision(
  225. session=self._session,
  226. tm=transaction.manager,
  227. content=bad_fruit_salad,
  228. ):
  229. content_api.delete(bad_fruit_salad)
  230. content_api.save(bad_fruit_salad)
  231. # File at the root for test
  232. new_fruit_salad = content_api.create(
  233. content_type=ContentType.Page,
  234. workspace=other_workspace,
  235. label='New Fruit Salad',
  236. do_save=True,
  237. )
  238. old_fruit_salad = content_api.create(
  239. content_type=ContentType.Page,
  240. workspace=other_workspace,
  241. label='Fruit Salad',
  242. do_save=True,
  243. )
  244. with new_revision(
  245. session=self._session,
  246. tm=transaction.manager,
  247. content=old_fruit_salad,
  248. ):
  249. content_api.archive(old_fruit_salad)
  250. content_api.save(old_fruit_salad)
  251. bad_fruit_salad = content_api.create(
  252. content_type=ContentType.Page,
  253. workspace=other_workspace,
  254. label='Bad Fruit Salad',
  255. do_save=True,
  256. )
  257. with new_revision(
  258. session=self._session,
  259. tm=transaction.manager,
  260. content=bad_fruit_salad,
  261. ):
  262. content_api.delete(bad_fruit_salad)
  263. content_api.save(bad_fruit_salad)
  264. content_api.create_comment(
  265. parent=best_cake_thread,
  266. content='<p>What is for you the best cake ever? </br> I personnally vote for Chocolate cupcake!</p>', # nopep8
  267. do_save=True,
  268. )
  269. bob_content_api.create_comment(
  270. parent=best_cake_thread,
  271. content='<p>What about Apple Pie? There are Awesome!</p>',
  272. do_save=True,
  273. )
  274. reader_content_api.create_comment(
  275. parent=best_cake_thread,
  276. content='<p>You are right, but Kouign-amann are clearly better.</p>',
  277. do_save=True,
  278. )
  279. with new_revision(
  280. session=self._session,
  281. tm=transaction.manager,
  282. content=best_cake_thread,
  283. ):
  284. bob_content_api.update_content(
  285. item=best_cake_thread,
  286. new_content='What is the best cake?',
  287. new_label='Best Cakes?',
  288. )
  289. bob_content_api.save(best_cake_thread)
  290. with new_revision(
  291. session=self._session,
  292. tm=transaction.manager,
  293. content=tiramisu_page,
  294. ):
  295. bob_content_api.update_content(
  296. item=tiramisu_page,
  297. new_content='<p>To cook a great Tiramisu, you need many ingredients.</p>', # nopep8
  298. new_label='Tiramisu Recipe',
  299. )
  300. bob_content_api.save(tiramisu_page)
  301. self._session.flush()