Browse Source

adapt set_allowed_content for folder endpoints

Guénaël Muller 6 years ago
parent
commit
53c8231294

+ 35 - 6
backend/tracim_backend/lib/core/content.py View File

25
 
25
 
26
 from tracim_backend.lib.utils.utils import cmp_to_key
26
 from tracim_backend.lib.utils.utils import cmp_to_key
27
 from tracim_backend.lib.core.notifications import NotifierFactory
27
 from tracim_backend.lib.core.notifications import NotifierFactory
28
-from tracim_backend.exceptions import SameValueError, UnallowedSubContent
28
+from tracim_backend.exceptions import SameValueError
29
+from tracim_backend.exceptions import UnallowedSubContent
30
+from tracim_backend.exceptions import ContentTypeNotExist
29
 from tracim_backend.exceptions import PageOfPreviewNotFound
31
 from tracim_backend.exceptions import PageOfPreviewNotFound
30
 from tracim_backend.exceptions import PreviewDimNotAllowed
32
 from tracim_backend.exceptions import PreviewDimNotAllowed
31
 from tracim_backend.exceptions import RevisionDoesNotMatchThisContent
33
 from tracim_backend.exceptions import RevisionDoesNotMatchThisContent
1135
     #
1137
     #
1136
     #     return result
1138
     #     return result
1137
 
1139
 
1138
-    def set_allowed_content(self, folder: Content, allowed_content_dict:dict):
1140
+    def _set_allowed_content(self, content: Content, allowed_content_dict: dict) -> None:  # nopep8
1139
         """
1141
         """
1140
-        :param folder: the given folder instance
1142
+        :param content: the given content instance
1141
         :param allowed_content_dict: must be something like this:
1143
         :param allowed_content_dict: must be something like this:
1142
             dict(
1144
             dict(
1143
                 folder = True
1145
                 folder = True
1145
                 file = False,
1147
                 file = False,
1146
                 page = True
1148
                 page = True
1147
             )
1149
             )
1148
-        :return:
1150
+        :return: nothing
1151
+        """
1152
+        properties = content.properties.copy()
1153
+        properties['allowed_content'] = allowed_content_dict
1154
+        content.properties = properties
1155
+
1156
+    def set_allowed_content(self, content: Content, allowed_content_type_slug_list: typing.List[str]) -> None:  # nopep8
1157
+        """
1158
+        :param content: the given content instance
1159
+        :param allowed_content_type_slug_list: list of content_type_slug to
1160
+        accept as subcontent.
1161
+        :return: nothing
1162
+        """
1163
+        allowed_content_dict = {}
1164
+        for allowed_content_type_slug in allowed_content_type_slug_list:
1165
+            if allowed_content_type_slug not in CONTENT_TYPES.extended_endpoint_allowed_types_slug():
1166
+                raise ContentTypeNotExist('Content_type {} does not exist'.format(allowed_content_type_slug))  # nopep8
1167
+            allowed_content_dict[allowed_content_type_slug] = True
1168
+
1169
+        self._set_allowed_content(content, allowed_content_dict)
1170
+
1171
+    def restore_content_default_allowed_content(self, content: Content) -> None:
1172
+        """
1173
+        Return to default allowed_content_types
1174
+        :param content: the given content instance
1175
+        :return: nothing
1149
         """
1176
         """
1150
-        properties = dict(allowed_content = allowed_content_dict)
1151
-        folder.properties = properties
1177
+        if content._properties and 'allowed_content' in content._properties:
1178
+            properties = content.properties.copy()
1179
+            del properties['allowed_content']
1180
+            content.properties = properties
1152
 
1181
 
1153
     def set_status(self, content: Content, new_status: str):
1182
     def set_status(self, content: Content, new_status: str):
1154
         if new_status in CONTENT_STATUS.allowed_slugs_values():
1183
         if new_status in CONTENT_STATUS.allowed_slugs_values():

+ 0 - 8
backend/tracim_backend/lib/webdav/resources.py View File

295
             parent=self.content
295
             parent=self.content
296
         )
296
         )
297
 
297
 
298
-        subcontent = dict(
299
-            folder=True,
300
-            thread=True,
301
-            file=True,
302
-            page=True
303
-        )
304
-
305
-        self.content_api.set_allowed_content(folder, subcontent)
306
         self.content_api.save(folder)
298
         self.content_api.save(folder)
307
 
299
 
308
         transaction.commit()
300
         transaction.commit()

+ 131 - 2
backend/tracim_backend/tests/library/test_content_api.py View File

209
             do_save=False
209
             do_save=False
210
         )
210
         )
211
         allowed_content_dict = {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: False} # nopep8
211
         allowed_content_dict = {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: False} # nopep8
212
-        api.set_allowed_content(
212
+        api._set_allowed_content(
213
             folder,
213
             folder,
214
             allowed_content_dict=allowed_content_dict
214
             allowed_content_dict=allowed_content_dict
215
         )
215
         )
275
                 parent=None,
275
                 parent=None,
276
                 label='lapin',
276
                 label='lapin',
277
                 do_save=True
277
                 do_save=True
278
-            )
278
+           )
279
+
280
+    def test_unit__set_allowed_content__ok__private_method(self):
281
+        uapi = UserApi(
282
+            session=self.session,
283
+            config=self.app_config,
284
+            current_user=None,
285
+        )
286
+        group_api = GroupApi(
287
+            current_user=None,
288
+            session=self.session,
289
+            config=self.app_config,
290
+        )
291
+        groups = [group_api.get_one(Group.TIM_USER),
292
+                  group_api.get_one(Group.TIM_MANAGER),
293
+                  group_api.get_one(Group.TIM_ADMIN)]
294
+
295
+        user = uapi.create_minimal_user(email='this.is@user',
296
+                                        groups=groups, save_now=True)
297
+        workspace = WorkspaceApi(
298
+            current_user=user,
299
+            session=self.session,
300
+            config=self.app_config,
301
+        ).create_workspace('test workspace', save_now=True)
302
+        api = ContentApi(
303
+            current_user=user,
304
+            session=self.session,
305
+            config=self.app_config,
306
+        )
307
+        folder = api.create(
308
+            content_type_slug=CONTENT_TYPES.Folder.slug,
309
+            workspace=workspace,
310
+            parent=None,
311
+            label='plop',
312
+            do_save=False
313
+        )
314
+        allowed_content_dict = {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: False}  # nopep8
315
+        api._set_allowed_content(
316
+            folder,
317
+            allowed_content_dict=allowed_content_dict
318
+        )
319
+        assert 'allowed_content' in folder.properties
320
+        assert folder.properties['allowed_content'] == {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: False}
321
+
322
+    def test_unit__set_allowed_content__ok__nominal_case(self):
323
+        uapi = UserApi(
324
+            session=self.session,
325
+            config=self.app_config,
326
+            current_user=None,
327
+        )
328
+        group_api = GroupApi(
329
+            current_user=None,
330
+            session=self.session,
331
+            config=self.app_config,
332
+        )
333
+        groups = [group_api.get_one(Group.TIM_USER),
334
+                  group_api.get_one(Group.TIM_MANAGER),
335
+                  group_api.get_one(Group.TIM_ADMIN)]
336
+
337
+        user = uapi.create_minimal_user(email='this.is@user',
338
+                                        groups=groups, save_now=True)
339
+        workspace = WorkspaceApi(
340
+            current_user=user,
341
+            session=self.session,
342
+            config=self.app_config,
343
+        ).create_workspace('test workspace', save_now=True)
344
+        api = ContentApi(
345
+            current_user=user,
346
+            session=self.session,
347
+            config=self.app_config,
348
+        )
349
+        folder = api.create(
350
+            content_type_slug=CONTENT_TYPES.Folder.slug,
351
+            workspace=workspace,
352
+            parent=None,
353
+            label='plop',
354
+            do_save=False
355
+        )
356
+        allowed_content_type_slug_list = [CONTENT_TYPES.Folder.slug, CONTENT_TYPES.File.slug]  # nopep8
357
+        api.set_allowed_content(
358
+            folder,
359
+            allowed_content_type_slug_list=allowed_content_type_slug_list
360
+        )
361
+        assert 'allowed_content' in folder.properties
362
+        assert folder.properties['allowed_content'] == {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: True}
363
+
364
+    def test_unit__restore_content_default_allowed_content__ok__nominal_case(self):
365
+        uapi = UserApi(
366
+            session=self.session,
367
+            config=self.app_config,
368
+            current_user=None,
369
+        )
370
+        group_api = GroupApi(
371
+            current_user=None,
372
+            session=self.session,
373
+            config=self.app_config,
374
+        )
375
+        groups = [group_api.get_one(Group.TIM_USER),
376
+                  group_api.get_one(Group.TIM_MANAGER),
377
+                  group_api.get_one(Group.TIM_ADMIN)]
378
+
379
+        user = uapi.create_minimal_user(email='this.is@user',
380
+                                        groups=groups, save_now=True)
381
+        workspace = WorkspaceApi(
382
+            current_user=user,
383
+            session=self.session,
384
+            config=self.app_config,
385
+        ).create_workspace('test workspace', save_now=True)
386
+        api = ContentApi(
387
+            current_user=user,
388
+            session=self.session,
389
+            config=self.app_config,
390
+        )
391
+        folder = api.create(
392
+            content_type_slug=CONTENT_TYPES.Folder.slug,
393
+            workspace=workspace,
394
+            parent=None,
395
+            label='plop',
396
+            do_save=False
397
+        )
398
+        allowed_content_type_slug_list = [CONTENT_TYPES.Folder.slug, CONTENT_TYPES.File.slug]  # nopep8
399
+        api.set_allowed_content(
400
+            folder,
401
+            allowed_content_type_slug_list=allowed_content_type_slug_list
402
+        )
403
+        assert 'allowed_content' in folder.properties
404
+        assert folder.properties['allowed_content'] == {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: True} # nopep8
405
+        api.restore_content_default_allowed_content(folder)
406
+        assert 'allowed_content' in folder.properties
407
+        assert folder.properties['allowed_content'] == CONTENT_TYPES.default_allowed_content_properties(folder.type)  # nopep8
279
 
408
 
280
     def test_delete(self):
409
     def test_delete(self):
281
         uapi = UserApi(
410
         uapi = UserApi(