Browse Source

Calendar: refactorise Content population

Bastien Sevajol (Algoo) 8 years ago
parent
commit
d0b84d563a
1 changed files with 65 additions and 58 deletions
  1. 65 58
      tracim/tracim/lib/calendar.py

+ 65 - 58
tracim/tracim/lib/calendar.py View File

@@ -114,33 +114,31 @@ class CalendarManager(object):
114 114
         :param owner: Event Owner
115 115
         :return: Created Content
116 116
         """
117
+        workspace = None
117 118
         if isinstance(calendar, WorkspaceCalendar):
118
-            content = ContentApi(owner).create(
119
-                content_type=ContentType.Event,
120
-                workspace=calendar.related_object,
121
-                label=event.get('summary'),
122
-                do_save=False
123
-            )
124
-            content.description = event.get('description')
125
-            content.properties = {
126
-                'name': event_name,
127
-                'location': event.get('location'),
128
-                'raw': event.to_ical().decode("utf-8"),
129
-                'start': event.get('dtend').dt.strftime('%Y-%m-%d %H:%M:%S%z'),
130
-                'end': event.get('dtstart').dt.strftime('%Y-%m-%d %H:%M:%S%z'),
131
-            }
132
-            content.revision_type = ActionDescription.CREATION
133
-            DBSession.add(content)
134
-            DBSession.flush()
135
-            transaction.commit()
136
-            return content
137
-
138
-        if isinstance(calendar, UserCalendar):
139
-            # TODO - 20160531 - Bastien: UserCalendar currently not managed
140
-            raise NotImplementedError()
141
-
142
-        raise UnknownCalendarType('Type "{0}" is not implemented'
143
-                                  .format(type(calendar)))
119
+            workspace = calendar.related_object
120
+        elif isinstance(calendar, UserCalendar):
121
+            pass
122
+        else:
123
+            raise UnknownCalendarType('Type "{0}" is not implemented'
124
+                                      .format(type(calendar)))
125
+
126
+        content = ContentApi(owner).create(
127
+            content_type=ContentType.Event,
128
+            workspace=workspace,
129
+            do_save=False
130
+        )
131
+        self.populate_content_with_event(
132
+            content,
133
+            event,
134
+            event_name
135
+        )
136
+        content.revision_type = ActionDescription.CREATION
137
+        DBSession.add(content)
138
+        DBSession.flush()
139
+        transaction.commit()
140
+
141
+        return content
144 142
 
145 143
     def update_event(
146 144
             self,
@@ -158,41 +156,34 @@ class CalendarManager(object):
158 156
         :param current_user: Current modification asking user
159 157
         :return: Updated Content
160 158
         """
159
+        workspace = None
161 160
         if isinstance(calendar, WorkspaceCalendar):
162
-            content_api = ContentApi(current_user, force_show_all_types=True)
163
-            content = content_api.find_one_by_unique_property(
164
-                property_name='name',
165
-                property_value=event_name,
166
-                workspace=calendar.related_object
161
+            workspace = calendar.related_object
162
+        elif isinstance(calendar, UserCalendar):
163
+            pass
164
+        else:
165
+            raise UnknownCalendarType('Type "{0}" is not implemented'
166
+                                      .format(type(calendar)))
167
+
168
+        content_api = ContentApi(current_user, force_show_all_types=True)
169
+        content = content_api.find_one_by_unique_property(
170
+            property_name='name',
171
+            property_value=event_name,
172
+            workspace=workspace
173
+        )
174
+
175
+        with new_revision(content):
176
+            self.populate_content_with_event(
177
+                content,
178
+                event,
179
+                event_name
167 180
             )
181
+            content.revision_type = ActionDescription.EDITION
182
+
183
+        DBSession.flush()
184
+        transaction.commit()
168 185
 
169
-            with new_revision(content):
170
-                content.label = event.get('summary')
171
-                content.description = event.get('description')
172
-                content.properties = {
173
-                    'name': event_name,
174
-                    'location': event.get('location'),
175
-                    'raw': event.to_ical().decode("utf-8"),
176
-                    'start': event.get('dtend').dt.strftime(
177
-                        '%Y-%m-%d %H:%M:%S%z'
178
-                    ),
179
-                    'end': event.get('dtstart').dt.strftime(
180
-                        '%Y-%m-%d %H:%M:%S%z'
181
-                    ),
182
-                }
183
-                content.revision_type = ActionDescription.EDITION
184
-
185
-            DBSession.flush()
186
-            transaction.commit()
187
-
188
-            return content
189
-
190
-        if isinstance(calendar, UserCalendar):
191
-            # TODO - 20160531 - Bastien: UserCalendar currently not managed
192
-            raise NotImplementedError()
193
-
194
-        raise UnknownCalendarType('Type "{0}" is not implemented'
195
-                                  .format(type(calendar)))
186
+        return content
196 187
 
197 188
     def delete_event_with_name(self, event_name: str, current_user: User)\
198 189
             -> Content:
@@ -217,3 +208,19 @@ class CalendarManager(object):
217 208
         transaction.commit()
218 209
 
219 210
         return content
211
+
212
+    def populate_content_with_event(
213
+            self,
214
+            content: Content,
215
+            event: iCalendarEvent,
216
+            event_name: str,
217
+    ) -> Content:
218
+        content.label = event.get('summary')
219
+        content.description = event.get('description')
220
+        content.properties = {
221
+            'name': event_name,
222
+            'location': event.get('location'),
223
+            'raw': event.to_ical().decode("utf-8"),
224
+            'start': event.get('dtend').dt.strftime('%Y-%m-%d %H:%M:%S%z'),
225
+            'end': event.get('dtstart').dt.strftime('%Y-%m-%d %H:%M:%S%z'),
226
+        }