diff --git a/apps/participation/forms.py b/apps/participation/forms.py index 3d0d829..fc6249c 100644 --- a/apps/participation/forms.py +++ b/apps/participation/forms.py @@ -80,6 +80,17 @@ class PhaseForm(forms.ModelForm): model = Phase fields = ('start', 'end',) widgets = { - 'start': DateTimePickerInput(), - 'end': DateTimePickerInput(), + 'start': DateTimePickerInput(format='%d/%m/%Y %H:%M'), + 'end': DateTimePickerInput(format='%d/%m/%Y %H:%M'), } + + def clean(self): + # Ensure that dates are in a right order + cleaned_data = super().clean() + if cleaned_data["end"] <= cleaned_data["start"]: + self.add_error("end", _("Start date must be before the end date.")) + if Phase.objects.filter(phase_number__lt=self.instance.phase_number, end__gt=cleaned_data["start"]).exists(): + self.add_error("start", _("This phase must start after the previous phases.")) + if Phase.objects.filter(phase_number__gt=self.instance.phase_number, start__lt=cleaned_data["end"]).exists(): + self.add_error("end", _("This phase must end after the next phases.")) + return cleaned_data diff --git a/apps/participation/tables.py b/apps/participation/tables.py index 45088a9..2ca91c9 100644 --- a/apps/participation/tables.py +++ b/apps/participation/tables.py @@ -12,12 +12,14 @@ class CalendarTable(tables.Table): } row_attrs = { 'class': lambda record: 'bg-success' if timezone.now() > record.end else - 'bg-waring' if timezone.now() > record.start else - 'bg-danger' + 'bg-warning' if timezone.now() > record.start else + 'bg-danger', + 'data-id': lambda record: str(record.phase_number), } model = Phase fields = ('phase_number', 'description', 'start', 'end',) template_name = 'django_tables2/bootstrap4.html' + order_by = ('phase_number',) # noinspection PyTypeChecker diff --git a/apps/participation/templates/participation/phase_form.html b/apps/participation/templates/participation/phase_form.html index a5bb901..b2b3a5c 100644 --- a/apps/participation/templates/participation/phase_form.html +++ b/apps/participation/templates/participation/phase_form.html @@ -3,7 +3,7 @@ {% load crispy_forms_filters i18n %} {% block content %} -