瀏覽代碼

Merge pull request #98 from tracim/fix/create_content_in_folder

Damien Accorsi 6 年之前
父節點
當前提交
bbccafc5a2
沒有帳戶連結到提交者的電子郵件

+ 3 - 0
tracim/exceptions.py 查看文件

@@ -162,3 +162,6 @@ class EmptyLabelNotAllowed(EmptyValueNotAllowed):
162 162
 
163 163
 class EmptyCommentContentNotAllowed(EmptyValueNotAllowed):
164 164
     pass
165
+
166
+class ParentNotFound(NotFound):
167
+    pass

+ 2 - 0
tracim/models/context_models.py 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -371,6 +371,11 @@ class ContentCreationSchema(marshmallow.Schema):
371 371
         example='html-documents',
372 372
         validate=OneOf(ContentType.allowed_types_for_folding()),  # nopep8
373 373
     )
374
+    parent_id = marshmallow.fields.Integer(
375
+        example=35,
376
+        description='content_id of parent content, if content should be placed in a folder, this should be folder content_id.'
377
+    )
378
+
374 379
 
375 380
     @post_load
376 381
     def make_content_filter(self, data):

+ 12 - 1
tracim/views/core_api/workspace_controller.py 查看文件

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