#86 Edit document
* adds support for editing of documents * adds buttons for intervention
This commit is contained in:
@@ -12,6 +12,8 @@ from bootstrap_modal_forms.forms import BSModalForm
|
||||
from bootstrap_modal_forms.utils import is_ajax
|
||||
from django import forms
|
||||
from django.contrib import messages
|
||||
from django.db.models.fields.files import FieldFile
|
||||
|
||||
from user.models import User
|
||||
from django.contrib.gis.forms import OSMWidget, MultiPolygonField
|
||||
from django.contrib.gis.geos import MultiPolygon
|
||||
@@ -21,10 +23,10 @@ from django.shortcuts import render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from konova.contexts import BaseContext
|
||||
from konova.models import BaseObject, Geometry, RecordableObjectMixin
|
||||
from konova.models import BaseObject, Geometry, RecordableObjectMixin, AbstractDocument
|
||||
from konova.settings import DEFAULT_SRID
|
||||
from konova.tasks import celery_update_parcels
|
||||
from konova.utils.message_templates import FORM_INVALID, FILE_TYPE_UNSUPPORTED, FILE_SIZE_TOO_LARGE
|
||||
from konova.utils.message_templates import FORM_INVALID, FILE_TYPE_UNSUPPORTED, FILE_SIZE_TOO_LARGE, DOCUMENT_EDITED
|
||||
from user.models import UserActionLogEntry
|
||||
|
||||
|
||||
@@ -348,7 +350,7 @@ class RemoveDeadlineModalForm(RemoveModalForm):
|
||||
self.instance.remove_deadline(self)
|
||||
|
||||
|
||||
class NewDocumentForm(BaseModalForm):
|
||||
class NewDocumentModalForm(BaseModalForm):
|
||||
""" Modal form for new documents
|
||||
|
||||
"""
|
||||
@@ -420,6 +422,10 @@ class NewDocumentForm(BaseModalForm):
|
||||
|
||||
_file = self.cleaned_data.get("file", None)
|
||||
|
||||
if _file is None or 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(
|
||||
@@ -458,6 +464,39 @@ class NewDocumentForm(BaseModalForm):
|
||||
return doc
|
||||
|
||||
|
||||
class EditDocumentModalForm(NewDocumentModalForm):
|
||||
document = None
|
||||
document_model = AbstractDocument
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.document = kwargs.pop("document", None)
|
||||
super().__init__(*args, **kwargs)
|
||||
form_data = {
|
||||
"title": self.document.title,
|
||||
"comment": self.document.comment,
|
||||
"creation_date": str(self.document.date_of_creation),
|
||||
"file": self.document.file,
|
||||
}
|
||||
self.load_initial_data(form_data)
|
||||
|
||||
|
||||
def save(self):
|
||||
with transaction.atomic():
|
||||
document = self.document
|
||||
file = self.cleaned_data.get("file", None)
|
||||
|
||||
document.title = self.cleaned_data.get("title", None)
|
||||
document.comment = self.cleaned_data.get("comment", None)
|
||||
document.date_of_creation = self.cleaned_data.get("creation_date", None)
|
||||
if not isinstance(file, FieldFile):
|
||||
document.replace_file(file)
|
||||
document.save()
|
||||
|
||||
self.instance.mark_as_edited(self.user, self.request, edit_comment=DOCUMENT_EDITED)
|
||||
|
||||
return document
|
||||
|
||||
|
||||
class RecordModalForm(BaseModalForm):
|
||||
""" Modal form for recording data
|
||||
|
||||
|
||||
@@ -50,5 +50,5 @@ def remove_document(request: HttpRequest, doc: AbstractDocument):
|
||||
form = RemoveModalForm(request.POST or None, instance=doc, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DOCUMENT_REMOVED_TEMPLATE.format(title)
|
||||
msg_success=DOCUMENT_REMOVED_TEMPLATE.format(title),
|
||||
)
|
||||
@@ -65,6 +65,7 @@ REVOCATION_REMOVED = _("Revocation removed")
|
||||
# DOCUMENTS
|
||||
DOCUMENT_REMOVED_TEMPLATE = _("Document '{}' deleted")
|
||||
DOCUMENT_ADDED = _("Document added")
|
||||
DOCUMENT_EDITED = _("Document edited")
|
||||
|
||||
# Edited
|
||||
EDITED_GENERAL_DATA = _("Edited general data")
|
||||
|
||||
Reference in New Issue
Block a user