2021-09-27 11:45:13 +02:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 02.12.20
|
|
|
|
|
|
|
|
"""
|
|
|
|
from dal import autocomplete
|
|
|
|
from django import forms
|
2022-02-02 15:16:25 +01:00
|
|
|
|
2022-08-15 10:50:01 +02:00
|
|
|
from konova.forms.base_form import BaseForm
|
2022-02-02 15:16:25 +01:00
|
|
|
from konova.utils.message_templates import EDITED_GENERAL_DATA
|
2022-01-12 12:56:22 +01:00
|
|
|
from user.models import User
|
2021-09-27 11:45:13 +02:00
|
|
|
from django.db import transaction
|
2021-09-27 13:57:56 +02:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2021-09-27 11:45:13 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
from codelist.models import KonovaCode
|
|
|
|
from codelist.settings import CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID, \
|
2022-03-03 12:09:09 +01:00
|
|
|
CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID, CODELIST_HANDLER_ID
|
2021-09-27 13:57:56 +02:00
|
|
|
from intervention.inputs import GenerateInput
|
2022-03-03 12:05:22 +01:00
|
|
|
from intervention.models import Intervention, Legal, Responsibility, Handler
|
2022-08-15 10:50:01 +02:00
|
|
|
from konova.forms.geometry_form import SimpleGeomForm
|
2021-11-16 13:15:15 +01:00
|
|
|
from user.models import UserActionLogEntry
|
2021-09-27 11:45:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
class NewInterventionForm(BaseForm):
|
|
|
|
identifier = forms.CharField(
|
|
|
|
label=_("Identifier"),
|
|
|
|
label_suffix="",
|
|
|
|
max_length=255,
|
|
|
|
help_text=_("Generated automatically"),
|
2021-09-27 13:57:56 +02:00
|
|
|
widget=GenerateInput(
|
|
|
|
attrs={
|
|
|
|
"class": "form-control",
|
|
|
|
"url": reverse_lazy("intervention:new-id"),
|
|
|
|
}
|
|
|
|
)
|
2021-09-27 11:45:13 +02:00
|
|
|
)
|
|
|
|
title = forms.CharField(
|
|
|
|
label=_("Title"),
|
|
|
|
label_suffix="",
|
|
|
|
help_text=_("An explanatory name"),
|
|
|
|
max_length=255,
|
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
2021-09-27 13:57:56 +02:00
|
|
|
"placeholder": _("Construction XY; Location ABC"),
|
|
|
|
"class": "form-control",
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
type = forms.ModelChoiceField(
|
|
|
|
label=_("Process type"),
|
|
|
|
label_suffix="",
|
|
|
|
help_text=_(""),
|
|
|
|
required=False,
|
|
|
|
queryset=KonovaCode.objects.filter(
|
|
|
|
is_archived=False,
|
|
|
|
is_leaf=True,
|
|
|
|
code_lists__in=[CODELIST_PROCESS_TYPE_ID],
|
|
|
|
),
|
|
|
|
widget=autocomplete.ModelSelect2(
|
2022-08-18 11:25:06 +02:00
|
|
|
url="codelist:process-type-autocomplete",
|
2021-09-27 11:45:13 +02:00
|
|
|
attrs={
|
2021-10-06 13:10:10 +02:00
|
|
|
"data-placeholder": _("Click for selection"),
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
laws = forms.ModelMultipleChoiceField(
|
|
|
|
label=_("Law"),
|
|
|
|
label_suffix="",
|
|
|
|
help_text=_("Multiple selection possible"),
|
|
|
|
required=False,
|
|
|
|
queryset=KonovaCode.objects.filter(
|
|
|
|
is_archived=False,
|
|
|
|
is_leaf=True,
|
|
|
|
code_lists__in=[CODELIST_LAW_ID],
|
|
|
|
),
|
|
|
|
widget=autocomplete.ModelSelect2Multiple(
|
2022-08-18 11:25:06 +02:00
|
|
|
url="codelist:law-autocomplete",
|
2021-09-27 11:45:13 +02:00
|
|
|
attrs={
|
2021-10-06 13:10:10 +02:00
|
|
|
"data-placeholder": _("Click for selection"),
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
registration_office = forms.ModelChoiceField(
|
|
|
|
label=_("Registration office"),
|
|
|
|
label_suffix="",
|
|
|
|
required=False,
|
|
|
|
queryset=KonovaCode.objects.filter(
|
|
|
|
is_archived=False,
|
|
|
|
is_leaf=True,
|
|
|
|
code_lists__in=[CODELIST_REGISTRATION_OFFICE_ID],
|
|
|
|
),
|
|
|
|
widget=autocomplete.ModelSelect2(
|
2022-08-18 11:25:06 +02:00
|
|
|
url="codelist:registration-office-autocomplete",
|
2021-09-27 11:45:13 +02:00
|
|
|
attrs={
|
2021-10-06 13:10:10 +02:00
|
|
|
"data-placeholder": _("Click for selection"),
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
conservation_office = forms.ModelChoiceField(
|
|
|
|
label=_("Conservation office"),
|
|
|
|
label_suffix="",
|
|
|
|
required=False,
|
|
|
|
queryset=KonovaCode.objects.filter(
|
|
|
|
is_archived=False,
|
|
|
|
is_leaf=True,
|
|
|
|
code_lists__in=[CODELIST_CONSERVATION_OFFICE_ID],
|
|
|
|
),
|
|
|
|
widget=autocomplete.ModelSelect2(
|
2022-08-18 11:25:06 +02:00
|
|
|
url="codelist:conservation-office-autocomplete",
|
2021-09-27 11:45:13 +02:00
|
|
|
attrs={
|
2021-10-06 13:10:10 +02:00
|
|
|
"data-placeholder": _("Click for selection"),
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
registration_file_number = forms.CharField(
|
|
|
|
label=_("Registration office file number"),
|
|
|
|
label_suffix="",
|
|
|
|
max_length=255,
|
|
|
|
required=False,
|
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
2021-09-27 13:57:56 +02:00
|
|
|
"placeholder": _("ZB-123/ABC.456"),
|
|
|
|
"class": "form-control",
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
conservation_file_number = forms.CharField(
|
|
|
|
label=_("Conservation office file number"),
|
|
|
|
label_suffix="",
|
|
|
|
max_length=255,
|
|
|
|
required=False,
|
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
2021-09-27 13:57:56 +02:00
|
|
|
"placeholder": _("ETS-123/ABC.456"),
|
|
|
|
"class": "form-control",
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2022-03-03 12:05:22 +01:00
|
|
|
handler_type = forms.ModelChoiceField(
|
|
|
|
label=_("Intervention handler type"),
|
|
|
|
label_suffix="",
|
|
|
|
help_text=_("What type of handler is responsible for the intervention?"),
|
|
|
|
required=False,
|
|
|
|
queryset=KonovaCode.objects.filter(
|
|
|
|
is_archived=False,
|
|
|
|
is_leaf=True,
|
2022-03-03 12:09:09 +01:00
|
|
|
code_lists__in=[CODELIST_HANDLER_ID],
|
2022-03-03 12:05:22 +01:00
|
|
|
),
|
|
|
|
widget=autocomplete.ModelSelect2(
|
2022-08-18 11:25:06 +02:00
|
|
|
url="codelist:handler-autocomplete",
|
2022-03-03 12:05:22 +01:00
|
|
|
attrs={
|
|
|
|
"data-placeholder": _("Click for selection"),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
handler_detail = forms.CharField(
|
|
|
|
label=_("Intervention handler detail"),
|
2021-09-27 11:45:13 +02:00
|
|
|
label_suffix="",
|
|
|
|
max_length=255,
|
|
|
|
required=False,
|
2022-03-03 12:05:22 +01:00
|
|
|
help_text=_("Detail input on the handler"),
|
2021-09-27 11:45:13 +02:00
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
2021-09-27 13:57:56 +02:00
|
|
|
"placeholder": _("Company Mustermann"),
|
|
|
|
"class": "form-control",
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2022-03-03 12:05:22 +01:00
|
|
|
|
2021-09-27 11:45:13 +02:00
|
|
|
registration_date = forms.DateField(
|
|
|
|
label=_("Registration date"),
|
|
|
|
label_suffix=_(""),
|
|
|
|
required=False,
|
|
|
|
widget=forms.DateInput(
|
|
|
|
attrs={
|
|
|
|
"type": "date",
|
2021-09-27 13:57:56 +02:00
|
|
|
"class": "form-control",
|
2021-09-27 11:45:13 +02:00
|
|
|
},
|
|
|
|
format="%d.%m.%Y"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
binding_date = forms.DateField(
|
|
|
|
label=_("Binding on"),
|
|
|
|
label_suffix=_(""),
|
|
|
|
required=False,
|
|
|
|
widget=forms.DateInput(
|
|
|
|
attrs={
|
|
|
|
"type": "date",
|
2021-09-27 13:57:56 +02:00
|
|
|
"class": "form-control",
|
2021-09-27 11:45:13 +02:00
|
|
|
},
|
|
|
|
format="%d.%m.%Y"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
comment = forms.CharField(
|
|
|
|
label_suffix="",
|
|
|
|
label=_("Comment"),
|
|
|
|
required=False,
|
|
|
|
help_text=_("Additional comment"),
|
|
|
|
widget=forms.Textarea(
|
|
|
|
attrs={
|
|
|
|
"rows": 5,
|
2021-09-27 13:57:56 +02:00
|
|
|
"class": "form-control"
|
2021-09-27 11:45:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("New intervention")
|
|
|
|
self.action_url = reverse("intervention:new")
|
|
|
|
self.cancel_redirect = reverse("intervention:index")
|
|
|
|
|
|
|
|
tmp_intervention = Intervention()
|
2021-10-05 16:35:24 +02:00
|
|
|
identifier = tmp_intervention.generate_new_identifier()
|
2021-09-27 11:45:13 +02:00
|
|
|
self.initialize_form_field("identifier", identifier)
|
|
|
|
|
2022-04-19 17:22:06 +02:00
|
|
|
def is_valid(self):
|
|
|
|
super_valid_result = super().is_valid()
|
|
|
|
return super_valid_result
|
|
|
|
|
2021-09-27 11:45:13 +02:00
|
|
|
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)
|
|
|
|
_type = self.cleaned_data.get("type", None)
|
|
|
|
laws = self.cleaned_data.get("laws", 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-09-27 11:45:13 +02:00
|
|
|
registration_office = self.cleaned_data.get("registration_office", None)
|
|
|
|
conservation_office = self.cleaned_data.get("conservation_office", None)
|
|
|
|
conservation_file_number = self.cleaned_data.get("conservation_file_number", None)
|
|
|
|
registration_file_number = self.cleaned_data.get("registration_file_number", None)
|
|
|
|
binding_date = self.cleaned_data.get("binding_date", None)
|
|
|
|
registration_date = self.cleaned_data.get("registration_date", 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-09-27 11:45:13 +02:00
|
|
|
|
|
|
|
# Create legal data object (without M2M laws first)
|
2021-11-15 17:09:17 +01:00
|
|
|
legal_data = Legal.objects.create(
|
2021-09-27 11:45:13 +02:00
|
|
|
registration_date=registration_date,
|
|
|
|
binding_date=binding_date,
|
|
|
|
process_type=_type,
|
|
|
|
)
|
|
|
|
# Then add the M2M laws to the object
|
|
|
|
legal_data.laws.set(laws)
|
|
|
|
|
2022-03-03 12:05:22 +01:00
|
|
|
handler = Handler.objects.create(
|
|
|
|
type=handler_type,
|
|
|
|
detail=handler_detail
|
|
|
|
)
|
2021-09-27 11:45:13 +02:00
|
|
|
# Create responsible data object
|
2021-11-15 17:09:17 +01:00
|
|
|
responsibility_data = Responsibility.objects.create(
|
2021-09-27 11:45:13 +02:00
|
|
|
registration_office=registration_office,
|
|
|
|
conservation_office=conservation_office,
|
|
|
|
registration_file_number=registration_file_number,
|
|
|
|
conservation_file_number=conservation_file_number,
|
|
|
|
handler=handler,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Finally create main object, holding the other objects
|
|
|
|
intervention = Intervention.objects.create(
|
|
|
|
identifier=identifier,
|
|
|
|
title=title,
|
|
|
|
responsible=responsibility_data,
|
|
|
|
legal=legal_data,
|
|
|
|
created=action,
|
2022-11-25 08:27:42 +01:00
|
|
|
modified=action,
|
2021-09-27 11:45:13 +02:00
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add the log entry to the main objects log list
|
|
|
|
intervention.log.add(action)
|
|
|
|
|
|
|
|
# Add the performing user as the first user having access to the data
|
2022-02-18 11:02:40 +01:00
|
|
|
intervention.share_with_user(user)
|
2022-11-23 13:51:05 +01:00
|
|
|
|
|
|
|
# Process the geometry form (NOT ATOMIC TRANSACTION DUE TO CELERY!)
|
|
|
|
geometry = geom_form.save(action)
|
|
|
|
intervention.geometry = geometry
|
|
|
|
intervention.save()
|
|
|
|
|
2021-09-27 11:45:13 +02:00
|
|
|
return intervention
|
|
|
|
|
|
|
|
|
|
|
|
class EditInterventionForm(NewInterventionForm):
|
|
|
|
""" Subclasses NewInterventionForm
|
|
|
|
|
|
|
|
Simply adds initializing of a provided self.instance object into the form fields
|
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if self.instance is not None:
|
|
|
|
self.action_url = reverse("intervention:edit", args=(self.instance.id,))
|
2021-10-13 09:26:46 +02:00
|
|
|
self.cancel_redirect = reverse("intervention:detail", args=(self.instance.id,))
|
2021-09-27 11:45:13 +02:00
|
|
|
self.form_title = _("Edit intervention")
|
|
|
|
self.form_caption = ""
|
|
|
|
|
|
|
|
reg_date = self.instance.legal.registration_date
|
|
|
|
bind_date = self.instance.legal.binding_date
|
|
|
|
if reg_date is not None:
|
|
|
|
reg_date = reg_date.isoformat()
|
|
|
|
if bind_date is not None:
|
|
|
|
bind_date = bind_date.isoformat()
|
|
|
|
|
|
|
|
# Initialize form data
|
|
|
|
form_data = {
|
|
|
|
"identifier": self.instance.identifier,
|
|
|
|
"title": self.instance.title,
|
|
|
|
"type": self.instance.legal.process_type,
|
|
|
|
"laws": list(self.instance.legal.laws.values_list("id", flat=True)),
|
2022-03-03 12:05:22 +01:00
|
|
|
"handler_type": self.instance.responsible.handler.type,
|
|
|
|
"handler_detail": self.instance.responsible.handler.detail,
|
2021-09-27 11:45:13 +02:00
|
|
|
"registration_office": self.instance.responsible.registration_office,
|
|
|
|
"registration_file_number": self.instance.responsible.registration_file_number,
|
|
|
|
"conservation_office": self.instance.responsible.conservation_office,
|
|
|
|
"conservation_file_number": self.instance.responsible.conservation_file_number,
|
|
|
|
"registration_date": reg_date,
|
|
|
|
"binding_date": bind_date,
|
|
|
|
"comment": self.instance.comment,
|
|
|
|
}
|
2021-09-27 13:57:56 +02:00
|
|
|
disabled_fields = []
|
2021-09-27 11:45:13 +02:00
|
|
|
self.load_initial_data(
|
|
|
|
form_data,
|
2021-09-27 13:57:56 +02:00
|
|
|
disabled_fields
|
2021-09-27 11:45:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def save(self, user: User, geom_form: SimpleGeomForm):
|
|
|
|
""" Overwrite instance with new form data
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user ():
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
with transaction.atomic():
|
|
|
|
identifier = self.cleaned_data.get("identifier", None)
|
|
|
|
title = self.cleaned_data.get("title", None)
|
|
|
|
process_type = self.cleaned_data.get("type", None)
|
|
|
|
laws = self.cleaned_data.get("laws", 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-09-27 11:45:13 +02:00
|
|
|
registration_office = self.cleaned_data.get("registration_office", None)
|
|
|
|
registration_file_number = self.cleaned_data.get("registration_file_number", None)
|
|
|
|
conservation_office = self.cleaned_data.get("conservation_office", None)
|
|
|
|
conservation_file_number = self.cleaned_data.get("conservation_file_number", None)
|
|
|
|
registration_date = self.cleaned_data.get("registration_date", None)
|
|
|
|
binding_date = self.cleaned_data.get("binding_date", None)
|
|
|
|
comment = self.cleaned_data.get("comment", None)
|
|
|
|
|
|
|
|
self.instance.legal.process_type = process_type
|
|
|
|
self.instance.legal.registration_date = registration_date
|
|
|
|
self.instance.legal.binding_date = binding_date
|
|
|
|
self.instance.legal.laws.set(laws)
|
|
|
|
self.instance.legal.save()
|
|
|
|
|
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-09-27 11:45:13 +02:00
|
|
|
self.instance.responsible.registration_office = registration_office
|
|
|
|
self.instance.responsible.registration_file_number = registration_file_number
|
|
|
|
self.instance.responsible.conservation_office = conservation_office
|
|
|
|
self.instance.responsible.conservation_file_number = conservation_file_number
|
|
|
|
self.instance.responsible.save()
|
|
|
|
|
2022-02-02 15:16:25 +01:00
|
|
|
user_action = self.instance.mark_as_edited(user, edit_comment=EDITED_GENERAL_DATA)
|
2021-09-27 11:45:13 +02:00
|
|
|
|
|
|
|
self.instance.log.add(user_action)
|
|
|
|
|
|
|
|
self.instance.identifier = identifier
|
|
|
|
self.instance.title = title
|
|
|
|
self.instance.comment = comment
|
|
|
|
self.instance.modified = user_action
|
|
|
|
self.instance.save()
|
|
|
|
|
2022-11-23 13:51:05 +01:00
|
|
|
# Process the geometry form (NOT ATOMIC TRANSACTION DUE TO CELERY!)
|
|
|
|
geometry = geom_form.save(user_action)
|
|
|
|
self.instance.geometry = geometry
|
|
|
|
self.instance.save()
|
|
|
|
|
2021-09-27 11:45:13 +02:00
|
|
|
return self.instance
|
|
|
|
|