Repository: digitalfabrik/integreat-cms

Issue:
Fix time representation in event form
Currently, an event is seen as all day long if and only if `start_time` equals 00:00 and `end_time` equals 23:59.
This results in an interesting bug: in the case where `start_time` and `end_time` of an event are manually set to these values, the fields in the form are not displayed upon reload but the corresponding checkbox also isn't checked.


Discussion:

Resolving patch:
diff --git a/backend/cms/forms/events/event_form.py b/backend/cms/forms/events/event_form.py
--- a/backend/cms/forms/events/event_form.py
+++ b/backend/cms/forms/events/event_form.py
@@ -70,19 +70,25 @@ def save(self, region=None, recurrence_rule=None):
 
     def clean(self):
         cleaned_data = super(EventForm, self).clean()
-
-        # If at least one of the time fields are missing, check if is_all_day is set
-        if not cleaned_data.get('start_time'):
-            if cleaned_data.get('is_all_day'):
-                cleaned_data['start_time'] = time.min
-            else:
+        logger.info('EventForm cleaned [1/2] with cleaned data %s', cleaned_data)
+
+        # make self.data mutable to allow values to be changed manually
+        self.data = self.data.copy()
+
+        if cleaned_data.get('is_all_day'):
+            cleaned_data['start_time'] = time.min
+            self.data['start_time'] = time.min
+            # Strip seconds and microseconds because our time fields only has hours and minutes
+            cleaned_data['end_time'] = time.max.replace(second=0, microsecond=0)
+            self.data['end_time'] = time.max.replace(second=0, microsecond=0)
+        else:
+            # If at least one of the time fields are missing, show an error
+            if not cleaned_data.get('start_time'):
                 self.add_error('start_time', forms.ValidationError(_('If the event is not all-day, the start time is required'), code='required'))
-        if not cleaned_data.get('end_time'):
-            if cleaned_data.get('is_all_day'):
-                # Strip seconds and microseconds because our time fields only has hours and minutes
-                cleaned_data['end_time'] = time.max.replace(second=0, microsecond=0)
-            else:
+            if not cleaned_data.get('end_time'):
                 self.add_error('end_time', forms.ValidationError(_('If the event is not all-day, the end time is required'), code='required'))
+            elif cleaned_data['start_time'] == time.min and cleaned_data['end_time'] == time.max.replace(second=0, microsecond=0):
+                self.data['is_all_day'] = True
 
         if cleaned_data.get('end_date') and cleaned_data.get('start_date'):
             if cleaned_data.get('end_date') < cleaned_data.get('start_date'):
@@ -95,12 +101,6 @@ def clean(self):
                     if cleaned_data.get('end_time') < cleaned_data.get('start_time'):
                         self.add_error('end_time', forms.ValidationError(_('The end of the event can\'t be before the start of the event'), code='invalid'))
 
-        return cleaned_data
+        logger.info('EventForm cleaned [2/2] with cleaned data %s', cleaned_data)
 
-    def has_changed(self):
-        # The change of all-day does not represent anything in the model
-        try:
-            self.changed_data.remove('is_all_day')
-        except ValueError:
-            pass
-        return super(EventForm, self).has_changed()
+        return cleaned_data
diff --git a/backend/cms/models/events/event_translation.py b/backend/cms/models/events/event_translation.py
--- a/backend/cms/models/events/event_translation.py
+++ b/backend/cms/models/events/event_translation.py
@@ -21,7 +21,7 @@ class EventTranslation(models.Model):
     slug = models.SlugField(max_length=200, blank=True, allow_unicode=True)
     status = models.CharField(max_length=6, choices=status.CHOICES, default=status.DRAFT)
     title = models.CharField(max_length=250)
-    description = models.TextField()
+    description = models.TextField(blank=True)
     language = models.ForeignKey(
         Language,
         related_name='event_translations',
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -19,7 +19,7 @@
     install_requires=[
         "beautifulsoup4",
         "cffi",
-        "Django==2.2.8",
+        "Django==2.2.9",
         "django-filer",
         "django-mptt",
         "django-widget-tweaks",


Test patch: