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
|
2021-11-15 17:19:06 +01:00
|
|
|
Created on: 15.11.21
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
"""
|
2021-11-16 08:29:18 +01:00
|
|
|
import shutil
|
|
|
|
|
2021-12-15 13:59:52 +01:00
|
|
|
from django.contrib import messages
|
2022-02-09 16:02:28 +01:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from django.db.models.fields.files import FieldFile
|
2022-02-02 11:26:02 +01:00
|
|
|
from django.urls import reverse
|
2022-01-21 09:02:56 +01:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-03-21 12:14:55 +01:00
|
|
|
from intervention.tasks import celery_export_to_egon
|
2022-01-12 12:56:22 +01:00
|
|
|
from user.models import User
|
2021-11-15 17:19:06 +01:00
|
|
|
from django.db import models, transaction
|
2021-09-17 09:05:43 +02:00
|
|
|
from django.db.models import QuerySet
|
2021-11-17 14:33:05 +01:00
|
|
|
from django.http import HttpRequest
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-10-20 10:28:01 +02:00
|
|
|
from intervention.managers import InterventionManager
|
2021-11-15 17:19:06 +01:00
|
|
|
from intervention.models.legal import Legal
|
|
|
|
from intervention.models.responsibility import Responsibility
|
2021-11-16 12:26:50 +01:00
|
|
|
from intervention.models.revocation import RevocationDocument, Revocation
|
2021-10-25 13:06:54 +02:00
|
|
|
from intervention.utils.quality import InterventionQualityChecker
|
2021-12-15 13:59:52 +01:00
|
|
|
from konova.models import generate_document_file_upload_path, AbstractDocument, BaseObject, \
|
|
|
|
ShareableObjectMixin, \
|
2022-08-15 08:08:15 +02:00
|
|
|
RecordableObjectMixin, CheckableObjectMixin, GeoReferencedMixin, ResubmitableObjectMixin
|
2022-02-08 13:16:20 +01:00
|
|
|
from konova.utils.message_templates import DATA_UNSHARED_EXPLANATION, DOCUMENT_REMOVED_TEMPLATE, \
|
2022-02-08 15:25:44 +01:00
|
|
|
PAYMENT_REMOVED, PAYMENT_ADDED, REVOCATION_REMOVED, INTERVENTION_HAS_REVOCATIONS_TEMPLATE
|
2021-11-16 13:15:15 +01:00
|
|
|
from user.models import UserActionLogEntry
|
2021-07-30 09:30:33 +02:00
|
|
|
|
|
|
|
|
2022-08-15 08:08:15 +02:00
|
|
|
class Intervention(BaseObject,
|
|
|
|
ShareableObjectMixin,
|
|
|
|
RecordableObjectMixin,
|
|
|
|
CheckableObjectMixin,
|
|
|
|
GeoReferencedMixin,
|
|
|
|
ResubmitableObjectMixin
|
|
|
|
):
|
2021-07-30 09:30:33 +02:00
|
|
|
"""
|
|
|
|
Interventions are e.g. construction sites where nature used to be.
|
|
|
|
"""
|
|
|
|
responsible = models.OneToOneField(
|
2021-11-15 17:09:17 +01:00
|
|
|
Responsibility,
|
2021-07-30 09:30:33 +02:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Holds data on responsible organizations ('Zulassungsbehörde', 'Eintragungsstelle')"
|
|
|
|
)
|
|
|
|
legal = models.OneToOneField(
|
2021-11-15 17:09:17 +01:00
|
|
|
Legal,
|
2021-07-30 09:30:33 +02:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Holds data on legal dates or law"
|
|
|
|
)
|
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):
|
2022-02-01 18:41:02 +01:00
|
|
|
return f"{self.identifier} ({self.title})"
|
2021-07-29 10:51:14 +02:00
|
|
|
|
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
|
|
|
|
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(
|
2021-11-15 12:18:22 +01:00
|
|
|
instance__in=self.legal.revocations.all()
|
2021-09-17 09:05:43 +02:00
|
|
|
)
|
|
|
|
regular_docs = InterventionDocument.objects.filter(
|
|
|
|
instance=self
|
|
|
|
)
|
|
|
|
return revoc_docs, regular_docs
|
|
|
|
|
2021-11-17 12:09:49 +01:00
|
|
|
def set_unchecked(self):
|
2021-11-17 14:33:05 +01:00
|
|
|
super().set_unchecked()
|
2021-11-11 13:13:05 +01:00
|
|
|
|
2021-11-17 12:09:49 +01:00
|
|
|
def set_checked(self, user: User) -> UserActionLogEntry:
|
|
|
|
log_entry = super().set_checked(user)
|
2022-01-07 15:41:40 +01:00
|
|
|
if log_entry is not None:
|
|
|
|
self.add_log_entry_to_compensations(log_entry)
|
2021-11-17 12:09:49 +01:00
|
|
|
return log_entry
|
2021-11-11 13:13:05 +01:00
|
|
|
|
2021-11-17 12:09:49 +01:00
|
|
|
def set_unrecorded(self, user: User):
|
|
|
|
log_entry = super().set_unrecorded(user)
|
|
|
|
self.add_log_entry_to_compensations(log_entry)
|
2022-01-25 09:29:14 +01:00
|
|
|
return log_entry
|
2021-11-11 13:13:05 +01:00
|
|
|
|
2022-03-21 12:14:55 +01:00
|
|
|
def send_data_to_egon(self):
|
|
|
|
""" Performs the export to rabbitmq of this intervention's data
|
|
|
|
|
|
|
|
FOLLOWING BACKWARDS COMPATIBILITY LOGIC
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
celery_export_to_egon.delay(self.id)
|
|
|
|
|
2021-11-17 12:09:49 +01:00
|
|
|
def set_recorded(self, user: User) -> UserActionLogEntry:
|
|
|
|
log_entry = super().set_recorded(user)
|
|
|
|
self.add_log_entry_to_compensations(log_entry)
|
|
|
|
return log_entry
|
2021-11-11 13:13:05 +01:00
|
|
|
|
2021-11-17 12:09:49 +01:00
|
|
|
def add_log_entry_to_compensations(self, log_entry: UserActionLogEntry):
|
|
|
|
""" Adds the log entry to related compensations
|
2021-11-11 13:13:05 +01:00
|
|
|
|
|
|
|
Args:
|
2021-11-17 12:09:49 +01:00
|
|
|
log_entry (UserActionLogEntry): The log entry
|
2021-11-11 13:13:05 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comps = self.compensations.all()
|
|
|
|
for comp in comps:
|
|
|
|
comp.log.add(log_entry)
|
|
|
|
|
2021-11-15 17:09:17 +01:00
|
|
|
def add_payment(self, form):
|
|
|
|
""" Adds a new payment to the intervention
|
|
|
|
|
|
|
|
Args:
|
|
|
|
form (NewPaymentForm): The form holding the data
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
from compensation.models import Payment
|
|
|
|
form_data = form.cleaned_data
|
|
|
|
user = form.user
|
|
|
|
with transaction.atomic():
|
2021-11-16 13:15:15 +01:00
|
|
|
created_action = UserActionLogEntry.get_created_action(user)
|
2021-11-15 17:09:17 +01:00
|
|
|
pay = Payment.objects.create(
|
|
|
|
created=created_action,
|
|
|
|
amount=form_data.get("amount", -1),
|
|
|
|
due_on=form_data.get("due", None),
|
|
|
|
comment=form_data.get("comment", None),
|
|
|
|
intervention=self,
|
|
|
|
)
|
2022-02-08 13:16:20 +01:00
|
|
|
self.mark_as_edited(user, form.request, edit_comment=PAYMENT_ADDED)
|
2022-04-25 11:16:51 +02:00
|
|
|
|
|
|
|
self.send_data_to_egon()
|
2021-11-17 12:09:49 +01:00
|
|
|
return pay
|
2021-11-15 17:09:17 +01:00
|
|
|
|
2021-11-16 12:26:50 +01:00
|
|
|
def add_revocation(self, form):
|
|
|
|
""" Adds a new revocation to the intervention
|
|
|
|
|
|
|
|
Args:
|
2021-11-16 12:43:13 +01:00
|
|
|
form (NewRevocationModalForm): The form holding the data
|
2021-11-16 12:26:50 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
form_data = form.cleaned_data
|
|
|
|
user = form.user
|
|
|
|
with transaction.atomic():
|
2021-11-16 13:15:15 +01:00
|
|
|
created_action = UserActionLogEntry.get_created_action(user)
|
|
|
|
|
2021-11-16 12:26:50 +01:00
|
|
|
revocation = Revocation.objects.create(
|
|
|
|
date=form_data["date"],
|
|
|
|
legal=self.legal,
|
|
|
|
comment=form_data["comment"],
|
|
|
|
created=created_action,
|
|
|
|
)
|
|
|
|
|
|
|
|
if form_data["file"]:
|
|
|
|
RevocationDocument.objects.create(
|
|
|
|
title="revocation_of_{}".format(self.identifier),
|
|
|
|
date_of_creation=form_data["date"],
|
|
|
|
comment=form_data["comment"],
|
|
|
|
file=form_data["file"],
|
|
|
|
instance=revocation
|
|
|
|
)
|
|
|
|
return revocation
|
|
|
|
|
2022-02-09 16:02:28 +01:00
|
|
|
def edit_revocation(self, form):
|
|
|
|
""" Updates a revocation of the intervention
|
|
|
|
|
|
|
|
Args:
|
|
|
|
form (EditRevocationModalForm): The form holding the data
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
form_data = form.cleaned_data
|
|
|
|
file = form_data.get("file", None)
|
|
|
|
|
|
|
|
revocation = form.revocation
|
|
|
|
revocation.date = form_data.get("date", None)
|
|
|
|
revocation.comment = form_data.get("comment", None)
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
try:
|
|
|
|
revocation.document.date_of_creation = revocation.date
|
|
|
|
revocation.document.comment = revocation.comment
|
|
|
|
if not isinstance(file, FieldFile):
|
|
|
|
revocation.document.replace_file(file)
|
|
|
|
revocation.document.save()
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
revocation.document = RevocationDocument.objects.create(
|
|
|
|
title="revocation_of_{}".format(self.identifier),
|
|
|
|
date_of_creation=revocation.date,
|
|
|
|
comment=revocation.comment,
|
|
|
|
file=file,
|
|
|
|
instance=revocation
|
|
|
|
)
|
|
|
|
revocation.save()
|
|
|
|
|
|
|
|
return revocation
|
|
|
|
|
2022-02-08 13:16:20 +01:00
|
|
|
def remove_revocation(self, form):
|
|
|
|
""" Removes a revocation from the intervention
|
|
|
|
|
|
|
|
Args:
|
2022-02-08 13:31:40 +01:00
|
|
|
form (RemoveRevocationModalForm): The form holding all relevant data
|
2022-02-08 13:16:20 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
revocation = form.revocation
|
|
|
|
user = form.user
|
|
|
|
with transaction.atomic():
|
|
|
|
revocation.delete()
|
|
|
|
self.mark_as_edited(user, request=form.request, edit_comment=REVOCATION_REMOVED)
|
|
|
|
|
2022-01-31 10:14:46 +01:00
|
|
|
def mark_as_edited(self, performing_user: User, request: HttpRequest = None, edit_comment: str = None, reset_recorded: bool = True):
|
2021-11-17 12:09:49 +01:00
|
|
|
""" In case the object or a related object changed, internal processes need to be started, such as
|
|
|
|
unrecord and uncheck
|
|
|
|
|
|
|
|
Args:
|
|
|
|
performing_user (User): The user which performed the editing action
|
2022-01-31 10:14:46 +01:00
|
|
|
request (HttpRequest): The used request for this action
|
|
|
|
edit_comment (str): Additional comment for the log entry
|
|
|
|
reset_recorded (bool): Whether the record-state of the object should be reset
|
2021-11-17 12:09:49 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-08 13:16:20 +01:00
|
|
|
action = super().mark_as_edited(performing_user, edit_comment=edit_comment)
|
2022-02-04 16:56:08 +01:00
|
|
|
if reset_recorded:
|
|
|
|
self.unrecord(performing_user, request)
|
2021-11-17 12:09:49 +01:00
|
|
|
if self.checked:
|
|
|
|
self.set_unchecked()
|
2022-02-02 15:16:25 +01:00
|
|
|
return action
|
2021-11-17 12:09:49 +01:00
|
|
|
|
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
|
|
|
|
"""
|
2022-02-08 15:25:44 +01:00
|
|
|
# Inform user about revocation
|
|
|
|
if self.legal.revocations.exists():
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
INTERVENTION_HAS_REVOCATIONS_TEMPLATE.format(self.legal.revocations.count()),
|
|
|
|
extra_tags="danger",
|
|
|
|
)
|
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
|
|
|
request = self.set_geometry_conflict_message(request)
|
2021-12-15 13:59:52 +01:00
|
|
|
return request
|
|
|
|
|
2022-01-21 09:02:56 +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
|
|
|
|
"""
|
|
|
|
now_date = timezone.now().date()
|
|
|
|
binding_date = self.legal.binding_date
|
|
|
|
is_binding_date_ready = binding_date is not None and binding_date <= now_date
|
|
|
|
is_recorded = self.recorded is not None
|
|
|
|
is_free_of_revocations = not self.legal.revocations.exists()
|
|
|
|
is_ready = is_binding_date_ready \
|
|
|
|
and is_recorded \
|
|
|
|
and is_free_of_revocations
|
|
|
|
return is_ready
|
|
|
|
|
2022-02-02 11:26:02 +01:00
|
|
|
def get_share_link(self):
|
|
|
|
""" Returns the share url for the object
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
return reverse("intervention:share", args=(self.id, self.access_token))
|
|
|
|
|
2022-02-08 13:16:20 +01:00
|
|
|
def remove_payment(self, form):
|
|
|
|
""" Removes a Payment from the intervention
|
|
|
|
|
|
|
|
Args:
|
2022-02-08 13:31:40 +01:00
|
|
|
form (RemovePaymentModalForm): The form holding all relevant data
|
2022-02-08 13:16:20 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
payment = form.payment
|
|
|
|
user = form.user
|
|
|
|
with transaction.atomic():
|
|
|
|
payment.delete()
|
|
|
|
self.mark_as_edited(user, request=form.request, edit_comment=PAYMENT_REMOVED)
|
2022-04-25 11:16:51 +02:00
|
|
|
self.send_data_to_egon()
|
2022-02-08 13:16:20 +01:00
|
|
|
|
2021-11-11 13:13:05 +01:00
|
|
|
|
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
|
|
|
|
2022-02-04 09:18:46 +01:00
|
|
|
def delete(self, user=None, *args, **kwargs):
|
2021-09-16 09:12:32 +02:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
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-16 09:12:32 +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
|