Przeglądaj źródła

refactor comment creation with subcontent check

Guénaël Muller 6 lat temu
rodzic
commit
5281fd4100

+ 19 - 15
backend/tracim_backend/lib/core/content.py Wyświetl plik

@@ -418,20 +418,23 @@ class ContentApi(object):
418 418
             workspace = parent.workspace
419 419
 
420 420
         content_type = CONTENT_TYPES.get_one_by_slug(content_type_slug)
421
-        if parent:
421
+        if parent and parent.properties and 'allowed_content' in parent.properties:
422 422
             if content_type.slug not in parent.properties['allowed_content'] or not parent.properties['allowed_content'][content_type.slug]:
423 423
                 raise UnallowedSubContent(' SubContent of type {subcontent_type}  not allowed in content {content_id}'.format(  # nopep8
424 424
                     subcontent_type=content_type.slug,
425 425
                     content_id=parent.content_id,
426 426
                 ))
427
+        if not workspace and parent:
428
+            workspace = parent.workspace
427 429
 
428
-        if content_type.slug not in workspace.get_allowed_content_types():
429
-            raise UnallowedSubContent(
430
-                ' SubContent of type {subcontent_type}  not allowed in workspace {content_id}'.format(  # nopep8
431
-                    subcontent_type=content_type.slug,
432
-                    content_id=workspace.workspace_id,
430
+        if workspace:
431
+            if content_type.slug not in workspace.get_allowed_content_types():
432
+                raise UnallowedSubContent(
433
+                    ' SubContent of type {subcontent_type}  not allowed in workspace {content_id}'.format(  # nopep8
434
+                        subcontent_type=content_type.slug,
435
+                        content_id=workspace.workspace_id,
436
+                    )
433 437
                 )
434
-            )
435 438
 
436 439
         content = Content()
437 440
 
@@ -473,15 +476,16 @@ class ContentApi(object):
473 476
         assert parent and parent.type != CONTENT_TYPES.Folder.slug
474 477
         if not content:
475 478
             raise EmptyCommentContentNotAllowed()
476
-        item = Content()
477
-        item.owner = self._user
478
-        item.parent = parent
479
-        if not workspace:
480
-            workspace = item.parent.workspace
481
-        item.workspace = workspace
482
-        item.type = CONTENT_TYPES.Comment.slug
479
+
480
+        item = self.create(
481
+            content_type_slug=CONTENT_TYPES.Comment.slug,
482
+            workspace=workspace,
483
+            parent=parent,
484
+            do_notify=False,
485
+            do_save=False,
486
+            label='',
487
+        )
483 488
         item.description = content
484
-        item.label = ''
485 489
         item.revision_type = ActionDescription.COMMENT
486 490
 
487 491
         if do_save:

+ 1 - 1
backend/tracim_backend/models/data.py Wyświetl plik

@@ -1181,7 +1181,7 @@ class Content(DeclarativeBase):
1181 1181
         else:
1182 1182
             properties = json.loads(self._properties)
1183 1183
         if CONTENT_TYPES.get_one_by_slug(self.type) != CONTENT_TYPES.Event:
1184
-            if not 'allowed_content' in self._properties:
1184
+            if self._properties and not 'allowed_content' in self._properties:
1185 1185
                 properties['allowed_content'] = CONTENT_TYPES.default_allowed_content_properties(self.type)
1186 1186
         return properties
1187 1187