2021-11-15 17:41:52 +01:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 15.11.21
|
|
|
|
|
|
|
|
"""
|
2021-09-17 09:24:31 +02:00
|
|
|
import shutil
|
|
|
|
|
2021-12-15 13:59:52 +01:00
|
|
|
from django.contrib import messages
|
2021-08-19 13:02:31 +02:00
|
|
|
from django.db import models
|
2021-09-17 09:24:31 +02:00
|
|
|
from django.db.models import QuerySet
|
2021-12-15 13:59:52 +01:00
|
|
|
from django.http import HttpRequest
|
2022-02-02 11:26:02 +01:00
|
|
|
from django.urls import reverse
|
2021-08-19 13:02:31 +02:00
|
|
|
|
2022-05-31 13:33:44 +02:00
|
|
|
from compensation.models import AbstractCompensation, PikMixin
|
2021-10-14 14:12:33 +02:00
|
|
|
from ema.managers import EmaManager
|
2021-10-25 14:36:58 +02:00
|
|
|
from ema.utils.quality import EmaQualityChecker
|
2021-11-16 08:29:18 +01:00
|
|
|
from konova.models import AbstractDocument, generate_document_file_upload_path, RecordableObjectMixin, ShareableObjectMixin
|
2022-02-04 09:18:46 +01:00
|
|
|
from konova.utils.message_templates import DATA_UNSHARED_EXPLANATION, DOCUMENT_REMOVED_TEMPLATE
|
2021-08-19 13:02:31 +02:00
|
|
|
|
|
|
|
|
2022-05-31 13:33:44 +02:00
|
|
|
class Ema(AbstractCompensation, ShareableObjectMixin, RecordableObjectMixin, PikMixin):
|
2021-08-19 13:02:31 +02:00
|
|
|
"""
|
|
|
|
EMA = Ersatzzahlungsmaßnahme
|
|
|
|
(compensation actions from payments)
|
|
|
|
|
|
|
|
Until 2015 the EMA was the data object to keep track of any compensation, which has been funded by payments
|
|
|
|
previously paid. In 2015 another organization got in charge of this, which led to the creation of the data object
|
|
|
|
MAE (which is basically the same, just renamed in their system) to differ between the 'old' payment funded ones and
|
|
|
|
the new. For historical reasons, we need to keep EMAs in our system, since there are still entries done to this day,
|
|
|
|
which have been performed somewhere before 2015 and therefore needs to be entered.
|
|
|
|
Further information:
|
|
|
|
https://snu.rlp.de/de/foerderungen/massnahmen-aus-ersatzzahlungen/uebersicht-mae/
|
|
|
|
|
|
|
|
EMA therefore holds data like a compensation: actions, before-/after-states, deadlines, ...
|
|
|
|
|
|
|
|
"""
|
2021-10-14 14:12:33 +02:00
|
|
|
objects = EmaManager()
|
|
|
|
|
2021-08-19 13:02:31 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "{}".format(self.identifier)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.identifier is None or len(self.identifier) == 0:
|
|
|
|
# Create new identifier
|
2021-10-05 16:35:24 +02:00
|
|
|
new_id = self.generate_new_identifier()
|
2021-08-19 13:02:31 +02:00
|
|
|
while Ema.objects.filter(identifier=new_id).exists():
|
2021-10-05 16:35:24 +02:00
|
|
|
new_id = self.generate_new_identifier()
|
2021-08-19 13:02:31 +02:00
|
|
|
self.identifier = new_id
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2021-10-25 14:36:58 +02:00
|
|
|
def quality_check(self) -> EmaQualityChecker:
|
2021-08-19 13:44:06 +02:00
|
|
|
""" Quality check
|
|
|
|
|
|
|
|
Returns:
|
2021-10-25 14:36:58 +02:00
|
|
|
ret_msgs (EmaQualityChecker): Holds validity error messages
|
2021-08-19 13:44:06 +02:00
|
|
|
"""
|
2021-10-25 14:36:58 +02:00
|
|
|
checker = EmaQualityChecker(self)
|
|
|
|
checker.run_check()
|
|
|
|
return checker
|
2021-09-01 16:24:49 +02:00
|
|
|
|
2021-09-17 09:24:31 +02:00
|
|
|
def get_documents(self) -> QuerySet:
|
|
|
|
""" Getter for all documents of an EMA
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
docs (QuerySet): The queryset of all documents
|
|
|
|
"""
|
|
|
|
docs = EmaDocument.objects.filter(
|
|
|
|
instance=self
|
|
|
|
)
|
|
|
|
return docs
|
|
|
|
|
2021-12-15 13:59:52 +01:00
|
|
|
def set_status_messages(self, request: HttpRequest):
|
2021-12-15 15:10:35 +01:00
|
|
|
""" Setter for different information that need to be rendered
|
|
|
|
|
|
|
|
Adds messages to the given HttpRequest
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
request (HttpRequest): The modified request
|
|
|
|
"""
|
2021-12-15 13:59:52 +01:00
|
|
|
if not self.is_shared_with(request.user):
|
|
|
|
messages.info(request, DATA_UNSHARED_EXPLANATION)
|
2022-01-05 14:13:26 +01:00
|
|
|
self.set_geometry_conflict_message(request)
|
2021-12-15 13:59:52 +01:00
|
|
|
return request
|
|
|
|
|
2022-01-21 09:15:06 +01:00
|
|
|
def is_ready_for_publish(self) -> bool:
|
|
|
|
""" Checks whether the data passes all constraints for being publishable
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
is_ready (bool) : True|False
|
|
|
|
"""
|
|
|
|
is_recorded = self.recorded is not None
|
|
|
|
is_ready = is_recorded
|
|
|
|
return is_ready
|
|
|
|
|
2022-02-02 11:26:02 +01:00
|
|
|
def get_share_link(self):
|
|
|
|
""" Returns the share url for the object
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-08-22 10:58:07 +02:00
|
|
|
return reverse("ema:share-token", args=(self.id, self.access_token))
|
2022-02-02 11:26:02 +01:00
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
|
|
|
|
class EmaDocument(AbstractDocument):
|
|
|
|
"""
|
|
|
|
Specializes document upload for ema with certain path
|
|
|
|
"""
|
|
|
|
instance = models.ForeignKey(
|
|
|
|
Ema,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="documents",
|
|
|
|
)
|
|
|
|
file = models.FileField(
|
2021-09-01 16:40:36 +02:00
|
|
|
upload_to=generate_document_file_upload_path,
|
|
|
|
max_length=1000,
|
2021-09-17 09:24:31 +02:00
|
|
|
)
|
|
|
|
|
2022-02-04 09:18:46 +01:00
|
|
|
def delete(self, user=None, *args, **kwargs):
|
2021-09-17 09:24:31 +02:00
|
|
|
"""
|
|
|
|
Custom delete functionality for EcoAccountDocuments.
|
|
|
|
Removes the folder from the file system if there are no further documents for this entry.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
*args ():
|
|
|
|
**kwargs ():
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
ema_docs = self.instance.get_documents()
|
|
|
|
|
|
|
|
folder_path = None
|
|
|
|
if ema_docs.count() == 1:
|
|
|
|
# The only file left for this EMA is the one which is currently processed and will be deleted
|
|
|
|
# Make sure that the compensation folder itself is deleted as well, not only the file
|
|
|
|
# Therefore take the folder path from the file path
|
|
|
|
folder_path = self.file.path.split("/")[:-1]
|
|
|
|
folder_path = "/".join(folder_path)
|
|
|
|
|
2022-02-04 09:18:46 +01:00
|
|
|
if user:
|
|
|
|
self.instance.mark_as_edited(user, edit_comment=DOCUMENT_REMOVED_TEMPLATE.format(self.title))
|
|
|
|
|
2021-09-17 09:24:31 +02:00
|
|
|
# Remove the file itself
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
|
|
|
# If a folder path has been set, we need to delete the whole folder!
|
|
|
|
if folder_path is not None:
|
2021-09-17 09:43:03 +02:00
|
|
|
try:
|
|
|
|
shutil.rmtree(folder_path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Folder seems to be missing already...
|
|
|
|
pass
|