diff --git a/intervention/forms/modalForms.py b/intervention/forms/modalForms.py
index 6a044c7c..8039b3ae 100644
--- a/intervention/forms/modalForms.py
+++ b/intervention/forms/modalForms.py
@@ -6,8 +6,11 @@ Created on: 27.09.21
"""
from dal import autocomplete
+from django.core.exceptions import ObjectDoesNotExist
+from django.db.models.fields.files import FieldFile
-from konova.utils.message_templates import DEDUCTION_ADDED, REVOCATION_ADDED, DEDUCTION_REMOVED, DEDUCTION_EDITED
+from konova.utils.message_templates import DEDUCTION_ADDED, REVOCATION_ADDED, DEDUCTION_REMOVED, DEDUCTION_EDITED, \
+ REVOCATION_EDITED, FILE_TYPE_UNSUPPORTED, FILE_SIZE_TOO_LARGE
from user.models import User, UserActionLogEntry
from django.db import transaction
from django import forms
@@ -15,7 +18,7 @@ from django.utils.translation import gettext_lazy as _
from compensation.models import EcoAccount, EcoAccountDeduction
from intervention.inputs import TextToClipboardInput
-from intervention.models import Intervention, InterventionDocument
+from intervention.models import Intervention, InterventionDocument, RevocationDocument
from konova.forms import BaseModalForm, NewDocumentForm, RemoveModalForm
from konova.utils.general import format_german_float
from konova.utils.user_checks import is_default_group_only
@@ -157,6 +160,7 @@ class NewRevocationModalForm(BaseModalForm):
}
)
)
+ document_model = RevocationDocument
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -166,12 +170,61 @@ class NewRevocationModalForm(BaseModalForm):
"enctype": "multipart/form-data", # important for file upload
}
+ def is_valid(self):
+ super_valid = super().is_valid()
+
+ _file = self.cleaned_data.get("file", None)
+
+ if isinstance(_file, FieldFile):
+ # FieldFile declares that no new file has been uploaded and we do not need to check on the file again
+ return super_valid
+
+ mime_type_valid = self.document_model.is_mime_type_valid(_file)
+ if not mime_type_valid:
+ self.add_error(
+ "file",
+ FILE_TYPE_UNSUPPORTED
+ )
+
+ file_size_valid = self.document_model.is_file_size_valid(_file)
+ if not file_size_valid:
+ self.add_error(
+ "file",
+ FILE_SIZE_TOO_LARGE
+ )
+
+ file_valid = mime_type_valid and file_size_valid
+ return super_valid and file_valid
+
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)
+ 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
diff --git a/intervention/models/intervention.py b/intervention/models/intervention.py
index e2e736d7..167c27af 100644
--- a/intervention/models/intervention.py
+++ b/intervention/models/intervention.py
@@ -8,6 +8,8 @@ Created on: 15.11.21
import shutil
from django.contrib import messages
+from django.core.exceptions import ObjectDoesNotExist
+from django.db.models.fields.files import FieldFile
from django.urls import reverse
from django.utils import timezone
@@ -202,6 +204,41 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
)
return revocation
+ def edit_revocation(self, form):
+ """ Updates a revocation of the intervention
+
+ Args:
+ form (EditRevocationModalForm): The form holding the data
+
+ Returns:
+
+ """
+ form_data = form.cleaned_data
+ file = form_data.get("file", None)
+
+ revocation = form.revocation
+ revocation.date = form_data.get("date", None)
+ revocation.comment = form_data.get("comment", None)
+
+ with transaction.atomic():
+ try:
+ revocation.document.date_of_creation = revocation.date
+ revocation.document.comment = revocation.comment
+ if not isinstance(file, FieldFile):
+ revocation.document.replace_file(file)
+ revocation.document.save()
+ except ObjectDoesNotExist:
+ revocation.document = RevocationDocument.objects.create(
+ title="revocation_of_{}".format(self.identifier),
+ date_of_creation=revocation.date,
+ comment=revocation.comment,
+ file=file,
+ instance=revocation
+ )
+ revocation.save()
+
+ return revocation
+
def remove_revocation(self, form):
""" Removes a revocation from the intervention
diff --git a/intervention/templates/intervention/detail/includes/revocation.html b/intervention/templates/intervention/detail/includes/revocation.html
index d6b07b72..6eb68f99 100644
--- a/intervention/templates/intervention/detail/includes/revocation.html
+++ b/intervention/templates/intervention/detail/includes/revocation.html
@@ -63,9 +63,12 @@
{{ rev.comment }}
-
+ |
{% if is_default_member and has_access %}
- |