content.py 8.9KB

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