Browse Source

refactor content status

Guénaël Muller 6 years ago
parent
commit
8b7a40289e

+ 2 - 2
backend/tracim_backend/lib/core/content.py View File

34
 from tracim_backend.exceptions import ContentNotFound
34
 from tracim_backend.exceptions import ContentNotFound
35
 from tracim_backend.exceptions import WorkspacesDoNotMatch
35
 from tracim_backend.exceptions import WorkspacesDoNotMatch
36
 from tracim_backend.lib.utils.utils import current_date_for_filename
36
 from tracim_backend.lib.utils.utils import current_date_for_filename
37
+from tracim_backend.models.contents import CONTENT_STATUS
37
 from tracim_backend.models.revision_protection import new_revision
38
 from tracim_backend.models.revision_protection import new_revision
38
 from tracim_backend.models.auth import User
39
 from tracim_backend.models.auth import User
39
 from tracim_backend.models.data import ActionDescription
40
 from tracim_backend.models.data import ActionDescription
40
-from tracim_backend.models.data import ContentStatus
41
 from tracim_backend.models.data import ContentRevisionRO
41
 from tracim_backend.models.data import ContentRevisionRO
42
 from tracim_backend.models.data import Content
42
 from tracim_backend.models.data import Content
43
 from tracim_backend.models.data import ContentType
43
 from tracim_backend.models.data import ContentType
1121
         folder.properties = properties
1121
         folder.properties = properties
1122
 
1122
 
1123
     def set_status(self, content: Content, new_status: str):
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
             content.status = new_status
1125
             content.status = new_status
1126
             content.revision_type = ActionDescription.STATUS_UPDATE
1126
             content.revision_type = ActionDescription.STATUS_UPDATE
1127
         else:
1127
         else:

+ 37 - 45
backend/tracim_backend/models/contents.py View File

19
     CLOSED = 'closed'
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
     def __init__(
26
     def __init__(
27
             self,
27
             self,
38
         self.hexcolor = hexcolor
38
         self.hexcolor = hexcolor
39
 
39
 
40
 
40
 
41
-open_status = NewContentStatus(
41
+open_status = ContentStatus(
42
     slug='open',
42
     slug='open',
43
     global_status=GlobalStatus.OPEN.value,
43
     global_status=GlobalStatus.OPEN.value,
44
     label='Open',
44
     label='Open',
46
     hexcolor='#3f52e3',
46
     hexcolor='#3f52e3',
47
 )
47
 )
48
 
48
 
49
-closed_validated_status = NewContentStatus(
49
+closed_validated_status = ContentStatus(
50
     slug='closed-validated',
50
     slug='closed-validated',
51
     global_status=GlobalStatus.CLOSED.value,
51
     global_status=GlobalStatus.CLOSED.value,
52
     label='Validated',
52
     label='Validated',
54
     hexcolor='#008000',
54
     hexcolor='#008000',
55
 )
55
 )
56
 
56
 
57
-closed_unvalidated_status = NewContentStatus(
57
+closed_unvalidated_status = ContentStatus(
58
     slug='closed-unvalidated',
58
     slug='closed-unvalidated',
59
     global_status=GlobalStatus.CLOSED.value,
59
     global_status=GlobalStatus.CLOSED.value,
60
     label='Cancelled',
60
     label='Cancelled',
62
     hexcolor='#f63434',
62
     hexcolor='#f63434',
63
 )
63
 )
64
 
64
 
65
-closed_deprecated_status = NewContentStatus(
65
+closed_deprecated_status = ContentStatus(
66
     slug='closed-deprecated',
66
     slug='closed-deprecated',
67
     global_status=GlobalStatus.CLOSED.value,
67
     global_status=GlobalStatus.CLOSED.value,
68
     label='Deprecated',
68
     label='Deprecated',
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
         raise ContentStatusNotExist()
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
 # ContentType
106
 # ContentType
115
 
107
 
125
             hexcolor: str,
117
             hexcolor: str,
126
             label: str,
118
             label: str,
127
             creation_label: str,
119
             creation_label: str,
128
-            available_statuses: typing.List[NewContentStatus],
120
+            available_statuses: typing.List[ContentStatus],
129
 
121
 
130
     ):
122
     ):
131
         self.slug = slug
123
         self.slug = slug
142
     hexcolor=thread.hexcolor,
134
     hexcolor=thread.hexcolor,
143
     label='Thread',
135
     label='Thread',
144
     creation_label='Discuss about a topic',
136
     creation_label='Discuss about a topic',
145
-    available_statuses=CONTENT_DEFAULT_STATUS,
137
+    available_statuses=CONTENT_STATUS.allowed(),
146
 )
138
 )
147
 
139
 
148
 file_type = NewContentType(
140
 file_type = NewContentType(
151
     hexcolor=_file.hexcolor,
143
     hexcolor=_file.hexcolor,
152
     label='File',
144
     label='File',
153
     creation_label='Upload a file',
145
     creation_label='Upload a file',
154
-    available_statuses=CONTENT_DEFAULT_STATUS,
146
+    available_statuses=CONTENT_STATUS.allowed(),
155
 )
147
 )
156
 
148
 
157
 markdownpluspage_type = NewContentType(
149
 markdownpluspage_type = NewContentType(
160
     hexcolor=markdownpluspage.hexcolor,
152
     hexcolor=markdownpluspage.hexcolor,
161
     label='Rich Markdown File',
153
     label='Rich Markdown File',
162
     creation_label='Create a Markdown document',
154
     creation_label='Create a Markdown document',
163
-    available_statuses=CONTENT_DEFAULT_STATUS,
155
+    available_statuses=CONTENT_STATUS.allowed(),
164
 )
156
 )
165
 
157
 
166
 html_documents_type = NewContentType(
158
 html_documents_type = NewContentType(
169
     hexcolor=html_documents.hexcolor,
161
     hexcolor=html_documents.hexcolor,
170
     label='Text Document',
162
     label='Text Document',
171
     creation_label='Write a document',
163
     creation_label='Write a document',
172
-    available_statuses=CONTENT_DEFAULT_STATUS,
164
+    available_statuses=CONTENT_STATUS.allowed(),
173
 )
165
 )
174
 
166
 
175
 # TODO - G.M - 31-05-2018 - Set Better folder params
167
 # TODO - G.M - 31-05-2018 - Set Better folder params
179
     hexcolor=thread.hexcolor,
171
     hexcolor=thread.hexcolor,
180
     label='Folder',
172
     label='Folder',
181
     creation_label='Create collection of any documents',
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
 CONTENT_DEFAULT_TYPE = [
177
 CONTENT_DEFAULT_TYPE = [
197
     hexcolor=thread.hexcolor,
189
     hexcolor=thread.hexcolor,
198
     label='Event',
190
     label='Event',
199
     creation_label='Event',
191
     creation_label='Event',
200
-    available_statuses=CONTENT_DEFAULT_STATUS,
192
+    available_statuses=CONTENT_STATUS.allowed(),
201
 )
193
 )
202
 
194
 
203
 # TODO - G.M - 31-05-2018 - Set Better Event params
195
 # TODO - G.M - 31-05-2018 - Set Better Event params
207
     hexcolor=thread.hexcolor,
199
     hexcolor=thread.hexcolor,
208
     label='Comment',
200
     label='Comment',
209
     creation_label='Comment',
201
     creation_label='Comment',
210
-    available_statuses=CONTENT_DEFAULT_STATUS,
202
+    available_statuses=CONTENT_STATUS.allowed(),
211
 )
203
 )
212
 
204
 
213
 CONTENT_DEFAULT_TYPE_SPECIAL = [
205
 CONTENT_DEFAULT_TYPE_SPECIAL = [

+ 5 - 6
backend/tracim_backend/models/data.py View File

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

+ 3 - 4
backend/tracim_backend/views/core_api/schemas.py View File

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