2021-07-01 13:36:07 +02:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 17.11.20
|
|
|
|
|
|
|
|
"""
|
2021-09-16 09:12:32 +02:00
|
|
|
import shutil
|
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.gis.db import models
|
2021-09-17 09:05:43 +02:00
|
|
|
from django.db.models import QuerySet
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-08-27 09:44:23 +02:00
|
|
|
from codelist.models import KonovaCode
|
2021-08-27 15:16:05 +02:00
|
|
|
from codelist.settings import CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID, CODELIST_LAW_ID, \
|
|
|
|
CODELIST_PROCESS_TYPE_ID
|
2021-10-20 10:28:01 +02:00
|
|
|
from intervention.managers import InterventionManager
|
2021-10-25 13:06:54 +02:00
|
|
|
from intervention.utils.quality import InterventionQualityChecker
|
2021-09-01 16:24:49 +02:00
|
|
|
from konova.models import BaseObject, Geometry, UuidModel, BaseResource, AbstractDocument, \
|
2021-10-26 15:09:30 +02:00
|
|
|
generate_document_file_upload_path, RecordableObject, CheckableObject, ShareableObject
|
2021-10-13 14:03:34 +02:00
|
|
|
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE, LANIS_ZOOM_LUT
|
2021-07-30 13:30:42 +02:00
|
|
|
from konova.utils import generators
|
2021-08-05 12:54:28 +02:00
|
|
|
from user.models import UserActionLogEntry
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
|
2021-07-30 09:30:33 +02:00
|
|
|
class ResponsibilityData(UuidModel):
|
2021-07-01 13:36:07 +02:00
|
|
|
"""
|
2021-07-30 09:30:33 +02:00
|
|
|
Holds intervention data about responsible organizations and their file numbers for this case
|
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
"""
|
2021-08-27 09:44:23 +02:00
|
|
|
registration_office = models.ForeignKey(
|
|
|
|
KonovaCode,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
related_name="+",
|
|
|
|
blank=True,
|
|
|
|
limit_choices_to={
|
|
|
|
"code_lists__in": [CODELIST_REGISTRATION_OFFICE_ID],
|
|
|
|
"is_selectable": True,
|
|
|
|
"is_archived": False,
|
|
|
|
}
|
|
|
|
)
|
2021-07-01 14:38:57 +02:00
|
|
|
registration_file_number = models.CharField(max_length=1000, blank=True, null=True)
|
2021-08-27 09:44:23 +02:00
|
|
|
conservation_office = models.ForeignKey(
|
|
|
|
KonovaCode,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
related_name="+",
|
|
|
|
blank=True,
|
|
|
|
limit_choices_to={
|
|
|
|
"code_lists__in": [CODELIST_CONSERVATION_OFFICE_ID],
|
|
|
|
"is_selectable": True,
|
|
|
|
"is_archived": False,
|
|
|
|
}
|
|
|
|
)
|
2021-07-22 13:19:14 +02:00
|
|
|
conservation_file_number = models.CharField(max_length=1000, blank=True, null=True)
|
2021-08-27 09:44:23 +02:00
|
|
|
handler = models.CharField(max_length=500, null=True, blank=True, help_text="Refers to 'Eingriffsverursacher' or 'Maßnahmenträger'")
|
2021-07-20 14:23:16 +02:00
|
|
|
|
2021-08-02 11:52:20 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "ZB: {} | ETS: {} | Handler: {}".format(
|
|
|
|
self.registration_office,
|
|
|
|
self.conservation_office,
|
|
|
|
self.handler
|
|
|
|
)
|
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-08-04 13:32:35 +02:00
|
|
|
class Revocation(BaseResource):
|
|
|
|
"""
|
|
|
|
Holds revocation data e.g. for intervention objects
|
|
|
|
"""
|
|
|
|
date = models.DateField(null=True, blank=True, help_text="Revocation from")
|
|
|
|
comment = models.TextField(null=True, blank=True)
|
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
def delete(self, *args, **kwargs):
|
2021-08-04 13:32:35 +02:00
|
|
|
# Make sure related objects are being removed as well
|
2021-08-26 15:45:24 +02:00
|
|
|
if self.document:
|
2021-09-01 16:24:49 +02:00
|
|
|
self.document.delete(*args, **kwargs)
|
2021-08-04 13:32:35 +02:00
|
|
|
super().delete()
|
|
|
|
|
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
class RevocationDocument(AbstractDocument):
|
|
|
|
"""
|
|
|
|
Specializes document upload for revocations with certain path
|
|
|
|
"""
|
|
|
|
instance = models.OneToOneField(
|
|
|
|
Revocation,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="document",
|
|
|
|
)
|
|
|
|
file = models.FileField(
|
2021-09-01 16:40:36 +02:00
|
|
|
upload_to=generate_document_file_upload_path,
|
|
|
|
max_length=1000,
|
2021-09-01 16:24:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def intervention(self):
|
2021-09-17 09:05:43 +02:00
|
|
|
"""
|
|
|
|
Shortcut for opening the related intervention
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
intervention (Intervention)
|
|
|
|
"""
|
2021-09-01 16:24:49 +02:00
|
|
|
return self.instance.legaldata.intervention
|
|
|
|
|
2021-09-17 09:05:43 +02:00
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Custom delete functionality for RevocationDocuments.
|
|
|
|
Removes the folder from the file system if there are no further documents for this entry.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
*args ():
|
|
|
|
**kwargs ():
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
revoc_docs, other_intervention_docs = self.intervention.get_documents()
|
|
|
|
|
|
|
|
# Remove the file itself
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
|
|
|
# Always remove 'revocation' folder
|
|
|
|
folder_path = self.file.path.split("/")
|
2021-09-17 09:43:03 +02:00
|
|
|
try:
|
|
|
|
shutil.rmtree("/".join(folder_path[:-1]))
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Revocation subfolder seems to be missing already
|
|
|
|
pass
|
2021-09-17 09:05:43 +02:00
|
|
|
|
|
|
|
if other_intervention_docs.count() == 0:
|
|
|
|
# If there are no further documents for the intervention, we can simply remove the whole folder as well!
|
2021-09-17 09:43:03 +02:00
|
|
|
try:
|
|
|
|
shutil.rmtree("/".join(folder_path[:-2]))
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Folder seems to be missing already
|
|
|
|
pass
|
2021-09-01 16:24:49 +02:00
|
|
|
|
2021-09-20 13:33:07 +02:00
|
|
|
|
2021-07-30 09:30:33 +02:00
|
|
|
class LegalData(UuidModel):
|
|
|
|
"""
|
|
|
|
Holds intervention legal data such as important dates, laws or responsible handler
|
|
|
|
"""
|
2021-07-01 15:08:22 +02:00
|
|
|
# Refers to "zugelassen am"
|
2021-07-30 09:30:33 +02:00
|
|
|
registration_date = models.DateField(null=True, blank=True, help_text="Refers to 'Zugelassen am'")
|
2021-07-01 15:08:22 +02:00
|
|
|
|
|
|
|
# Refers to "Bestandskraft am"
|
2021-07-30 09:30:33 +02:00
|
|
|
binding_date = models.DateField(null=True, blank=True, help_text="Refers to 'Bestandskraft am'")
|
|
|
|
|
2021-08-27 15:16:05 +02:00
|
|
|
process_type = models.ForeignKey(
|
|
|
|
KonovaCode,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
related_name="+",
|
|
|
|
blank=True,
|
|
|
|
limit_choices_to={
|
|
|
|
"code_lists__in": [CODELIST_PROCESS_TYPE_ID],
|
|
|
|
"is_selectable": True,
|
|
|
|
"is_archived": False,
|
|
|
|
}
|
|
|
|
)
|
2021-09-20 13:33:07 +02:00
|
|
|
laws = models.ManyToManyField(
|
2021-08-27 15:16:05 +02:00
|
|
|
KonovaCode,
|
|
|
|
blank=True,
|
|
|
|
limit_choices_to={
|
|
|
|
"code_lists__in": [CODELIST_LAW_ID],
|
|
|
|
"is_selectable": True,
|
|
|
|
"is_archived": False,
|
|
|
|
}
|
|
|
|
)
|
2021-07-30 09:30:33 +02:00
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
revocation = models.OneToOneField(Revocation, null=True, blank=True, help_text="Refers to 'Widerspruch am'", on_delete=models.SET_NULL)
|
2021-08-04 13:32:35 +02:00
|
|
|
|
2021-07-30 09:30:33 +02:00
|
|
|
|
2021-10-26 15:09:30 +02:00
|
|
|
class Intervention(BaseObject, ShareableObject, RecordableObject, CheckableObject):
|
2021-07-30 09:30:33 +02:00
|
|
|
"""
|
|
|
|
Interventions are e.g. construction sites where nature used to be.
|
|
|
|
"""
|
|
|
|
responsible = models.OneToOneField(
|
|
|
|
ResponsibilityData,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Holds data on responsible organizations ('Zulassungsbehörde', 'Eintragungsstelle')"
|
|
|
|
)
|
|
|
|
legal = models.OneToOneField(
|
|
|
|
LegalData,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Holds data on legal dates or law"
|
|
|
|
)
|
|
|
|
geometry = models.ForeignKey(Geometry, null=True, blank=True, on_delete=models.SET_NULL)
|
2021-07-01 15:08:22 +02:00
|
|
|
|
2021-10-14 14:12:33 +02:00
|
|
|
objects = InterventionManager()
|
|
|
|
|
2021-07-29 10:51:14 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "{} ({})".format(self.identifier, self.title)
|
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
def save(self, *args, **kwargs):
|
2021-09-23 15:05:17 +02:00
|
|
|
""" Custom save functionality
|
|
|
|
|
|
|
|
Performs some pre-save checks:
|
|
|
|
1. Checking for existing identifiers
|
|
|
|
|
|
|
|
Args:
|
|
|
|
*args ():
|
|
|
|
**kwargs ():
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-07-01 13:36:07 +02:00
|
|
|
if self.identifier is None or len(self.identifier) == 0:
|
2021-10-25 17:39:39 +02:00
|
|
|
# No identifier given by the user
|
2021-10-05 16:35:24 +02:00
|
|
|
self.identifier = self.generate_new_identifier()
|
2021-09-23 15:05:17 +02:00
|
|
|
|
2021-10-25 17:39:39 +02:00
|
|
|
# Before saving, make sure the given identifier is not used in the meanwhile
|
|
|
|
while Intervention.objects.filter(identifier=self.identifier).exclude(id=self.id).exists():
|
2021-10-05 16:35:24 +02:00
|
|
|
self.identifier = self.generate_new_identifier()
|
2021-07-22 13:19:14 +02:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2021-09-23 15:05:17 +02:00
|
|
|
def delete(self, using=None, keep_parents=False):
|
|
|
|
to_delete = [
|
|
|
|
self.legal,
|
|
|
|
self.responsible,
|
|
|
|
self.geometry,
|
|
|
|
self.log.all()
|
|
|
|
]
|
|
|
|
for entry in to_delete:
|
|
|
|
try:
|
|
|
|
entry.delete()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
super().delete(using, keep_parents)
|
|
|
|
|
2021-10-25 13:06:54 +02:00
|
|
|
def quality_check(self) -> InterventionQualityChecker:
|
2021-08-10 17:19:42 +02:00
|
|
|
""" Quality check
|
|
|
|
|
|
|
|
Returns:
|
2021-08-19 13:44:06 +02:00
|
|
|
ret_msgs (list): Holds error messages
|
2021-08-10 17:19:42 +02:00
|
|
|
"""
|
2021-10-25 13:06:54 +02:00
|
|
|
checker = InterventionQualityChecker(obj=self)
|
|
|
|
checker.run_check()
|
|
|
|
return checker
|
2021-08-10 13:57:03 +02:00
|
|
|
|
|
|
|
def get_LANIS_link(self) -> str:
|
|
|
|
""" Generates a link for LANIS depending on the geometry
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
geom = self.geometry.geom.transform(DEFAULT_SRID_RLP, clone=True)
|
|
|
|
x = geom.centroid.x
|
|
|
|
y = geom.centroid.y
|
2021-10-13 14:03:34 +02:00
|
|
|
area = int(geom.envelope.area)
|
|
|
|
z_l = 16
|
|
|
|
for k_area, v_zoom in LANIS_ZOOM_LUT.items():
|
|
|
|
if k_area < area:
|
|
|
|
z_l = v_zoom
|
|
|
|
break
|
|
|
|
zoom_lvl = z_l
|
2021-08-10 13:57:03 +02:00
|
|
|
except AttributeError:
|
|
|
|
# If no geometry has been added, yet.
|
|
|
|
x = 1
|
|
|
|
y = 1
|
|
|
|
zoom_lvl = 6
|
|
|
|
return LANIS_LINK_TEMPLATE.format(
|
|
|
|
zoom_lvl,
|
|
|
|
x,
|
|
|
|
y,
|
2021-08-10 17:19:42 +02:00
|
|
|
)
|
2021-08-26 15:45:24 +02:00
|
|
|
|
2021-09-17 09:05:43 +02:00
|
|
|
def get_documents(self) -> (QuerySet, QuerySet):
|
|
|
|
""" Getter for all documents of an intervention
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
revoc_docs (QuerySet): The queryset of a revocation document
|
|
|
|
regular_docs (QuerySet): The queryset of regular other documents
|
|
|
|
"""
|
|
|
|
revoc_docs = RevocationDocument.objects.filter(
|
|
|
|
instance=self.legal.revocation
|
|
|
|
)
|
|
|
|
regular_docs = InterventionDocument.objects.filter(
|
|
|
|
instance=self
|
|
|
|
)
|
|
|
|
return revoc_docs, regular_docs
|
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
|
|
|
|
class InterventionDocument(AbstractDocument):
|
|
|
|
"""
|
|
|
|
Specializes document upload for an intervention with certain path
|
|
|
|
"""
|
|
|
|
instance = models.ForeignKey(
|
|
|
|
Intervention,
|
|
|
|
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-16 09:12:32 +02:00
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Custom delete functionality for InterventionDocuments.
|
|
|
|
Removes the folder from the file system if there are no further documents for this entry.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
*args ():
|
|
|
|
**kwargs ():
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-09-17 09:05:43 +02:00
|
|
|
revoc_docs, other_intervention_docs = self.instance.get_documents()
|
2021-09-16 09:12:32 +02:00
|
|
|
|
|
|
|
folder_path = None
|
|
|
|
if revoc_docs.count() == 0 and other_intervention_docs.count() == 1:
|
|
|
|
# The only file left for this intervention is the one which is currently processed and will be deleted
|
|
|
|
# Make sure that the intervention 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)
|
|
|
|
|
|
|
|
# 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
|