Files
konova/konova/views/document.py
mpeltriaux ed82109af9 Document views
* replaces function based views for creating, editing, removing and fetching documents with class based views
    * implemented for all major data types
2022-08-22 07:52:22 +02:00

139 lines
3.8 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 22.08.22
"""
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views import View
from konova.utils.documents import get_document, remove_document
from konova.utils.message_templates import DOCUMENT_ADDED, DOCUMENT_EDITED
class AbstractNewDocumentView(View):
model = None
form = None
redirect_url = None
class Meta:
abstract = True
def get(self, request, id: str):
""" Renders a form for uploading new documents
Args:
request (HttpRequest): The incoming request
id (str): The object's id to which the new document will be related
Returns:
"""
intervention = get_object_or_404(self.model, id=id)
form = self.form(request.POST or None, request.FILES or None, instance=intervention, request=request)
return form.process_request(
request,
msg_success=DOCUMENT_ADDED,
redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data"
)
def post(self, request, id: str):
return self.get(request, id)
class AbstractGetDocumentView(View):
model = None
document_model = None
class Meta:
abstract = True
def get(self, request, id: str, doc_id: str):
""" Returns the document as downloadable file
Wraps the generic document fetcher function from konova.utils.
Args:
request (HttpRequest): The incoming request
id (str): The object id
doc_id (str): The document id
Returns:
"""
get_object_or_404(self.model, id=id)
doc = get_object_or_404(self.document_model, id=doc_id)
return get_document(doc)
def post(self, request, id: str, doc_id: str):
return self.get(request, id, doc_id)
class AbstractRemoveDocumentView(View):
model = None
document_model = None
class Meta:
abstract = True
def get(self, request, id: str, doc_id: str):
""" Removes the document from the database and file system
Wraps the generic functionality from konova.utils.
Args:
request (HttpRequest): The incoming request
id (str): The intervention id
doc_id (str): The document id
Returns:
"""
get_object_or_404(self.model, id=id)
doc = get_object_or_404(self.document_model, id=doc_id)
return remove_document(
request,
doc
)
def post(self, request, id: str, doc_id: str):
return self.get(request, id, doc_id)
class AbstractEditDocumentView(View):
model = None
document_model = None
form = None
redirect_url = None
class Meta:
abstract = True
def get(self, request, id: str, doc_id: str):
""" GET handling for editing of existing document
Wraps the generic functionality from konova.utils.
Args:
request (HttpRequest): The incoming request
id (str): The intervention id
doc_id (str): The document id
Returns:
"""
obj = get_object_or_404(self.model, id=id)
doc = get_object_or_404(self.document_model, id=doc_id)
form = self.form(request.POST or None, request.FILES or None, instance=obj, document=doc,
request=request)
return form.process_request(
request,
DOCUMENT_EDITED,
redirect_url=reverse(self.redirect_url, args=(obj.id,)) + "#related_data"
)
def post(self, request, id: str, doc_id: str):
return self.get(request, id, doc_id)