""" 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.core.exceptions import ObjectDoesNotExist from django.utils.translation import gettext_lazy as _ from intervention.models import RevocationDocument from konova.forms.modals import BaseModalForm, RemoveModalForm from konova.utils.message_templates import REVOCATION_ADDED, REVOCATION_EDITED class NewRevocationModalForm(BaseModalForm): date = forms.DateField( label=_("Date"), label_suffix=_(""), help_text=_("Date of revocation"), widget=forms.DateInput( attrs={ "type": "date", "data-provide": "datepicker", "class": "form-control", }, format="%d.%m.%Y" ) ) file = forms.FileField( label=_("Document"), label_suffix=_(""), required=False, help_text=_("Must be smaller than 15 Mb"), widget=forms.FileInput( attrs={ "class": "form-control-file" } ) ) 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", } ) ) document_model = RevocationDocument def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.form_title = _("Add revocation") self.form_caption = "" self.form_attrs = { "enctype": "multipart/form-data", # important for file upload } def save(self): revocation = self.instance.add_revocation(self) self.instance.mark_as_edited(self.user, self.request, edit_comment=REVOCATION_ADDED) return revocation class EditRevocationModalForm(NewRevocationModalForm): revocation = None def __init__(self, *args, **kwargs): self.revocation = kwargs.pop("revocation", None) super().__init__(*args, **kwargs) self.form_title = _("Edit revocation") try: doc = self.revocation.document.file except ObjectDoesNotExist: doc = None form_data = { "date": str(self.revocation.date), "file": doc, "comment": self.revocation.comment, } self.load_initial_data(form_data) def save(self): revocation = self.instance.edit_revocation(self) self.instance.mark_as_edited(self.user, self.request, edit_comment=REVOCATION_EDITED) return revocation class RemoveRevocationModalForm(RemoveModalForm): """ Removing modal form for Revocation Can be used for anything, where removing shall be confirmed by the user a second time. """ revocation = None def __init__(self, *args, **kwargs): revocation = kwargs.pop("revocation", None) self.revocation = revocation super().__init__(*args, **kwargs) def save(self): self.instance.remove_revocation(self)