Browse Source

use always contentType and contentStatus class + accept special(comment,event) content_type in output schema

Guénaël Muller 6 years ago
parent
commit
a7de214c88
2 changed files with 20 additions and 15 deletions
  1. 16 12
      tracim/views/core_api/schemas.py
  2. 4 3
      tracim/views/core_api/system_controller.py

+ 16 - 12
tracim/views/core_api/schemas.py View File

2
 import marshmallow
2
 import marshmallow
3
 from marshmallow import post_load
3
 from marshmallow import post_load
4
 from marshmallow.validate import OneOf
4
 from marshmallow.validate import OneOf
5
+from marshmallow.validate import Equal
5
 
6
 
6
 from tracim.lib.utils.utils import DATETIME_FORMAT
7
 from tracim.lib.utils.utils import DATETIME_FORMAT
7
 from tracim.models.auth import Profile
8
 from tracim.models.auth import Profile
8
-from tracim.models.contents import CONTENT_DEFAULT_TYPE
9
-from tracim.models.contents import CONTENT_DEFAULT_STATUS
10
 from tracim.models.contents import GlobalStatus
9
 from tracim.models.contents import GlobalStatus
11
 from tracim.models.contents import open_status
10
 from tracim.models.contents import open_status
11
+from tracim.models.contents import ContentTypeLegacy as ContentType
12
+from tracim.models.contents import ContentStatusLegacy as ContentStatus
12
 from tracim.models.context_models import ContentCreation
13
 from tracim.models.context_models import ContentCreation
13
 from tracim.models.context_models import SetContentStatus
14
 from tracim.models.context_models import SetContentStatus
14
 from tracim.models.context_models import CommentCreation
15
 from tracim.models.context_models import CommentCreation
286
 class ContentTypeSchema(marshmallow.Schema):
287
 class ContentTypeSchema(marshmallow.Schema):
287
     slug = marshmallow.fields.String(
288
     slug = marshmallow.fields.String(
288
         example='pagehtml',
289
         example='pagehtml',
289
-        validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
290
+        validate=OneOf(ContentType.allowed_types()),
290
     )
291
     )
291
     fa_icon = marshmallow.fields.String(
292
     fa_icon = marshmallow.fields.String(
292
         example='fa-file-text-o',
293
         example='fa-file-text-o',
338
     )
339
     )
339
     content_type = marshmallow.fields.String(
340
     content_type = marshmallow.fields.String(
340
         example='html-documents',
341
         example='html-documents',
341
-        validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
342
+        validate=OneOf(ContentType.allowed_types_for_folding()),  # nopep8
342
     )
343
     )
343
 
344
 
344
     @post_load
345
     @post_load
360
     label = marshmallow.fields.Str(example='Intervention Report 12')
361
     label = marshmallow.fields.Str(example='Intervention Report 12')
361
     content_type = marshmallow.fields.Str(
362
     content_type = marshmallow.fields.Str(
362
         example='html-documents',
363
         example='html-documents',
363
-        validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
364
+        validate=OneOf(ContentType.allowed_types()),
364
     )
365
     )
365
     sub_content_types = marshmallow.fields.List(
366
     sub_content_types = marshmallow.fields.List(
366
-        marshmallow.fields.String(),
367
+        marshmallow.fields.String(
368
+            example='html-content',
369
+            validate=OneOf(ContentType.allowed_types())
370
+        ),
367
         description='list of content types allowed as sub contents. '
371
         description='list of content types allowed as sub contents. '
368
                     'This field is required for folder contents, '
372
                     'This field is required for folder contents, '
369
                     'set it to empty list in other cases'
373
                     'set it to empty list in other cases'
370
     )
374
     )
371
     status = marshmallow.fields.Str(
375
     status = marshmallow.fields.Str(
372
         example='closed-deprecated',
376
         example='closed-deprecated',
373
-        validate=OneOf([status.slug for status in CONTENT_DEFAULT_STATUS]),
377
+        validate=OneOf(ContentStatus.allowed_values()),
374
         description='this slug is found in content_type available statuses',
378
         description='this slug is found in content_type available statuses',
375
         default=open_status
379
         default=open_status
376
     )
380
     )
406
 class ThreadContentSchema(ContentSchema):
410
 class ThreadContentSchema(ContentSchema):
407
     content_type = marshmallow.fields.Str(
411
     content_type = marshmallow.fields.Str(
408
         example='thread',
412
         example='thread',
409
-        validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
413
+        validate=Equal(ContentType('thread').slug)
410
     )
414
     )
411
     raw_content = marshmallow.fields.String('Description of Thread')
415
     raw_content = marshmallow.fields.String('Description of Thread')
412
 
416
 
414
 class HtmlDocumentContentSchema(ContentSchema):
418
 class HtmlDocumentContentSchema(ContentSchema):
415
     content_type = marshmallow.fields.Str(
419
     content_type = marshmallow.fields.Str(
416
         example='html-documents',
420
         example='html-documents',
417
-        validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
421
+        validate=Equal(ContentType('html-documents').slug),
418
     )
422
     )
419
     raw_content = marshmallow.fields.String('<p>Html page Content!</p>')
423
     raw_content = marshmallow.fields.String('<p>Html page Content!</p>')
420
 
424
 
436
 class ThreadRevisionSchema(RevisionSchema):
440
 class ThreadRevisionSchema(RevisionSchema):
437
     content_type = marshmallow.fields.Str(
441
     content_type = marshmallow.fields.Str(
438
         example='thread',
442
         example='thread',
439
-        validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
443
+        validate=Equal(ContentType('thread').slug),
440
     )
444
     )
441
     raw_content = marshmallow.fields.String('Description of Thread')
445
     raw_content = marshmallow.fields.String('Description of Thread')
442
 
446
 
444
 class HtmlDocumentRevisionSchema(RevisionSchema):
448
 class HtmlDocumentRevisionSchema(RevisionSchema):
445
     content_type = marshmallow.fields.Str(
449
     content_type = marshmallow.fields.Str(
446
         example='html-documents',
450
         example='html-documents',
447
-        validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
451
+        validate=Equal(ContentType('html-documents').slug),
448
     )
452
     )
449
     raw_content = marshmallow.fields.String('<p>Html page Content!</p>')
453
     raw_content = marshmallow.fields.String('<p>Html page Content!</p>')
450
 
454
 
500
 class SetContentStatusSchema(marshmallow.Schema):
504
 class SetContentStatusSchema(marshmallow.Schema):
501
     status = marshmallow.fields.Str(
505
     status = marshmallow.fields.Str(
502
         example='closed-deprecated',
506
         example='closed-deprecated',
503
-        validate=OneOf([status.slug for status in CONTENT_DEFAULT_STATUS]),
507
+        validate=OneOf(ContentStatus.allowed_values()),
504
         description='this slug is found in content_type available statuses',
508
         description='this slug is found in content_type available statuses',
505
         default=open_status,
509
         default=open_status,
506
         required=True,
510
         required=True,

+ 4 - 3
tracim/views/core_api/system_controller.py View File

5
 from tracim.lib.utils.authorization import require_profile
5
 from tracim.lib.utils.authorization import require_profile
6
 from tracim.models import Group
6
 from tracim.models import Group
7
 from tracim.models.applications import applications
7
 from tracim.models.applications import applications
8
-from tracim.models.contents import CONTENT_DEFAULT_TYPE
8
+from tracim.models.contents import ContentTypeLegacy as ContentType
9
 
9
 
10
 try:  # Python 3.5+
10
 try:  # Python 3.5+
11
     from http import HTTPStatus
11
     from http import HTTPStatus
42
         """
42
         """
43
         Get list of alls content types availables in this tracim instance.
43
         Get list of alls content types availables in this tracim instance.
44
         """
44
         """
45
-
46
-        return CONTENT_DEFAULT_TYPE
45
+        content_types_slugs = ContentType.allowed_types_for_folding()
46
+        content_types = [ContentType(slug) for slug in content_types_slugs]
47
+        return content_types
47
 
48
 
48
     def bind(self, configurator: Configurator) -> None:
49
     def bind(self, configurator: Configurator) -> None:
49
         """
50
         """