Selaa lähdekoodia

Allow create content in folder

inkey 6 vuotta sitten
vanhempi
commit
fdc9941622

+ 3 - 0
tracim/exceptions.py Näytä tiedosto

@@ -143,3 +143,6 @@ class EmptyLabelNotAllowed(EmptyValueNotAllowed):
143 143
 
144 144
 class EmptyRawContentNotAllowed(EmptyValueNotAllowed):
145 145
     pass
146
+
147
+class ParentNotFound(NotFound):
148
+    pass

+ 2 - 0
tracim/models/context_models.py Näytä tiedosto

@@ -83,9 +83,11 @@ class ContentCreation(object):
83 83
             self,
84 84
             label: str,
85 85
             content_type: str,
86
+            parent_id: typing.Optional[int] = None,
86 87
     ) -> None:
87 88
         self.label = label
88 89
         self.content_type = content_type
90
+        self.parent_id = parent_id
89 91
 
90 92
 
91 93
 class CommentCreation(object):

+ 43 - 0
tracim/tests/functional/test_workspaces.py Näytä tiedosto

@@ -834,6 +834,49 @@ class TestWorkspaceContents(FunctionalTest):
834 834
         active_contents = self.testapp.get('/api/v2/workspaces/1/contents', params=params_active, status=200).json_body  # nopep8
835 835
         assert res.json_body in active_contents
836 836
 
837
+    def test_api__post_content_create_generic_content__ok_200__in_folder(self) -> None:  # nopep8
838
+        """
839
+        Create generic content in folder
840
+        """
841
+        self.testapp.authorization = (
842
+            'Basic',
843
+            (
844
+                'admin@admin.admin',
845
+                'admin@admin.admin'
846
+            )
847
+        )
848
+        params = {
849
+            'label': 'GenericCreatedContent',
850
+            'content_type': 'markdownpage',
851
+            'parent_id': 10,
852
+        }
853
+        res = self.testapp.post_json(
854
+            '/api/v2/workspaces/1/contents',
855
+            params=params,
856
+            status=200
857
+        )
858
+        assert res
859
+        assert res.json_body
860
+        assert res.json_body['status'] == 'open'
861
+        assert res.json_body['content_id']
862
+        assert res.json_body['content_type'] == 'markdownpage'
863
+        assert res.json_body['is_archived'] is False
864
+        assert res.json_body['is_deleted'] is False
865
+        assert res.json_body['workspace_id'] == 1
866
+        assert res.json_body['slug'] == 'genericcreatedcontent'
867
+        assert res.json_body['parent_id'] == 10
868
+        assert res.json_body['show_in_ui'] is True
869
+        assert res.json_body['sub_content_types']
870
+        params_active = {
871
+            'parent_id': 10,
872
+            'show_archived': 0,
873
+            'show_deleted': 0,
874
+            'show_active': 1,
875
+        }
876
+        # INFO - G.M - 2018-06-165 - Verify if new content is correctly created
877
+        active_contents = self.testapp.get('/api/v2/workspaces/1/contents', params=params_active, status=200).json_body  # nopep8
878
+        assert res.json_body in active_contents
879
+
837 880
     def test_api__post_content_create_generic_content__err_400__empty_label(self) -> None:  # nopep8
838 881
         """
839 882
         Create generic content

+ 5 - 0
tracim/views/core_api/schemas.py Näytä tiedosto

@@ -339,6 +339,11 @@ class ContentCreationSchema(marshmallow.Schema):
339 339
         example='html-documents',
340 340
         validate=OneOf(ContentType.allowed_types_for_folding()),  # nopep8
341 341
     )
342
+    parent_id = marshmallow.fields.Integer(
343
+        example=35,
344
+        description='content_id of parent content, if content should be placed in a folder, this should be folder content_id.'
345
+    )
346
+
342 347
 
343 348
     @post_load
344 349
     def make_content_filter(self, data):

+ 12 - 2
tracim/views/core_api/workspace_controller.py Näytä tiedosto

@@ -17,8 +17,9 @@ from tracim.models.data import UserRoleInWorkspace
17 17
 from tracim.models.data import ActionDescription
18 18
 from tracim.models.context_models import UserRoleWorkspaceInContext
19 19
 from tracim.models.context_models import ContentInContext
20
-from tracim.exceptions import EmptyLabelNotAllowed
20
+from tracim.exceptions import EmptyLabelNotAllowed, ContentNotFound
21 21
 from tracim.exceptions import WorkspacesDoNotMatch
22
+from tracim.exceptions import ParentNotFound
22 23
 from tracim.views.controllers import Controller
23 24
 from tracim.views.core_api.schemas import FilterContentQuerySchema
24 25
 from tracim.views.core_api.schemas import ContentMoveSchema
@@ -133,12 +134,21 @@ class WorkspaceController(Controller):
133 134
         api = ContentApi(
134 135
             current_user=request.current_user,
135 136
             session=request.dbsession,
136
-            config=app_config,
137
+            config=app_config
137 138
         )
139
+        parent = None
140
+        if creation_data.parent_id:
141
+            try:
142
+                parent = api.get_one(content_id=creation_data.parent_id, content_type=ContentType.Any)  # nopep8
143
+            except ContentNotFound as exc:
144
+                raise ParentNotFound(
145
+                    'Parent with content_id {} not found'.format(creation_data.parent_id)
146
+                ) from exc
138 147
         content = api.create(
139 148
             label=creation_data.label,
140 149
             content_type=creation_data.content_type,
141 150
             workspace=request.current_workspace,
151
+            parent=parent,
142 152
         )
143 153
         api.save(content, ActionDescription.CREATION)
144 154
         content = api.get_content_in_context(content)