2021-10-04 09:55:59 +02:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 04.12.20
|
|
|
|
|
|
|
|
"""
|
|
|
|
from dal import autocomplete
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db import transaction
|
|
|
|
from django.urls import reverse_lazy, reverse
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django import forms
|
|
|
|
|
|
|
|
from codelist.models import KonovaCode
|
|
|
|
from codelist.settings import CODELIST_COMPENSATION_FUNDING_ID
|
|
|
|
from compensation.models import Compensation
|
|
|
|
from intervention.inputs import GenerateInput
|
|
|
|
from intervention.models import Intervention
|
|
|
|
from konova.forms import BaseForm, SimpleGeomForm
|
|
|
|
from user.models import UserActionLogEntry, UserAction
|
|
|
|
|
|
|
|
|
|
|
|
class NewCompensationForm(BaseForm):
|
|
|
|
identifier = forms.CharField(
|
|
|
|
label=_("Identifier"),
|
|
|
|
label_suffix="",
|
|
|
|
max_length=255,
|
|
|
|
help_text=_("Generated automatically"),
|
|
|
|
widget=GenerateInput(
|
|
|
|
attrs={
|
|
|
|
"class": "form-control",
|
|
|
|
"url": reverse_lazy("compensation:new-id"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
title = forms.CharField(
|
|
|
|
label=_("Title"),
|
|
|
|
label_suffix="",
|
|
|
|
help_text=_("An explanatory name"),
|
|
|
|
max_length=255,
|
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
|
|
|
"placeholder": _("Compensation XY; Location ABC"),
|
|
|
|
"class": "form-control",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
intervention = forms.ModelChoiceField(
|
|
|
|
label=_("compensates intervention"),
|
|
|
|
label_suffix="",
|
|
|
|
help_text=_("Select the intervention for which this compensation compensates"),
|
|
|
|
queryset=Intervention.objects.filter(
|
|
|
|
deleted=None,
|
|
|
|
),
|
|
|
|
widget=autocomplete.ModelSelect2(
|
|
|
|
url="interventions-autocomplete",
|
|
|
|
attrs={
|
|
|
|
"data-placeholder": _("Intervention"),
|
|
|
|
"data-minimum-input-length": 3,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
fundings = forms.ModelMultipleChoiceField(
|
|
|
|
label=_("Fundings"),
|
|
|
|
label_suffix="",
|
|
|
|
required=False,
|
|
|
|
help_text=_("Select fundings for this compensation"),
|
|
|
|
queryset=KonovaCode.objects.filter(
|
|
|
|
is_archived=False,
|
|
|
|
is_leaf=True,
|
|
|
|
code_lists__in=[CODELIST_COMPENSATION_FUNDING_ID],
|
|
|
|
),
|
|
|
|
widget=autocomplete.ModelSelect2Multiple(
|
|
|
|
url="codes-compensation-funding-autocomplete",
|
|
|
|
attrs={
|
|
|
|
"data-placeholder": _("Funding by..."),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
comment = forms.CharField(
|
|
|
|
label_suffix="",
|
|
|
|
label=_("Comment"),
|
|
|
|
required=False,
|
|
|
|
help_text=_("Additional comment"),
|
|
|
|
widget=forms.Textarea(
|
|
|
|
attrs={
|
|
|
|
"rows": 5,
|
|
|
|
"class": "form-control"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-10-04 13:23:14 +02:00
|
|
|
intervention_id = kwargs.pop("intervention_id", None)
|
2021-10-04 09:55:59 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("New compensation")
|
2021-10-04 13:23:14 +02:00
|
|
|
|
|
|
|
# If the compensation shall directly be initialized from an intervention, we need to fill in the intervention id
|
|
|
|
# and disable the form field.
|
|
|
|
# Furthermore the action_url needs to be set accordingly.
|
|
|
|
if intervention_id is not None:
|
|
|
|
self.initialize_form_field("intervention", intervention_id)
|
|
|
|
self.disable_form_field("intervention")
|
|
|
|
self.action_url = reverse("compensation:new", args=(intervention_id,))
|
|
|
|
self.cancel_redirect = reverse("intervention:open", args=(intervention_id,))
|
|
|
|
else:
|
|
|
|
self.action_url = reverse("compensation:new")
|
|
|
|
self.cancel_redirect = reverse("compensation:index")
|
2021-10-04 09:55:59 +02:00
|
|
|
|
|
|
|
tmp = Compensation()
|
|
|
|
identifier = tmp._generate_new_identifier()
|
|
|
|
self.initialize_form_field("identifier", identifier)
|
|
|
|
|
|
|
|
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)
|
|
|
|
fundings = self.cleaned_data.get("fundings", None)
|
|
|
|
intervention = self.cleaned_data.get("intervention", None)
|
|
|
|
comment = self.cleaned_data.get("comment", None)
|
|
|
|
|
|
|
|
# Create log entry
|
|
|
|
action = UserActionLogEntry.objects.create(
|
|
|
|
user=user,
|
|
|
|
action=UserAction.CREATED,
|
|
|
|
)
|
|
|
|
# Process the geometry form
|
|
|
|
geometry = geom_form.save(action)
|
|
|
|
|
|
|
|
# Finally create main object
|
|
|
|
comp = Compensation.objects.create(
|
|
|
|
identifier=identifier,
|
|
|
|
title=title,
|
|
|
|
intervention=intervention,
|
|
|
|
created=action,
|
|
|
|
geometry=geometry,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
comp.fundings.set(fundings)
|
|
|
|
|
|
|
|
# Add the log entry to the main objects log list
|
|
|
|
comp.log.add(action)
|
|
|
|
return comp
|
|
|
|
|
|
|
|
|