Преглед изворни кода

fix default allowed types according to existent_content_typs

Guénaël Muller пре 6 година
родитељ
комит
0c7ca8e8af

+ 21 - 0
backend/tracim_backend/models/contents.py Прегледај датотеку

@@ -121,6 +121,7 @@ class ContentType(object):
121 121
             creation_label: str,
122 122
             available_statuses: typing.List[ContentStatus],
123 123
             slug_alias: typing.List[str] = None,
124
+            allow_sub_content: bool = False,
124 125
     ):
125 126
         self.slug = slug
126 127
         self.fa_icon = fa_icon
@@ -129,6 +130,7 @@ class ContentType(object):
129 130
         self.creation_label = creation_label
130 131
         self.available_statuses = available_statuses
131 132
         self.slug_alias = slug_alias
133
+        self.allow_sub_content = allow_sub_content
132 134
 
133 135
 
134 136
 thread_type = ContentType(
@@ -176,6 +178,7 @@ folder_type = ContentType(
176 178
     label='Folder',
177 179
     creation_label='Create collection of any documents',
178 180
     available_statuses=CONTENT_STATUS.allowed(),
181
+    allow_sub_content=True,
179 182
 )
180 183
 
181 184
 
@@ -240,6 +243,12 @@ class ContentTypeList(object):
240 243
         allowed_type_slug = [contents_type.slug for contents_type in self._content_types]  # nopep8
241 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 252
     def query_allowed_types_slugs(self) -> typing.List[str]:
244 253
         """
245 254
         Return alls allowed types slug : content_type slug + all alias, any
@@ -256,6 +265,18 @@ class ContentTypeList(object):
256 265
         allowed_types_slug.extend(self._extra_slugs)
257 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 281
 CONTENT_TYPES = ContentTypeList(
261 282
     [

+ 21 - 42
backend/tracim_backend/models/data.py Прегледај датотеку

@@ -32,15 +32,6 @@ from tracim_backend.models.meta import DeclarativeBase
32 32
 from tracim_backend.models.auth import User
33 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 36
 class Workspace(DeclarativeBase):
46 37
 
@@ -545,22 +536,8 @@ class ContentChecker(object):
545 536
 
546 537
     @classmethod
547 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 540
         if item.type == CONTENT_TYPES.Event.slug:
563
-            properties = item.properties
564 541
             if 'name' not in properties.keys():
565 542
                 return False
566 543
             if 'raw' not in properties.keys():
@@ -570,22 +547,16 @@ class ContentChecker(object):
570 547
             if 'end' not in properties.keys():
571 548
                 return False
572 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 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 562
 class ContentRevisionRO(DeclarativeBase):
@@ -1204,9 +1175,15 @@ class Content(DeclarativeBase):
1204 1175
     @hybrid_property
1205 1176
     def properties(self) -> dict:
1206 1177
         """ return a structure decoded from json content of _properties """
1178
+
1207 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 1188
     @properties.setter
1212 1189
     def properties(self, properties_struct: dict) -> None:
@@ -1347,7 +1324,9 @@ class Content(DeclarativeBase):
1347 1324
             allowed_types = self.properties['allowed_content']
1348 1325
             for type_label, is_allowed in allowed_types.items():
1349 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 1330
         except Exception as e:
1352 1331
             print(e.__str__())
1353 1332
             print('----- /*\ *****')

+ 26 - 22
backend/tracim_backend/tests/functional/test_workspaces.py Прегледај датотеку

@@ -643,7 +643,9 @@ class TestWorkspaceContents(FunctionalTest):
643 643
         assert content['show_in_ui'] is True
644 644
         assert content['slug'] == 'tools'
645 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 649
         assert content['workspace_id'] == 1
648 650
         content = res[1]
649 651
         assert content['content_id'] == 2
@@ -655,7 +657,9 @@ class TestWorkspaceContents(FunctionalTest):
655 657
         assert content['show_in_ui'] is True
656 658
         assert content['slug'] == 'menus'
657 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 663
         assert content['workspace_id'] == 1
660 664
         content = res[2]
661 665
         assert content['content_id'] == 11
@@ -667,7 +671,7 @@ class TestWorkspaceContents(FunctionalTest):
667 671
         assert content['show_in_ui'] is True
668 672
         assert content['slug'] == 'current-menu'
669 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 675
         assert content['workspace_id'] == 1
672 676
 
673 677
     def test_api__get_workspace_content__ok_200__get_default_html_documents(self):
@@ -697,7 +701,7 @@ class TestWorkspaceContents(FunctionalTest):
697 701
         assert content['show_in_ui'] is True
698 702
         assert content['slug'] == 'current-menu'
699 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 705
         assert content['workspace_id'] == 1
702 706
 
703 707
     # Root related
@@ -736,7 +740,7 @@ class TestWorkspaceContents(FunctionalTest):
736 740
         assert content['show_in_ui'] is True
737 741
         assert content['slug'] == 'new-fruit-salad'
738 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 744
         assert content['workspace_id'] == 3
741 745
 
742 746
         content = res[2]
@@ -749,7 +753,7 @@ class TestWorkspaceContents(FunctionalTest):
749 753
         assert content['show_in_ui'] is True
750 754
         assert content['slug'].startswith('fruit-salad')
751 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 757
         assert content['workspace_id'] == 3
754 758
 
755 759
         content = res[3]
@@ -762,7 +766,7 @@ class TestWorkspaceContents(FunctionalTest):
762 766
         assert content['show_in_ui'] is True
763 767
         assert content['slug'].startswith('bad-fruit-salad')
764 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 770
         assert content['workspace_id'] == 3
767 771
 
768 772
     def test_api__get_workspace_content__ok_200__get_all_root_content(self):
@@ -799,7 +803,7 @@ class TestWorkspaceContents(FunctionalTest):
799 803
         assert content['show_in_ui'] is True
800 804
         assert content['slug'] == 'new-fruit-salad'
801 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 807
         assert content['workspace_id'] == 3
804 808
 
805 809
         content = res[2]
@@ -812,7 +816,7 @@ class TestWorkspaceContents(FunctionalTest):
812 816
         assert content['show_in_ui'] is True
813 817
         assert content['slug'].startswith('fruit-salad')
814 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 820
         assert content['workspace_id'] == 3
817 821
 
818 822
         content = res[3]
@@ -825,7 +829,7 @@ class TestWorkspaceContents(FunctionalTest):
825 829
         assert content['show_in_ui'] is True
826 830
         assert content['slug'].startswith('bad-fruit-salad')
827 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 833
         assert content['workspace_id'] == 3
830 834
 
831 835
     def test_api__get_workspace_content__ok_200__get_only_active_root_content(self):  # nopep8
@@ -862,7 +866,7 @@ class TestWorkspaceContents(FunctionalTest):
862 866
         assert content['show_in_ui'] is True
863 867
         assert content['slug'] == 'new-fruit-salad'
864 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 870
         assert content['workspace_id'] == 3
867 871
 
868 872
     def test_api__get_workspace_content__ok_200__get_only_archived_root_content(self):  # nopep8
@@ -898,7 +902,7 @@ class TestWorkspaceContents(FunctionalTest):
898 902
         assert content['show_in_ui'] is True
899 903
         assert content['slug'].startswith('fruit-salad')
900 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 906
         assert content['workspace_id'] == 3
903 907
 
904 908
     def test_api__get_workspace_content__ok_200__get_only_deleted_root_content(self):  # nopep8
@@ -936,7 +940,7 @@ class TestWorkspaceContents(FunctionalTest):
936 940
         assert content['show_in_ui'] is True
937 941
         assert content['slug'].startswith('bad-fruit-salad')
938 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 944
         assert content['workspace_id'] == 3
941 945
 
942 946
     def test_api__get_workspace_content__ok_200__get_nothing_root_content(self):
@@ -1058,7 +1062,7 @@ class TestWorkspaceContents(FunctionalTest):
1058 1062
         assert content['show_in_ui'] is True
1059 1063
         assert content['slug'] == 'test-thread'
1060 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 1066
         assert content['workspace_id'] == 1
1063 1067
 
1064 1068
     def test_api__get_workspace_content__ok_200__get_all_filter_content_html_and_legacy_page(self):  # nopep8
@@ -1155,7 +1159,7 @@ class TestWorkspaceContents(FunctionalTest):
1155 1159
         assert content['show_in_ui'] is True
1156 1160
         assert content['slug'] == 'test-page'
1157 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 1163
         assert content['workspace_id'] == 1
1160 1164
         content = res[1]
1161 1165
         assert content['content_type'] == 'html-document'
@@ -1167,7 +1171,7 @@ class TestWorkspaceContents(FunctionalTest):
1167 1171
         assert content['show_in_ui'] is True
1168 1172
         assert content['slug'] == 'test-html-page'
1169 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 1175
         assert content['workspace_id'] == 1
1172 1176
         assert res[0]['content_id'] != res[1]['content_id']
1173 1177
 
@@ -1205,7 +1209,7 @@ class TestWorkspaceContents(FunctionalTest):
1205 1209
         assert content['show_in_ui'] is True
1206 1210
         assert content['slug'] == 'new-fruit-salad'
1207 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 1213
         assert content['workspace_id'] == 2
1210 1214
 
1211 1215
         content = res[1]
@@ -1218,7 +1222,7 @@ class TestWorkspaceContents(FunctionalTest):
1218 1222
         assert content['show_in_ui'] is True
1219 1223
         assert content['slug'].startswith('fruit-salad')
1220 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 1226
         assert content['workspace_id'] == 2
1223 1227
 
1224 1228
         content = res[2]
@@ -1231,7 +1235,7 @@ class TestWorkspaceContents(FunctionalTest):
1231 1235
         assert content['show_in_ui'] is True
1232 1236
         assert content['slug'].startswith('bad-fruit-salad')
1233 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 1239
         assert content['workspace_id'] == 2
1236 1240
 
1237 1241
     def test_api__get_workspace_content__ok_200__get_only_active_folder_content(self):  # nopep8
@@ -1267,7 +1271,7 @@ class TestWorkspaceContents(FunctionalTest):
1267 1271
         assert content['show_in_ui'] is True
1268 1272
         assert content['slug'] == 'new-fruit-salad'
1269 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 1275
         assert content['workspace_id'] == 2
1272 1276
 
1273 1277
     def test_api__get_workspace_content__ok_200__get_only_archived_folder_content(self):  # nopep8
@@ -1303,7 +1307,7 @@ class TestWorkspaceContents(FunctionalTest):
1303 1307
         assert content['show_in_ui'] is True
1304 1308
         assert content['slug'].startswith('fruit-salad')
1305 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 1311
         assert content['workspace_id'] == 2
1308 1312
 
1309 1313
     def test_api__get_workspace_content__ok_200__get_only_deleted_folder_content(self):  # nopep8
@@ -1340,7 +1344,7 @@ class TestWorkspaceContents(FunctionalTest):
1340 1344
         assert content['show_in_ui'] is True
1341 1345
         assert content['slug'].startswith('bad-fruit-salad')
1342 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 1348
         assert content['workspace_id'] == 2
1345 1349
 
1346 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 Прегледај датотеку

@@ -686,7 +686,7 @@ class ContentDigestSchema(marshmallow.Schema):
686 686
     sub_content_types = marshmallow.fields.List(
687 687
         marshmallow.fields.String(
688 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 691
         description='list of content types allowed as sub contents. '
692 692
                     'This field is required for folder contents, '