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
|
|
|
|
|
|
|
|
"""
|
2021-11-15 09:59:01 +01:00
|
|
|
from dal import autocomplete
|
|
|
|
from django import forms
|
2021-10-06 16:00:17 +02:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
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
|
|
|
|
2021-10-06 16:00:17 +02:00
|
|
|
from compensation.forms.forms import AbstractCompensationForm, CompensationResponsibleFormMixin
|
2021-11-15 17:09:17 +01:00
|
|
|
from ema.models import Ema, EmaDocument
|
|
|
|
from intervention.models import Responsibility
|
|
|
|
from konova.forms import SimpleGeomForm, NewDocumentForm
|
2021-11-16 13:15:15 +01:00
|
|
|
from user.models import UserActionLogEntry
|
2021-10-06 16:00:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin):
|
|
|
|
""" 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",
|
|
|
|
"handler",
|
|
|
|
"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)
|
|
|
|
handler = self.cleaned_data.get("handler", None)
|
|
|
|
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)
|
|
|
|
|
|
|
|
# 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
|
|
|
# Process the geometry form
|
|
|
|
geometry = geom_form.save(action)
|
|
|
|
|
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
|
|
|
|
acc = Ema.objects.create(
|
|
|
|
identifier=identifier,
|
|
|
|
title=title,
|
|
|
|
responsible=responsible,
|
|
|
|
created=action,
|
|
|
|
geometry=geometry,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add the creating user to the list of shared users
|
2021-11-11 15:09:03 +01:00
|
|
|
acc.share_with(user)
|
2021-10-06 16:00:17 +02:00
|
|
|
|
|
|
|
# Add the log entry to the main objects log list
|
|
|
|
acc.log.add(action)
|
|
|
|
return acc
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
"handler": self.instance.responsible.handler,
|
|
|
|
"conservation_office": self.instance.responsible.conservation_office,
|
|
|
|
"conservation_file_number": self.instance.responsible.conservation_file_number,
|
|
|
|
"comment": self.instance.comment,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
handler = self.cleaned_data.get("handler", None)
|
|
|
|
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)
|
|
|
|
|
|
|
|
# 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
|
|
|
# Process the geometry form
|
|
|
|
geometry = geom_form.save(action)
|
|
|
|
|
|
|
|
# Update responsible data
|
|
|
|
self.instance.responsible.handler = handler
|
|
|
|
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.geometry = geometry
|
|
|
|
self.instance.comment = comment
|
|
|
|
self.instance.modified = action
|
|
|
|
self.instance.save()
|
|
|
|
|
|
|
|
# Add the log entry to the main objects log list
|
|
|
|
self.instance.log.add(action)
|
|
|
|
return self.instance
|
2021-11-15 17:09:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NewEmaDocumentForm(NewDocumentForm):
|
|
|
|
document_model = EmaDocument
|