Parcourir la source

add content_filter to workspace_content endpoint

Guénaël Muller il y a 6 ans
Parent
révision
2471e19b3c

+ 2 - 2
tracim/lib/core/content.py Voir le fichier

@@ -713,8 +713,8 @@ class ContentApi(object):
713 713
 
714 714
     def get_all(self, parent_id: int=None, content_type: str=ContentType.Any, workspace: Workspace=None) -> typing.List[Content]:
715 715
         assert parent_id is None or isinstance(parent_id, int) # DYN_REMOVE
716
-        assert content_type is not None# DYN_REMOVE
717
-        assert isinstance(content_type, str) # DYN_REMOVE
716
+        if not content_type:
717
+            content_type = ContentType.Any
718 718
 
719 719
         resultset = self._base_query(workspace)
720 720
 

+ 9 - 0
tracim/models/contents.py Voir le fichier

@@ -261,6 +261,15 @@ class ContentTypeLegacy(NewContentType):
261 261
         return contents_types
262 262
 
263 263
     @classmethod
264
+    def allowed_type_values(cls) -> typing.List[str]:
265
+        """
266
+        All content type slug + special values like any
267
+        """
268
+        content_types = cls.allowed_types()
269
+        content_types.append(ContentTypeLegacy.Any)
270
+        return content_types
271
+
272
+    @classmethod
264 273
     def allowed_types_for_folding(cls):
265 274
         # This method is used for showing only "main"
266 275
         # types in the left-side treeview

+ 2 - 0
tracim/models/context_models.py Voir le fichier

@@ -68,11 +68,13 @@ class ContentFilter(object):
68 68
             show_archived: int = 0,
69 69
             show_deleted: int = 0,
70 70
             show_active: int = 1,
71
+            content_type: str = None,
71 72
     ) -> None:
72 73
         self.parent_id = parent_id
73 74
         self.show_archived = bool(show_archived)
74 75
         self.show_deleted = bool(show_deleted)
75 76
         self.show_active = bool(show_active)
77
+        self.content_type = content_type
76 78
 
77 79
 
78 80
 class ContentCreation(object):

+ 34 - 0
tracim/tests/functional/test_workspaces.py Voir le fichier

@@ -241,6 +241,7 @@ class TestWorkspaceContents(FunctionalTest):
241 241
         assert len(res) == 3
242 242
         content = res[0]
243 243
         assert content['content_id'] == 1
244
+        assert content['content_type'] == 'folder'
244 245
         assert content['is_archived'] is False
245 246
         assert content['is_deleted'] is False
246 247
         assert content['label'] == 'Tools'
@@ -252,6 +253,7 @@ class TestWorkspaceContents(FunctionalTest):
252 253
         assert content['workspace_id'] == 1
253 254
         content = res[1]
254 255
         assert content['content_id'] == 2
256
+        assert content['content_type'] == 'folder'
255 257
         assert content['is_archived'] is False
256 258
         assert content['is_deleted'] is False
257 259
         assert content['label'] == 'Menus'
@@ -263,6 +265,37 @@ class TestWorkspaceContents(FunctionalTest):
263 265
         assert content['workspace_id'] == 1
264 266
         content = res[2]
265 267
         assert content['content_id'] == 11
268
+        assert content['content_type'] == 'html-documents'
269
+        assert content['is_archived'] is False
270
+        assert content['is_deleted'] is False
271
+        assert content['label'] == 'Current Menu'
272
+        assert content['parent_id'] == 2
273
+        assert content['show_in_ui'] is True
274
+        assert content['slug'] == 'current-menu'
275
+        assert content['status'] == 'open'
276
+        assert set(content['sub_content_types']) == {'thread', 'html-documents', 'folder', 'file'}  # nopep8
277
+        assert content['workspace_id'] == 1
278
+
279
+    def test_api__get_workspace_content__ok_200__get_default_html_documents(self):
280
+        """
281
+        Check obtain workspace contents with defaults filters + content_filter
282
+        """
283
+        self.testapp.authorization = (
284
+            'Basic',
285
+            (
286
+                'admin@admin.admin',
287
+                'admin@admin.admin'
288
+            )
289
+        )
290
+        params = {
291
+            'content_type': 'html-documents',
292
+        }
293
+        res = self.testapp.get('/api/v2/workspaces/1/contents', status=200, params=params).json_body   # nopep8
294
+        assert len(res) == 1
295
+        content = res[0]
296
+        assert content
297
+        assert content['content_id'] == 11
298
+        assert content['content_type'] == 'html-documents'
266 299
         assert content['is_archived'] is False
267 300
         assert content['is_deleted'] is False
268 301
         assert content['label'] == 'Current Menu'
@@ -549,6 +582,7 @@ class TestWorkspaceContents(FunctionalTest):
549 582
             'show_archived': 1,
550 583
             'show_deleted': 1,
551 584
             'show_active': 1,
585
+            'content_type': 'any'
552 586
         }
553 587
         self.testapp.authorization = (
554 588
             'Basic',

+ 5 - 0
tracim/views/core_api/schemas.py Voir le fichier

@@ -142,6 +142,11 @@ class FilterContentQuerySchema(marshmallow.Schema):
142 142
                     'The reason for this parameter to exist is for example '
143 143
                     'to allow to show only archived documents'
144 144
     )
145
+    content_type = marshmallow.fields.String(
146
+        example=ContentType.Any,
147
+        default=ContentType.Any,
148
+        validate=OneOf(ContentType.allowed_type_values())
149
+    )
145 150
 
146 151
     @post_load
147 152
     def make_content_filter(self, data):

+ 1 - 0
tracim/views/core_api/workspace_controller.py Voir le fichier

@@ -107,6 +107,7 @@ class WorkspaceController(Controller):
107 107
         contents = api.get_all(
108 108
             parent_id=content_filter.parent_id,
109 109
             workspace=request.current_workspace,
110
+            content_type=content_filter.content_type,
110 111
         )
111 112
         contents = [
112 113
             api.get_content_in_context(content) for content in contents