mpeltriaux
d785285805
* refactors konova/autocompletes.py by splitting into individual files and moving them to fitting apps * autocomplete files now live in APPNAME/autocomplete/...
155 lines
5.3 KiB
Python
155 lines
5.3 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: ksp-servicestelle@sgdnord.rlp.de
|
|
Created on: 18.08.22
|
|
|
|
"""
|
|
from dal import autocomplete
|
|
from django import forms
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from codelist.models import KonovaCode
|
|
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_COMPENSATION_ACTION_DETAIL_ID
|
|
from intervention.inputs import CompensationActionTreeCheckboxSelectMultiple
|
|
from konova.forms.modals import BaseModalForm, RemoveModalForm
|
|
from konova.utils.message_templates import COMPENSATION_ACTION_EDITED, ADDED_COMPENSATION_ACTION
|
|
|
|
|
|
class NewCompensationActionModalForm(BaseModalForm):
|
|
""" Form handling action related input
|
|
|
|
Compensation actions are the actions performed on the area, which shall be compensated. Actions will change the
|
|
surface of the area, the biotopes, and have an environmental impact. With actions the before-after states can change
|
|
(not in the process logic in Konova, but in the real world).
|
|
|
|
"""
|
|
from compensation.models import UnitChoices
|
|
action_type = forms.MultipleChoiceField(
|
|
label=_("Action Type"),
|
|
label_suffix="",
|
|
required=True,
|
|
help_text=_("An action can consist of multiple different action types. All the selected action types are expected to be performed according to the amount and unit below on this form."),
|
|
choices=[],
|
|
widget=CompensationActionTreeCheckboxSelectMultiple(),
|
|
)
|
|
action_type_details = forms.ModelMultipleChoiceField(
|
|
label=_("Action Type detail"),
|
|
label_suffix="",
|
|
required=False,
|
|
help_text=_("Select the action type detail"),
|
|
queryset=KonovaCode.objects.filter(
|
|
is_archived=False,
|
|
is_leaf=True,
|
|
code_lists__in=[CODELIST_COMPENSATION_ACTION_DETAIL_ID],
|
|
),
|
|
widget=autocomplete.ModelSelect2Multiple(
|
|
url="codelist:compensation-action-detail-autocomplete",
|
|
attrs={
|
|
"data-placeholder": _("Action Type detail"),
|
|
}
|
|
),
|
|
)
|
|
unit = forms.ChoiceField(
|
|
label=_("Unit"),
|
|
label_suffix="",
|
|
required=True,
|
|
help_text=_("Select the unit"),
|
|
choices=UnitChoices.choices,
|
|
widget=forms.Select(
|
|
attrs={
|
|
"class": "form-control"
|
|
}
|
|
)
|
|
)
|
|
amount = forms.DecimalField(
|
|
label=_("Amount"),
|
|
label_suffix="",
|
|
required=True,
|
|
help_text=_("Insert the amount"),
|
|
decimal_places=2,
|
|
min_value=0.00,
|
|
widget=forms.NumberInput(
|
|
attrs={
|
|
"class": "form-control",
|
|
"placeholder": "0,00",
|
|
}
|
|
)
|
|
)
|
|
comment = forms.CharField(
|
|
required=False,
|
|
label=_("Comment"),
|
|
label_suffix=_(""),
|
|
help_text=_("Additional comment"),
|
|
widget=forms.Textarea(
|
|
attrs={
|
|
"rows": 5,
|
|
"class": "form-control",
|
|
}
|
|
)
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.form_title = _("New action")
|
|
self.form_caption = _("Insert data for the new action")
|
|
choices =KonovaCode.objects.filter(
|
|
code_lists__in=[CODELIST_COMPENSATION_ACTION_ID],
|
|
is_archived=False,
|
|
is_leaf=True,
|
|
).values_list("id", flat=True)
|
|
choices = [
|
|
(choice, choice)
|
|
for choice in choices
|
|
]
|
|
self.fields["action_type"].choices = choices
|
|
|
|
def save(self):
|
|
action = self.instance.add_action(self)
|
|
self.instance.mark_as_edited(self.user, self.request, ADDED_COMPENSATION_ACTION)
|
|
return action
|
|
|
|
|
|
class EditCompensationActionModalForm(NewCompensationActionModalForm):
|
|
action = None
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.action = kwargs.pop("action", None)
|
|
super().__init__(*args, **kwargs)
|
|
self.form_title = _("Edit action")
|
|
form_data = {
|
|
"action_type": list(self.action.action_type.values_list("id", flat=True)),
|
|
"action_type_details": self.action.action_type_details.all(),
|
|
"amount": self.action.amount,
|
|
"unit": self.action.unit,
|
|
"comment": self.action.comment,
|
|
}
|
|
self.load_initial_data(form_data)
|
|
|
|
def save(self):
|
|
action = self.action
|
|
action.action_type.set(self.cleaned_data.get("action_type", []))
|
|
action.action_type_details.set(self.cleaned_data.get("action_type_details", []))
|
|
action.amount = self.cleaned_data.get("amount", None)
|
|
action.unit = self.cleaned_data.get("unit", None)
|
|
action.comment = self.cleaned_data.get("comment", None)
|
|
action.save()
|
|
self.instance.mark_as_edited(self.user, self.request, edit_comment=COMPENSATION_ACTION_EDITED)
|
|
return action
|
|
|
|
|
|
class RemoveCompensationActionModalForm(RemoveModalForm):
|
|
""" Removing modal form for CompensationAction
|
|
|
|
Can be used for anything, where removing shall be confirmed by the user a second time.
|
|
|
|
"""
|
|
action = None
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
action = kwargs.pop("action", None)
|
|
self.action = action
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def save(self):
|
|
self.instance.remove_action(self) |