#86 Revocation edit
* adds support for revocation edit
* revocation document files will be replaced on an edit
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user