Browse Source

fix default allowed types according to existent_content_typs

Guénaël Muller 6 years ago
parent
commit
0c7ca8e8af

+ 21 - 0
backend/tracim_backend/models/contents.py View File

121
             creation_label: str,
121
             creation_label: str,
122
             available_statuses: typing.List[ContentStatus],
122
             available_statuses: typing.List[ContentStatus],
123
             slug_alias: typing.List[str] = None,
123
             slug_alias: typing.List[str] = None,
124
+            allow_sub_content: bool = False,
124
     ):
125
     ):
125
         self.slug = slug
126
         self.slug = slug
126
         self.fa_icon = fa_icon
127
         self.fa_icon = fa_icon
129
         self.creation_label = creation_label
130
         self.creation_label = creation_label
130
         self.available_statuses = available_statuses
131
         self.available_statuses = available_statuses
131
         self.slug_alias = slug_alias
132
         self.slug_alias = slug_alias
133
+        self.allow_sub_content = allow_sub_content
132
 
134
 
133
 
135
 
134
 thread_type = ContentType(
136
 thread_type = ContentType(
176
     label='Folder',
178
     label='Folder',
177
     creation_label='Create collection of any documents',
179
     creation_label='Create collection of any documents',
178
     available_statuses=CONTENT_STATUS.allowed(),
180
     available_statuses=CONTENT_STATUS.allowed(),
181
+    allow_sub_content=True,
179
 )
182
 )
180
 
183
 
181
 
184
 
240
         allowed_type_slug = [contents_type.slug for contents_type in self._content_types]  # nopep8
243
         allowed_type_slug = [contents_type.slug for contents_type in self._content_types]  # nopep8
241
         return allowed_type_slug
244
         return allowed_type_slug
242
 
245
 
246
+    def extended_endpoint_allowed_types_slug(self) -> typing.List[str]:
247
+        allowed_types_slug = self.endpoint_allowed_types_slug().copy()
248
+        for content_type in self._special_contents_types:
249
+            allowed_types_slug.append(content_type.slug)
250
+        return allowed_types_slug
251
+
243
     def query_allowed_types_slugs(self) -> typing.List[str]:
252
     def query_allowed_types_slugs(self) -> typing.List[str]:
244
         """
253
         """
245
         Return alls allowed types slug : content_type slug + all alias, any
254
         Return alls allowed types slug : content_type slug + all alias, any
256
         allowed_types_slug.extend(self._extra_slugs)
265
         allowed_types_slug.extend(self._extra_slugs)
257
         return allowed_types_slug
266
         return allowed_types_slug
258
 
267
 
268
+    def default_allowed_content_properties(self, slug) -> dict:
269
+        content_type = self.get_one_by_slug(slug)
270
+        if content_type.allow_sub_content:
271
+            sub_content_allowed = self.extended_endpoint_allowed_types_slug()
272
+        else:
273
+            sub_content_allowed = [self.Comment.slug]
274
+
275
+        properties_dict = {}
276
+        for elem in sub_content_allowed:
277
+            properties_dict[elem] = True
278
+        return properties_dict
279
+
259
 
280
 
260
 CONTENT_TYPES = ContentTypeList(
281
 CONTENT_TYPES = ContentTypeList(
261
     [
282
     [

+ 21 - 42
backend/tracim_backend/models/data.py View File

32
 from tracim_backend.models.auth import User
32
 from tracim_backend.models.auth import User
33
 from tracim_backend.models.roles import WorkspaceRoles
33
 from tracim_backend.models.roles import WorkspaceRoles
34
 
34
 
35
-DEFAULT_PROPERTIES = dict(
36
-    allowed_content=dict(
37
-        folder=True,
38
-        file=True,
39
-        page=True,
40
-        thread=True,
41
-    ),
42
-)
43
-
44
 
35
 
45
 class Workspace(DeclarativeBase):
36
 class Workspace(DeclarativeBase):
46
 
37
 
545
 
536
 
546
     @classmethod
537
     @classmethod
547
     def check_properties(cls, item):
538
     def check_properties(cls, item):
548
-        if item.type == CONTENT_TYPES.Folder.slug:
549
-            properties = item.properties
550
-            if 'allowed_content' not in properties.keys():
551
-                return False
552
-            if 'folders' not in properties['allowed_content']:
553
-                return False
554
-            if 'files' not in properties['allowed_content']:
555
-                return False
556
-            if 'pages' not in properties['allowed_content']:
557
-                return False
558
-            if 'threads' not in properties['allowed_content']:
559
-                return False
560
-            return True
561
-
539
+        properties = item.properties
562
         if item.type == CONTENT_TYPES.Event.slug:
540
         if item.type == CONTENT_TYPES.Event.slug:
563
-            properties = item.properties
564
             if 'name' not in properties.keys():
541
             if 'name' not in properties.keys():
565
                 return False
542
                 return False
566
             if 'raw' not in properties.keys():
543
             if 'raw' not in properties.keys():
570
             if 'end' not in properties.keys():
547
             if 'end' not in properties.keys():
571
                 return False
548
                 return False
572
             return True
549
             return True
573
-
574
-        # TODO - G.M - 15-03-2018 - Choose only correct Content-type for origin
575
-        # Only content who can be copied need this
576
-        if item.type == CONTENT_TYPES.Any_SLUG:
577
-            properties = item.properties
550
+        else:
551
+            if 'allowed_content' in properties.keys():
552
+                for content_slug, value in properties['allowed_content'].items():  # nopep8
553
+                    if not isinstance(value, bool):
554
+                        return False
555
+                    if not content_slug in CONTENT_TYPES.extended_endpoint_allowed_types_slug():  # nopep8
556
+                        return False
578
             if 'origin' in properties.keys():
557
             if 'origin' in properties.keys():
579
-                return True
580
-        raise NotImplementedError
581
-
582
-    @classmethod
583
-    def reset_properties(cls, item):
584
-        if item.type == CONTENT_TYPES.Folder.slug:
585
-            item.properties = DEFAULT_PROPERTIES
586
-            return
587
-
588
-        raise NotImplementedError
558
+                pass
559
+            return True
589
 
560
 
590
 
561
 
591
 class ContentRevisionRO(DeclarativeBase):
562
 class ContentRevisionRO(DeclarativeBase):
1204
     @hybrid_property
1175
     @hybrid_property
1205
     def properties(self) -> dict:
1176
     def properties(self) -> dict:
1206
         """ return a structure decoded from json content of _properties """
1177
         """ return a structure decoded from json content of _properties """
1178
+
1207
         if not self._properties:
1179
         if not self._properties:
1208
-            return DEFAULT_PROPERTIES
1209
-        return json.loads(self._properties)
1180
+            properties = {}
1181
+        else:
1182
+            properties = json.loads(self._properties)
1183
+        if CONTENT_TYPES.get_one_by_slug(self.type) != CONTENT_TYPES.Event:
1184
+            if not 'allowed_content' in self._properties:
1185
+                properties['allowed_content'] = CONTENT_TYPES.default_allowed_content_properties(self.type)
1186
+        return properties
1210
 
1187
 
1211
     @properties.setter
1188
     @properties.setter
1212
     def properties(self, properties_struct: dict) -> None:
1189
     def properties(self, properties_struct: dict) -> None:
1347
             allowed_types = self.properties['allowed_content']
1324
             allowed_types = self.properties['allowed_content']
1348
             for type_label, is_allowed in allowed_types.items():
1325
             for type_label, is_allowed in allowed_types.items():
1349
                 if is_allowed:
1326
                 if is_allowed:
1350
-                    types.append(CONTENT_TYPES.get_one_by_slug(type_label))
1327
+                   types.append(
1328
+                        CONTENT_TYPES.get_one_by_slug(type_label)
1329
+                   )
1351
         except Exception as e:
1330
         except Exception as e:
1352
             print(e.__str__())
1331
             print(e.__str__())
1353
             print('----- /*\ *****')
1332
             print('----- /*\ *****')

+ 26 - 22
backend/tracim_backend/tests/functional/test_workspaces.py View File

643
         assert content['show_in_ui'] is True
643
         assert content['show_in_ui'] is True
644
         assert content['slug'] == 'tools'
644
         assert content['slug'] == 'tools'
645
         assert content['status'] == 'open'
645
         assert content['status'] == 'open'
646
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
646
+        assert len(content['sub_content_types']) > 1
647
+        assert 'comment' in content['sub_content_types']
648
+        assert 'folder' in content['sub_content_types']
647
         assert content['workspace_id'] == 1
649
         assert content['workspace_id'] == 1
648
         content = res[1]
650
         content = res[1]
649
         assert content['content_id'] == 2
651
         assert content['content_id'] == 2
655
         assert content['show_in_ui'] is True
657
         assert content['show_in_ui'] is True
656
         assert content['slug'] == 'menus'
658
         assert content['slug'] == 'menus'
657
         assert content['status'] == 'open'
659
         assert content['status'] == 'open'
658
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
660
+        assert len(content['sub_content_types']) > 1
661
+        assert 'comment' in content['sub_content_types']
662
+        assert 'folder' in content['sub_content_types']
659
         assert content['workspace_id'] == 1
663
         assert content['workspace_id'] == 1
660
         content = res[2]
664
         content = res[2]
661
         assert content['content_id'] == 11
665
         assert content['content_id'] == 11
667
         assert content['show_in_ui'] is True
671
         assert content['show_in_ui'] is True
668
         assert content['slug'] == 'current-menu'
672
         assert content['slug'] == 'current-menu'
669
         assert content['status'] == 'open'
673
         assert content['status'] == 'open'
670
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
674
+        assert set(content['sub_content_types']) == {'comment'}
671
         assert content['workspace_id'] == 1
675
         assert content['workspace_id'] == 1
672
 
676
 
673
     def test_api__get_workspace_content__ok_200__get_default_html_documents(self):
677
     def test_api__get_workspace_content__ok_200__get_default_html_documents(self):
697
         assert content['show_in_ui'] is True
701
         assert content['show_in_ui'] is True
698
         assert content['slug'] == 'current-menu'
702
         assert content['slug'] == 'current-menu'
699
         assert content['status'] == 'open'
703
         assert content['status'] == 'open'
700
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
704
+        assert set(content['sub_content_types']) == {'comment'}
701
         assert content['workspace_id'] == 1
705
         assert content['workspace_id'] == 1
702
 
706
 
703
     # Root related
707
     # Root related
736
         assert content['show_in_ui'] is True
740
         assert content['show_in_ui'] is True
737
         assert content['slug'] == 'new-fruit-salad'
741
         assert content['slug'] == 'new-fruit-salad'
738
         assert content['status'] == 'open'
742
         assert content['status'] == 'open'
739
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
743
+        assert set(content['sub_content_types']) == {'comment'}
740
         assert content['workspace_id'] == 3
744
         assert content['workspace_id'] == 3
741
 
745
 
742
         content = res[2]
746
         content = res[2]
749
         assert content['show_in_ui'] is True
753
         assert content['show_in_ui'] is True
750
         assert content['slug'].startswith('fruit-salad')
754
         assert content['slug'].startswith('fruit-salad')
751
         assert content['status'] == 'open'
755
         assert content['status'] == 'open'
752
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
756
+        assert set(content['sub_content_types']) == {'comment'}
753
         assert content['workspace_id'] == 3
757
         assert content['workspace_id'] == 3
754
 
758
 
755
         content = res[3]
759
         content = res[3]
762
         assert content['show_in_ui'] is True
766
         assert content['show_in_ui'] is True
763
         assert content['slug'].startswith('bad-fruit-salad')
767
         assert content['slug'].startswith('bad-fruit-salad')
764
         assert content['status'] == 'open'
768
         assert content['status'] == 'open'
765
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
769
+        assert set(content['sub_content_types']) == {'comment'}
766
         assert content['workspace_id'] == 3
770
         assert content['workspace_id'] == 3
767
 
771
 
768
     def test_api__get_workspace_content__ok_200__get_all_root_content(self):
772
     def test_api__get_workspace_content__ok_200__get_all_root_content(self):
799
         assert content['show_in_ui'] is True
803
         assert content['show_in_ui'] is True
800
         assert content['slug'] == 'new-fruit-salad'
804
         assert content['slug'] == 'new-fruit-salad'
801
         assert content['status'] == 'open'
805
         assert content['status'] == 'open'
802
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
806
+        assert set(content['sub_content_types']) == {'comment'}
803
         assert content['workspace_id'] == 3
807
         assert content['workspace_id'] == 3
804
 
808
 
805
         content = res[2]
809
         content = res[2]
812
         assert content['show_in_ui'] is True
816
         assert content['show_in_ui'] is True
813
         assert content['slug'].startswith('fruit-salad')
817
         assert content['slug'].startswith('fruit-salad')
814
         assert content['status'] == 'open'
818
         assert content['status'] == 'open'
815
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
819
+        assert set(content['sub_content_types']) == {'comment'}
816
         assert content['workspace_id'] == 3
820
         assert content['workspace_id'] == 3
817
 
821
 
818
         content = res[3]
822
         content = res[3]
825
         assert content['show_in_ui'] is True
829
         assert content['show_in_ui'] is True
826
         assert content['slug'].startswith('bad-fruit-salad')
830
         assert content['slug'].startswith('bad-fruit-salad')
827
         assert content['status'] == 'open'
831
         assert content['status'] == 'open'
828
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
832
+        assert set(content['sub_content_types']) == {'comment'}
829
         assert content['workspace_id'] == 3
833
         assert content['workspace_id'] == 3
830
 
834
 
831
     def test_api__get_workspace_content__ok_200__get_only_active_root_content(self):  # nopep8
835
     def test_api__get_workspace_content__ok_200__get_only_active_root_content(self):  # nopep8
862
         assert content['show_in_ui'] is True
866
         assert content['show_in_ui'] is True
863
         assert content['slug'] == 'new-fruit-salad'
867
         assert content['slug'] == 'new-fruit-salad'
864
         assert content['status'] == 'open'
868
         assert content['status'] == 'open'
865
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
869
+        assert set(content['sub_content_types']) == {'comment'}
866
         assert content['workspace_id'] == 3
870
         assert content['workspace_id'] == 3
867
 
871
 
868
     def test_api__get_workspace_content__ok_200__get_only_archived_root_content(self):  # nopep8
872
     def test_api__get_workspace_content__ok_200__get_only_archived_root_content(self):  # nopep8
898
         assert content['show_in_ui'] is True
902
         assert content['show_in_ui'] is True
899
         assert content['slug'].startswith('fruit-salad')
903
         assert content['slug'].startswith('fruit-salad')
900
         assert content['status'] == 'open'
904
         assert content['status'] == 'open'
901
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
905
+        assert set(content['sub_content_types']) == {'comment'}
902
         assert content['workspace_id'] == 3
906
         assert content['workspace_id'] == 3
903
 
907
 
904
     def test_api__get_workspace_content__ok_200__get_only_deleted_root_content(self):  # nopep8
908
     def test_api__get_workspace_content__ok_200__get_only_deleted_root_content(self):  # nopep8
936
         assert content['show_in_ui'] is True
940
         assert content['show_in_ui'] is True
937
         assert content['slug'].startswith('bad-fruit-salad')
941
         assert content['slug'].startswith('bad-fruit-salad')
938
         assert content['status'] == 'open'
942
         assert content['status'] == 'open'
939
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
943
+        assert set(content['sub_content_types']) == {'comment'}
940
         assert content['workspace_id'] == 3
944
         assert content['workspace_id'] == 3
941
 
945
 
942
     def test_api__get_workspace_content__ok_200__get_nothing_root_content(self):
946
     def test_api__get_workspace_content__ok_200__get_nothing_root_content(self):
1058
         assert content['show_in_ui'] is True
1062
         assert content['show_in_ui'] is True
1059
         assert content['slug'] == 'test-thread'
1063
         assert content['slug'] == 'test-thread'
1060
         assert content['status'] == 'open'
1064
         assert content['status'] == 'open'
1061
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1065
+        assert set(content['sub_content_types']) == {'comment'}
1062
         assert content['workspace_id'] == 1
1066
         assert content['workspace_id'] == 1
1063
 
1067
 
1064
     def test_api__get_workspace_content__ok_200__get_all_filter_content_html_and_legacy_page(self):  # nopep8
1068
     def test_api__get_workspace_content__ok_200__get_all_filter_content_html_and_legacy_page(self):  # nopep8
1155
         assert content['show_in_ui'] is True
1159
         assert content['show_in_ui'] is True
1156
         assert content['slug'] == 'test-page'
1160
         assert content['slug'] == 'test-page'
1157
         assert content['status'] == 'open'
1161
         assert content['status'] == 'open'
1158
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1162
+        assert set(content['sub_content_types']) == {'comment'}  # nopep8
1159
         assert content['workspace_id'] == 1
1163
         assert content['workspace_id'] == 1
1160
         content = res[1]
1164
         content = res[1]
1161
         assert content['content_type'] == 'html-document'
1165
         assert content['content_type'] == 'html-document'
1167
         assert content['show_in_ui'] is True
1171
         assert content['show_in_ui'] is True
1168
         assert content['slug'] == 'test-html-page'
1172
         assert content['slug'] == 'test-html-page'
1169
         assert content['status'] == 'open'
1173
         assert content['status'] == 'open'
1170
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1174
+        assert set(content['sub_content_types']) == {'comment'}  # nopep8
1171
         assert content['workspace_id'] == 1
1175
         assert content['workspace_id'] == 1
1172
         assert res[0]['content_id'] != res[1]['content_id']
1176
         assert res[0]['content_id'] != res[1]['content_id']
1173
 
1177
 
1205
         assert content['show_in_ui'] is True
1209
         assert content['show_in_ui'] is True
1206
         assert content['slug'] == 'new-fruit-salad'
1210
         assert content['slug'] == 'new-fruit-salad'
1207
         assert content['status'] == 'open'
1211
         assert content['status'] == 'open'
1208
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1212
+        assert set(content['sub_content_types']) == {'comment'}  # nopep8
1209
         assert content['workspace_id'] == 2
1213
         assert content['workspace_id'] == 2
1210
 
1214
 
1211
         content = res[1]
1215
         content = res[1]
1218
         assert content['show_in_ui'] is True
1222
         assert content['show_in_ui'] is True
1219
         assert content['slug'].startswith('fruit-salad')
1223
         assert content['slug'].startswith('fruit-salad')
1220
         assert content['status'] == 'open'
1224
         assert content['status'] == 'open'
1221
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1225
+        assert set(content['sub_content_types']) == {'comment'}  # nopep8
1222
         assert content['workspace_id'] == 2
1226
         assert content['workspace_id'] == 2
1223
 
1227
 
1224
         content = res[2]
1228
         content = res[2]
1231
         assert content['show_in_ui'] is True
1235
         assert content['show_in_ui'] is True
1232
         assert content['slug'].startswith('bad-fruit-salad')
1236
         assert content['slug'].startswith('bad-fruit-salad')
1233
         assert content['status'] == 'open'
1237
         assert content['status'] == 'open'
1234
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1238
+        assert set(content['sub_content_types']) == {'comment'}  # nopep8
1235
         assert content['workspace_id'] == 2
1239
         assert content['workspace_id'] == 2
1236
 
1240
 
1237
     def test_api__get_workspace_content__ok_200__get_only_active_folder_content(self):  # nopep8
1241
     def test_api__get_workspace_content__ok_200__get_only_active_folder_content(self):  # nopep8
1267
         assert content['show_in_ui'] is True
1271
         assert content['show_in_ui'] is True
1268
         assert content['slug'] == 'new-fruit-salad'
1272
         assert content['slug'] == 'new-fruit-salad'
1269
         assert content['status'] == 'open'
1273
         assert content['status'] == 'open'
1270
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1274
+        assert set(content['sub_content_types']) == {'comment'}  # nopep8
1271
         assert content['workspace_id'] == 2
1275
         assert content['workspace_id'] == 2
1272
 
1276
 
1273
     def test_api__get_workspace_content__ok_200__get_only_archived_folder_content(self):  # nopep8
1277
     def test_api__get_workspace_content__ok_200__get_only_archived_folder_content(self):  # nopep8
1303
         assert content['show_in_ui'] is True
1307
         assert content['show_in_ui'] is True
1304
         assert content['slug'].startswith('fruit-salad')
1308
         assert content['slug'].startswith('fruit-salad')
1305
         assert content['status'] == 'open'
1309
         assert content['status'] == 'open'
1306
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1310
+        assert set(content['sub_content_types']) == {'comment'}  # nopep8
1307
         assert content['workspace_id'] == 2
1311
         assert content['workspace_id'] == 2
1308
 
1312
 
1309
     def test_api__get_workspace_content__ok_200__get_only_deleted_folder_content(self):  # nopep8
1313
     def test_api__get_workspace_content__ok_200__get_only_deleted_folder_content(self):  # nopep8
1340
         assert content['show_in_ui'] is True
1344
         assert content['show_in_ui'] is True
1341
         assert content['slug'].startswith('bad-fruit-salad')
1345
         assert content['slug'].startswith('bad-fruit-salad')
1342
         assert content['status'] == 'open'
1346
         assert content['status'] == 'open'
1343
-        assert set(content['sub_content_types']) == {'thread', 'html-document', 'folder', 'file'}  # nopep8
1347
+        assert set(content['sub_content_types']) == {'comment'}  # nopep8
1344
         assert content['workspace_id'] == 2
1348
         assert content['workspace_id'] == 2
1345
 
1349
 
1346
     def test_api__get_workspace_content__ok_200__get_nothing_folder_content(self):  # nopep8
1350
     def test_api__get_workspace_content__ok_200__get_nothing_folder_content(self):  # nopep8

+ 1 - 1
backend/tracim_backend/views/core_api/schemas.py View File

686
     sub_content_types = marshmallow.fields.List(
686
     sub_content_types = marshmallow.fields.List(
687
         marshmallow.fields.String(
687
         marshmallow.fields.String(
688
             example='html-content',
688
             example='html-content',
689
-            validate=OneOf(CONTENT_TYPES.endpoint_allowed_types_slug())
689
+            validate=OneOf(CONTENT_TYPES.extended_endpoint_allowed_types_slug())
690
         ),
690
         ),
691
         description='list of content types allowed as sub contents. '
691
         description='list of content types allowed as sub contents. '
692
                     'This field is required for folder contents, '
692
                     'This field is required for folder contents, '