Browse Source

Merge pull request #98 from tracim/fix/create_content_in_folder

Damien Accorsi 6 years ago
parent
commit
bbccafc5a2
No account linked to committer's email

+ 3 - 0
tracim/exceptions.py View File

162
 
162
 
163
 class EmptyCommentContentNotAllowed(EmptyValueNotAllowed):
163
 class EmptyCommentContentNotAllowed(EmptyValueNotAllowed):
164
     pass
164
     pass
165
+
166
+class ParentNotFound(NotFound):
167
+    pass

+ 2 - 0
tracim/models/context_models.py View File

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

+ 43 - 0
tracim/tests/functional/test_workspaces.py View File

834
         active_contents = self.testapp.get('/api/v2/workspaces/1/contents', params=params_active, status=200).json_body  # nopep8
834
         active_contents = self.testapp.get('/api/v2/workspaces/1/contents', params=params_active, status=200).json_body  # nopep8
835
         assert res.json_body in active_contents
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
     def test_api__post_content_create_generic_content__err_400__empty_label(self) -> None:  # nopep8
880
     def test_api__post_content_create_generic_content__err_400__empty_label(self) -> None:  # nopep8
838
         """
881
         """
839
         Create generic content
882
         Create generic content

+ 5 - 0
tracim/views/core_api/schemas.py View File

371
         example='html-documents',
371
         example='html-documents',
372
         validate=OneOf(ContentType.allowed_types_for_folding()),  # nopep8
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
     @post_load
380
     @post_load
376
     def make_content_filter(self, data):
381
     def make_content_filter(self, data):

+ 12 - 1
tracim/views/core_api/workspace_controller.py View File

18
 from tracim.models.context_models import UserRoleWorkspaceInContext
18
 from tracim.models.context_models import UserRoleWorkspaceInContext
19
 from tracim.models.context_models import ContentInContext
19
 from tracim.models.context_models import ContentInContext
20
 from tracim.exceptions import EmptyLabelNotAllowed
20
 from tracim.exceptions import EmptyLabelNotAllowed
21
+from tracim.exceptions import ContentNotFound
21
 from tracim.exceptions import WorkspacesDoNotMatch
22
 from tracim.exceptions import WorkspacesDoNotMatch
23
+from tracim.exceptions import ParentNotFound
22
 from tracim.views.controllers import Controller
24
 from tracim.views.controllers import Controller
23
 from tracim.views.core_api.schemas import FilterContentQuerySchema
25
 from tracim.views.core_api.schemas import FilterContentQuerySchema
24
 from tracim.views.core_api.schemas import ContentMoveSchema
26
 from tracim.views.core_api.schemas import ContentMoveSchema
133
         api = ContentApi(
135
         api = ContentApi(
134
             current_user=request.current_user,
136
             current_user=request.current_user,
135
             session=request.dbsession,
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
         content = api.create(
148
         content = api.create(
139
             label=creation_data.label,
149
             label=creation_data.label,
140
             content_type=creation_data.content_type,
150
             content_type=creation_data.content_type,
141
             workspace=request.current_workspace,
151
             workspace=request.current_workspace,
152
+            parent=parent,
142
         )
153
         )
143
         api.save(content, ActionDescription.CREATION)
154
         api.save(content, ActionDescription.CREATION)
144
         content = api.get_content_in_context(content)
155
         content = api.get_content_in_context(content)