2022-08-18 09:54:49 +02:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: ksp-servicestelle@sgdnord.rlp.de
|
|
|
|
Created on: 18.08.22
|
|
|
|
|
|
|
|
"""
|
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
from konova.forms.modals import BaseModalForm
|
|
|
|
from konova.models import DeadlineType
|
2023-05-17 14:08:57 +02:00
|
|
|
from konova.utils import validators
|
2022-08-18 09:54:49 +02:00
|
|
|
from konova.utils.message_templates import DEADLINE_EDITED
|
|
|
|
|
|
|
|
|
|
|
|
class NewDeadlineModalForm(BaseModalForm):
|
|
|
|
""" Form handling deadline related input
|
|
|
|
|
|
|
|
"""
|
|
|
|
type = forms.ChoiceField(
|
|
|
|
label=_("Deadline Type"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
|
|
|
help_text=_("Select the deadline type"),
|
|
|
|
choices=DeadlineType.choices,
|
|
|
|
widget=forms.Select(
|
|
|
|
attrs={
|
|
|
|
"class": "form-control"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
date = forms.DateField(
|
|
|
|
label=_("Date"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
|
|
|
help_text=_("Select date"),
|
2023-05-17 14:08:57 +02:00
|
|
|
validators=[validators.reasonable_date],
|
2022-08-18 09:54:49 +02:00
|
|
|
widget=forms.DateInput(
|
|
|
|
attrs={
|
|
|
|
"type": "date",
|
|
|
|
"data-provide": "datepicker",
|
|
|
|
"class": "form-control",
|
|
|
|
},
|
|
|
|
format="%d.%m.%Y"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
comment = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
max_length=200,
|
|
|
|
label=_("Comment"),
|
|
|
|
label_suffix=_(""),
|
|
|
|
help_text=_("Additional comment, maximum {} letters").format(200),
|
|
|
|
widget=forms.Textarea(
|
|
|
|
attrs={
|
|
|
|
"cols": 30,
|
|
|
|
"rows": 5,
|
|
|
|
"class": "form-control",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("New deadline")
|
|
|
|
self.form_caption = _("Insert data for the new deadline")
|
|
|
|
|
2023-08-29 10:55:03 +02:00
|
|
|
def is_valid(self):
|
|
|
|
valid = super().is_valid()
|
|
|
|
deadline_type = self.cleaned_data.get("type")
|
|
|
|
comment = self.cleaned_data.get("comment") or None
|
|
|
|
|
|
|
|
other_deadline_without_comment = deadline_type == DeadlineType.OTHER and comment is None
|
|
|
|
|
|
|
|
if other_deadline_without_comment:
|
|
|
|
self.add_error(
|
|
|
|
"comment",
|
|
|
|
_("Please explain this 'other' type of deadline.")
|
|
|
|
)
|
|
|
|
valid &= False
|
|
|
|
|
|
|
|
return valid
|
|
|
|
|
2022-08-18 09:54:49 +02:00
|
|
|
def save(self):
|
|
|
|
deadline = self.instance.add_deadline(self)
|
|
|
|
return deadline
|
|
|
|
|
|
|
|
|
|
|
|
class EditDeadlineModalForm(NewDeadlineModalForm):
|
|
|
|
deadline = None
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.deadline = kwargs.pop("deadline", None)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("Edit deadline")
|
|
|
|
form_data = {
|
|
|
|
"type": self.deadline.type,
|
|
|
|
"date": str(self.deadline.date),
|
|
|
|
"comment": self.deadline.comment,
|
|
|
|
}
|
|
|
|
self.load_initial_data(form_data)
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
deadline = self.deadline
|
|
|
|
deadline.type = self.cleaned_data.get("type", None)
|
|
|
|
deadline.date = self.cleaned_data.get("date", None)
|
|
|
|
deadline.comment = self.cleaned_data.get("comment", None)
|
|
|
|
deadline.save()
|
|
|
|
self.instance.mark_as_edited(self.user, self.request, edit_comment=DEADLINE_EDITED)
|
|
|
|
return deadline
|