Explorar el Código

refactor content status

Guénaël Muller hace 6 años
padre
commit
8b7a40289e

+ 2 - 2
backend/tracim_backend/lib/core/content.py Ver fichero

@@ -34,10 +34,10 @@ from tracim_backend.exceptions import EmptyLabelNotAllowed
34 34
 from tracim_backend.exceptions import ContentNotFound
35 35
 from tracim_backend.exceptions import WorkspacesDoNotMatch
36 36
 from tracim_backend.lib.utils.utils import current_date_for_filename
37
+from tracim_backend.models.contents import CONTENT_STATUS
37 38
 from tracim_backend.models.revision_protection import new_revision
38 39
 from tracim_backend.models.auth import User
39 40
 from tracim_backend.models.data import ActionDescription
40
-from tracim_backend.models.data import ContentStatus
41 41
 from tracim_backend.models.data import ContentRevisionRO
42 42
 from tracim_backend.models.data import Content
43 43
 from tracim_backend.models.data import ContentType
@@ -1121,7 +1121,7 @@ class ContentApi(object):
1121 1121
         folder.properties = properties
1122 1122
 
1123 1123
     def set_status(self, content: Content, new_status: str):
1124
-        if new_status in ContentStatus.allowed_values():
1124
+        if new_status in CONTENT_STATUS.allowed_slugs_values():
1125 1125
             content.status = new_status
1126 1126
             content.revision_type = ActionDescription.STATUS_UPDATE
1127 1127
         else:

+ 37 - 45
backend/tracim_backend/models/contents.py Ver fichero

@@ -19,9 +19,9 @@ class GlobalStatus(Enum):
19 19
     CLOSED = 'closed'
20 20
 
21 21
 
22
-class NewContentStatus(object):
22
+class ContentStatus(object):
23 23
     """
24
-    Future ContentStatus object class
24
+    ContentStatus object class
25 25
     """
26 26
     def __init__(
27 27
             self,
@@ -38,7 +38,7 @@ class NewContentStatus(object):
38 38
         self.hexcolor = hexcolor
39 39
 
40 40
 
41
-open_status = NewContentStatus(
41
+open_status = ContentStatus(
42 42
     slug='open',
43 43
     global_status=GlobalStatus.OPEN.value,
44 44
     label='Open',
@@ -46,7 +46,7 @@ open_status = NewContentStatus(
46 46
     hexcolor='#3f52e3',
47 47
 )
48 48
 
49
-closed_validated_status = NewContentStatus(
49
+closed_validated_status = ContentStatus(
50 50
     slug='closed-validated',
51 51
     global_status=GlobalStatus.CLOSED.value,
52 52
     label='Validated',
@@ -54,7 +54,7 @@ closed_validated_status = NewContentStatus(
54 54
     hexcolor='#008000',
55 55
 )
56 56
 
57
-closed_unvalidated_status = NewContentStatus(
57
+closed_unvalidated_status = ContentStatus(
58 58
     slug='closed-unvalidated',
59 59
     global_status=GlobalStatus.CLOSED.value,
60 60
     label='Cancelled',
@@ -62,7 +62,7 @@ closed_unvalidated_status = NewContentStatus(
62 62
     hexcolor='#f63434',
63 63
 )
64 64
 
65
-closed_deprecated_status = NewContentStatus(
65
+closed_deprecated_status = ContentStatus(
66 66
     slug='closed-deprecated',
67 67
     global_status=GlobalStatus.CLOSED.value,
68 68
     label='Deprecated',
@@ -71,45 +71,37 @@ closed_deprecated_status = NewContentStatus(
71 71
 )
72 72
 
73 73
 
74
-CONTENT_DEFAULT_STATUS = [
75
-    open_status,
76
-    closed_validated_status,
77
-    closed_unvalidated_status,
78
-    closed_deprecated_status,
79
-]
74
+class ContentStatusList(object):
80 75
 
76
+    OPEN = open_status
81 77
 
82
-class ContentStatusLegacy(NewContentStatus):
83
-    """
84
-    Temporary remplacement object for Legacy ContentStatus Object
85
-    """
86
-    OPEN = open_status.slug
87
-    CLOSED_VALIDATED = closed_validated_status.slug
88
-    CLOSED_UNVALIDATED = closed_unvalidated_status.slug
89
-    CLOSED_DEPRECATED = closed_deprecated_status.slug
78
+    def __init__(self, extend_content_status: typing.List[ContentStatus]):
79
+        self._content_status = [self.OPEN]
80
+        self._content_status.extend(extend_content_status)
90 81
 
91
-    def __init__(self, slug: str):
92
-        for status in CONTENT_DEFAULT_STATUS:
93
-            if slug == status.slug:
94
-                super(ContentStatusLegacy, self).__init__(
95
-                    slug=status.slug,
96
-                    global_status=status.global_status,
97
-                    label=status.label,
98
-                    fa_icon=status.fa_icon,
99
-                    hexcolor=status.hexcolor,
100
-                )
101
-                return
82
+    def get_one_by_slug(self, slug: str) -> ContentStatus:
83
+        for item in self._content_status:
84
+            if item.slug == slug:
85
+                return item
102 86
         raise ContentStatusNotExist()
103 87
 
104
-    @classmethod
105
-    def all(cls, type='') -> ['NewContentStatus']:
106
-        return CONTENT_DEFAULT_STATUS
88
+    def allowed_slugs_values(self) -> typing.List[str]:
89
+        return [item.slug for item in self._content_status]
107 90
 
108
-    @classmethod
109
-    def allowed_values(cls):
110
-        return [status.slug for status in CONTENT_DEFAULT_STATUS]
91
+    def allowed(self) -> typing.List[ContentStatus]:
92
+        return [item for item in self._content_status]
111 93
 
94
+    def get_default_status(self) -> ContentStatus:
95
+        return self.OPEN
112 96
 
97
+
98
+CONTENT_STATUS = ContentStatusList(
99
+    [
100
+        closed_validated_status,
101
+        closed_unvalidated_status,
102
+        closed_deprecated_status,
103
+    ]
104
+)
113 105
 ####
114 106
 # ContentType
115 107
 
@@ -125,7 +117,7 @@ class NewContentType(object):
125 117
             hexcolor: str,
126 118
             label: str,
127 119
             creation_label: str,
128
-            available_statuses: typing.List[NewContentStatus],
120
+            available_statuses: typing.List[ContentStatus],
129 121
 
130 122
     ):
131 123
         self.slug = slug
@@ -142,7 +134,7 @@ thread_type = NewContentType(
142 134
     hexcolor=thread.hexcolor,
143 135
     label='Thread',
144 136
     creation_label='Discuss about a topic',
145
-    available_statuses=CONTENT_DEFAULT_STATUS,
137
+    available_statuses=CONTENT_STATUS.allowed(),
146 138
 )
147 139
 
148 140
 file_type = NewContentType(
@@ -151,7 +143,7 @@ file_type = NewContentType(
151 143
     hexcolor=_file.hexcolor,
152 144
     label='File',
153 145
     creation_label='Upload a file',
154
-    available_statuses=CONTENT_DEFAULT_STATUS,
146
+    available_statuses=CONTENT_STATUS.allowed(),
155 147
 )
156 148
 
157 149
 markdownpluspage_type = NewContentType(
@@ -160,7 +152,7 @@ markdownpluspage_type = NewContentType(
160 152
     hexcolor=markdownpluspage.hexcolor,
161 153
     label='Rich Markdown File',
162 154
     creation_label='Create a Markdown document',
163
-    available_statuses=CONTENT_DEFAULT_STATUS,
155
+    available_statuses=CONTENT_STATUS.allowed(),
164 156
 )
165 157
 
166 158
 html_documents_type = NewContentType(
@@ -169,7 +161,7 @@ html_documents_type = NewContentType(
169 161
     hexcolor=html_documents.hexcolor,
170 162
     label='Text Document',
171 163
     creation_label='Write a document',
172
-    available_statuses=CONTENT_DEFAULT_STATUS,
164
+    available_statuses=CONTENT_STATUS.allowed(),
173 165
 )
174 166
 
175 167
 # TODO - G.M - 31-05-2018 - Set Better folder params
@@ -179,7 +171,7 @@ folder_type = NewContentType(
179 171
     hexcolor=thread.hexcolor,
180 172
     label='Folder',
181 173
     creation_label='Create collection of any documents',
182
-    available_statuses=CONTENT_DEFAULT_STATUS,
174
+    available_statuses=CONTENT_STATUS.allowed(),
183 175
 )
184 176
 
185 177
 CONTENT_DEFAULT_TYPE = [
@@ -197,7 +189,7 @@ event_type = NewContentType(
197 189
     hexcolor=thread.hexcolor,
198 190
     label='Event',
199 191
     creation_label='Event',
200
-    available_statuses=CONTENT_DEFAULT_STATUS,
192
+    available_statuses=CONTENT_STATUS.allowed(),
201 193
 )
202 194
 
203 195
 # TODO - G.M - 31-05-2018 - Set Better Event params
@@ -207,7 +199,7 @@ comment_type = NewContentType(
207 199
     hexcolor=thread.hexcolor,
208 200
     label='Comment',
209 201
     creation_label='Comment',
210
-    available_statuses=CONTENT_DEFAULT_STATUS,
202
+    available_statuses=CONTENT_STATUS.allowed(),
211 203
 )
212 204
 
213 205
 CONTENT_DEFAULT_TYPE_SPECIAL = [

+ 5 - 6
backend/tracim_backend/models/data.py Ver fichero

@@ -291,7 +291,8 @@ class ActionDescription(object):
291 291
                 ]
292 292
 
293 293
 
294
-from tracim_backend.models.contents import ContentStatusLegacy as ContentStatus
294
+from tracim_backend.models.contents import CONTENT_STATUS
295
+from tracim_backend.models.contents import ContentStatus
295 296
 from tracim_backend.models.contents import ContentTypeLegacy as ContentType
296 297
 # TODO - G.M - 30-05-2018 - Drop this old code when whe are sure nothing
297 298
 # is lost .
@@ -616,7 +617,7 @@ class ContentRevisionRO(DeclarativeBase):
616 617
     properties = Column('properties', Text(), unique=False, nullable=False, default='')
617 618
 
618 619
     type = Column(Unicode(32), unique=False, nullable=False)
619
-    status = Column(Unicode(32), unique=False, nullable=False, default=ContentStatus.OPEN)
620
+    status = Column(Unicode(32), unique=False, nullable=False, default=str(CONTENT_STATUS.get_default_status().slug))
620 621
     created = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
621 622
     updated = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
622 623
     is_deleted = Column(Boolean, unique=False, nullable=False, default=False)
@@ -754,7 +755,7 @@ class ContentRevisionRO(DeclarativeBase):
754 755
         super().__setattr__(key, value)
755 756
 
756 757
     def get_status(self) -> ContentStatus:
757
-        return ContentStatus(self.status)
758
+        return CONTENT_STATUS.get_one_by_slug(self.status)
758 759
 
759 760
     def get_label(self) -> str:
760 761
         return self.label or self.file_name or ''
@@ -1247,10 +1248,8 @@ class Content(DeclarativeBase):
1247 1248
         return self.revision.get_label_as_file()
1248 1249
 
1249 1250
     def get_status(self) -> ContentStatus:
1250
-        return ContentStatus(
1251
+        return CONTENT_STATUS.get_one_by_slug(
1251 1252
             self.status,
1252
-            # TODO - G.M - 10-04-2018 - [Cleanup] Drop this
1253
-            # self.type.__str__()
1254 1253
         )
1255 1254
 
1256 1255
     def get_last_action(self) -> ActionDescription:

+ 3 - 4
backend/tracim_backend/views/core_api/schemas.py Ver fichero

@@ -6,10 +6,9 @@ from marshmallow.validate import Range
6 6
 
7 7
 from tracim_backend.lib.utils.utils import DATETIME_FORMAT
8 8
 from tracim_backend.models.auth import Profile
9
-from tracim_backend.models.contents import GlobalStatus
9
+from tracim_backend.models.contents import GlobalStatus, CONTENT_STATUS
10 10
 from tracim_backend.models.contents import open_status
11 11
 from tracim_backend.models.contents import ContentTypeLegacy as ContentType
12
-from tracim_backend.models.contents import ContentStatusLegacy as ContentStatus
13 12
 from tracim_backend.models.context_models import ActiveContentFilter
14 13
 from tracim_backend.models.context_models import ContentIdsQuery
15 14
 from tracim_backend.models.context_models import UserWorkspaceAndContentPath
@@ -691,7 +690,7 @@ class ContentDigestSchema(marshmallow.Schema):
691 690
     )
692 691
     status = marshmallow.fields.Str(
693 692
         example='closed-deprecated',
694
-        validate=OneOf(ContentStatus.allowed_values()),
693
+        validate=OneOf(CONTENT_STATUS.allowed_slugs_values()),
695 694
         description='this slug is found in content_type available statuses',
696 695
         default=open_status
697 696
     )
@@ -835,7 +834,7 @@ class FileContentModifySchema(TextBasedContentModifySchema):
835 834
 class SetContentStatusSchema(marshmallow.Schema):
836 835
     status = marshmallow.fields.Str(
837 836
         example='closed-deprecated',
838
-        validate=OneOf(ContentStatus.allowed_values()),
837
+        validate=OneOf(CONTENT_STATUS.allowed_slugs_values()),
839 838
         description='this slug is found in content_type available statuses',
840 839
         default=open_status,
841 840
         required=True,