2021-10-06 16:00:17 +02:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 06.10.21
|
|
|
|
|
|
|
|
"""
|
2022-01-12 12:56:22 +01:00
|
|
|
from user.models import User
|
2021-10-06 16:00:17 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.urls import reverse, reverse_lazy
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-11-15 09:59:01 +01:00
|
|
|
|
2022-08-18 09:54:49 +02:00
|
|
|
from compensation.forms.mixins import CompensationResponsibleFormMixin, PikCompensationFormMixin
|
|
|
|
from compensation.forms.compensation import AbstractCompensationForm
|
2021-11-15 17:09:17 +01:00
|
|
|
from ema.models import Ema, EmaDocument
|
2022-03-03 12:05:22 +01:00
|
|
|
from intervention.models import Responsibility, Handler
|
2022-08-15 10:50:01 +02:00
|
|
|
from konova.forms import SimpleGeomForm
|
|
|
|
from konova.forms.modals import NewDocumentModalForm
|
2021-11-16 13:15:15 +01:00
|
|
|
from user.models import UserActionLogEntry
|
2021-10-06 16:00:17 +02:00
|
|
|
|
|
|
|
|
2022-05-31 13:33:44 +02:00
|
|
|
class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin, PikCompensationFormMixin):
|
2021-10-06 16:00:17 +02:00
|
|
|
""" Form for creating new EMA objects.
|
|
|
|
|
|
|
|
Inherits basic form fields from AbstractCompensationForm and additional from CompensationResponsibleFormMixin.
|
|
|
|
Second holds self.instance.response related fields
|
|
|
|
|
|
|
|
"""
|
|
|
|
field_order = [
|
|
|
|
"identifier",
|
|
|
|
"title",
|
|
|
|
"conservation_office",
|
|
|
|
"conservation_file_number",
|
2022-05-31 13:33:44 +02:00
|
|
|
"is_pik",
|
2022-03-03 12:05:22 +01:00
|
|
|
"handler_type",
|
|
|
|
"handler_detail",
|
2021-10-06 16:00:17 +02:00
|
|
|
"comment",
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("New EMA")
|
|
|
|
|
|
|
|
self.action_url = reverse("ema:new")
|
|
|
|
self.cancel_redirect = reverse("ema:index")
|
|
|
|
|
|
|
|
tmp = Ema()
|
|
|
|
identifier = tmp.generate_new_identifier()
|
|
|
|
self.initialize_form_field("identifier", identifier)
|
|
|
|
self.fields["identifier"].widget.attrs["url"] = reverse_lazy("ema:new-id")
|
|
|
|
self.fields["title"].widget.attrs["placeholder"] = _("Compensation XY; Location ABC")
|
|
|
|
|
|
|
|
def save(self, user: User, geom_form: SimpleGeomForm):
|
|
|
|
with transaction.atomic():
|
|
|
|
# Fetch data from cleaned POST values
|
|
|
|
identifier = self.cleaned_data.get("identifier", None)
|
|
|
|
title = self.cleaned_data.get("title", None)
|
2022-03-03 12:05:22 +01:00
|
|
|
handler_type = self.cleaned_data.get("handler_type", None)
|
|
|
|
handler_detail = self.cleaned_data.get("handler_detail", None)
|
2021-10-06 16:00:17 +02:00
|
|
|
conservation_office = self.cleaned_data.get("conservation_office", None)
|
|
|
|
conservation_file_number = self.cleaned_data.get("conservation_file_number", None)
|
2022-05-31 13:33:44 +02:00
|
|
|
is_pik = self.cleaned_data.get("is_pik", None)
|
2021-10-06 16:00:17 +02:00
|
|
|
comment = self.cleaned_data.get("comment", None)
|
|
|
|
|
|
|
|
# Create log entry
|
2021-11-16 13:15:15 +01:00
|
|
|
action = UserActionLogEntry.get_created_action(user)
|
2021-10-06 16:00:17 +02:00
|
|
|
|
2022-03-03 12:05:22 +01:00
|
|
|
handler = Handler.objects.create(
|
|
|
|
type=handler_type,
|
|
|
|
detail=handler_detail
|
|
|
|
)
|
2021-11-15 17:09:17 +01:00
|
|
|
responsible = Responsibility.objects.create(
|
2021-10-06 16:00:17 +02:00
|
|
|
handler=handler,
|
|
|
|
conservation_file_number=conservation_file_number,
|
|
|
|
conservation_office=conservation_office,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Finally create main object
|
2023-09-05 11:24:29 +02:00
|
|
|
ema = Ema.objects.create(
|
2021-10-06 16:00:17 +02:00
|
|
|
identifier=identifier,
|
|
|
|
title=title,
|
|
|
|
responsible=responsible,
|
|
|
|
created=action,
|
2022-11-25 08:27:42 +01:00
|
|
|
modified=action,
|
2021-10-06 16:00:17 +02:00
|
|
|
comment=comment,
|
2022-05-31 13:33:44 +02:00
|
|
|
is_pik=is_pik,
|
2021-10-06 16:00:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Add the creating user to the list of shared users
|
2023-09-05 11:24:29 +02:00
|
|
|
ema.share_with_user(user)
|
2021-10-06 16:00:17 +02:00
|
|
|
|
|
|
|
# Add the log entry to the main objects log list
|
2023-09-05 11:24:29 +02:00
|
|
|
ema.log.add(action)
|
2022-11-23 13:51:05 +01:00
|
|
|
|
|
|
|
# Process the geometry form (NOT ATOMIC TRANSACTION DUE TO CELERY!)
|
|
|
|
geometry = geom_form.save(action)
|
2023-09-05 11:24:29 +02:00
|
|
|
ema.geometry = geometry
|
|
|
|
ema.save()
|
|
|
|
return ema
|
2021-10-06 16:00:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditEmaForm(NewEmaForm):
|
|
|
|
""" Form for editing EMAs
|
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("Edit EMA")
|
|
|
|
|
|
|
|
self.action_url = reverse("ema:edit", args=(self.instance.id,))
|
2021-10-13 09:26:46 +02:00
|
|
|
self.cancel_redirect = reverse("ema:detail", args=(self.instance.id,))
|
2021-10-06 16:00:17 +02:00
|
|
|
|
|
|
|
self.fields["identifier"].widget.attrs["url"] = reverse_lazy("ema:new-id")
|
|
|
|
self.fields["title"].widget.attrs["placeholder"] = _("Compensation XY; Location ABC")
|
|
|
|
|
|
|
|
# Initialize form data
|
|
|
|
form_data = {
|
|
|
|
"identifier": self.instance.identifier,
|
|
|
|
"title": self.instance.title,
|
2022-03-03 12:05:22 +01:00
|
|
|
"handler_type": self.instance.responsible.handler.type,
|
|
|
|
"handler_detail": self.instance.responsible.handler.detail,
|
2021-10-06 16:00:17 +02:00
|
|
|
"conservation_office": self.instance.responsible.conservation_office,
|
|
|
|
"conservation_file_number": self.instance.responsible.conservation_file_number,
|
|
|
|
"comment": self.instance.comment,
|
2022-05-31 13:33:44 +02:00
|
|
|
"is_pik": self.instance.is_pik,
|
2021-10-06 16:00:17 +02:00
|
|
|
}
|
|
|
|
disabled_fields = []
|
|
|
|
self.load_initial_data(
|
|
|
|
form_data,
|
|
|
|
disabled_fields
|
|
|
|
)
|
|
|
|
|
|
|
|
def save(self, user: User, geom_form: SimpleGeomForm):
|
|
|
|
with transaction.atomic():
|
|
|
|
# Fetch data from cleaned POST values
|
|
|
|
identifier = self.cleaned_data.get("identifier", None)
|
|
|
|
title = self.cleaned_data.get("title", None)
|
2022-03-03 12:05:22 +01:00
|
|
|
handler_type = self.cleaned_data.get("handler_type", None)
|
|
|
|
handler_detail = self.cleaned_data.get("handler_detail", None)
|
2021-10-06 16:00:17 +02:00
|
|
|
conservation_office = self.cleaned_data.get("conservation_office", None)
|
|
|
|
conservation_file_number = self.cleaned_data.get("conservation_file_number", None)
|
|
|
|
comment = self.cleaned_data.get("comment", None)
|
2022-05-31 13:33:44 +02:00
|
|
|
is_pik = self.cleaned_data.get("is_pik", None)
|
2021-10-06 16:00:17 +02:00
|
|
|
|
|
|
|
# Create log entry
|
2021-11-16 13:15:15 +01:00
|
|
|
action = UserActionLogEntry.get_edited_action(user)
|
2021-10-06 16:00:17 +02:00
|
|
|
|
|
|
|
# Update responsible data
|
2022-03-03 12:05:22 +01:00
|
|
|
self.instance.responsible.handler.type = handler_type
|
|
|
|
self.instance.responsible.handler.detail = handler_detail
|
|
|
|
self.instance.responsible.handler.save()
|
2021-10-06 16:00:17 +02:00
|
|
|
self.instance.responsible.conservation_office = conservation_office
|
|
|
|
self.instance.responsible.conservation_file_number = conservation_file_number
|
|
|
|
self.instance.responsible.save()
|
|
|
|
|
|
|
|
# Update main oject data
|
|
|
|
self.instance.identifier = identifier
|
|
|
|
self.instance.title = title
|
|
|
|
self.instance.comment = comment
|
2022-05-31 13:33:44 +02:00
|
|
|
self.instance.is_pik = is_pik
|
2021-10-06 16:00:17 +02:00
|
|
|
self.instance.modified = action
|
|
|
|
self.instance.save()
|
|
|
|
|
|
|
|
# Add the log entry to the main objects log list
|
|
|
|
self.instance.log.add(action)
|
2022-11-23 13:51:05 +01:00
|
|
|
|
|
|
|
# Process the geometry form (NOT ATOMIC TRANSACTION DUE TO CELERY!)
|
|
|
|
geometry = geom_form.save(action)
|
|
|
|
self.instance.geometry = geometry
|
|
|
|
self.instance.save()
|
2021-10-06 16:00:17 +02:00
|
|
|
return self.instance
|
2021-11-15 17:09:17 +01:00
|
|
|
|
|
|
|
|
2022-02-10 10:21:18 +01:00
|
|
|
class NewEmaDocumentModalForm(NewDocumentModalForm):
|
2021-11-15 17:09:17 +01:00
|
|
|
document_model = EmaDocument
|