2021-07-01 13:36:07 +02:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 04.12.20
|
|
|
|
|
|
|
|
"""
|
2021-08-19 09:06:35 +02:00
|
|
|
from bootstrap_modal_forms.utils import is_ajax
|
2021-07-26 10:23:09 +02:00
|
|
|
from django import forms
|
2021-08-03 17:22:41 +02:00
|
|
|
from django.contrib import messages
|
2021-07-26 10:23:09 +02:00
|
|
|
from django.db import transaction
|
2021-08-19 09:06:35 +02:00
|
|
|
from django.http import HttpRequest, HttpResponseRedirect
|
|
|
|
from django.shortcuts import render
|
2021-07-26 10:23:09 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2021-08-04 10:44:02 +02:00
|
|
|
from compensation.models import Payment, CompensationState, CompensationAction, UnitChoices
|
2021-08-03 17:22:41 +02:00
|
|
|
from konova.contexts import BaseContext
|
2021-07-26 10:23:09 +02:00
|
|
|
from konova.forms import BaseForm, BaseModalForm
|
2021-08-03 13:13:01 +02:00
|
|
|
from konova.models import Deadline, DeadlineType
|
2021-08-03 17:22:41 +02:00
|
|
|
from konova.utils.message_templates import FORM_INVALID
|
|
|
|
from user.models import UserActionLogEntry, UserAction
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class NewCompensationForm(BaseForm):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-07-26 10:23:09 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2021-08-05 12:54:28 +02:00
|
|
|
def save(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
user_action = UserActionLogEntry.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
action=UserAction.CREATED
|
|
|
|
)
|
|
|
|
# Save action to log
|
|
|
|
|
2021-07-26 10:23:09 +02:00
|
|
|
|
|
|
|
class NewPaymentForm(BaseModalForm):
|
2021-07-26 11:29:05 +02:00
|
|
|
amount = forms.DecimalField(
|
|
|
|
min_value=0.00,
|
|
|
|
decimal_places=2,
|
2021-07-26 10:23:09 +02:00
|
|
|
label=_("Amount"),
|
|
|
|
label_suffix=_(""),
|
2021-07-26 11:29:05 +02:00
|
|
|
help_text=_("Amount in Euro"),
|
2021-07-26 10:23:09 +02:00
|
|
|
)
|
|
|
|
due = forms.DateField(
|
|
|
|
label=_("Due on"),
|
|
|
|
label_suffix=_(""),
|
2021-07-26 11:29:05 +02:00
|
|
|
help_text=_("Due on which date"),
|
2021-07-26 10:23:09 +02:00
|
|
|
widget=forms.DateInput(
|
|
|
|
attrs={
|
|
|
|
"type": "date",
|
|
|
|
"data-provide": "datepicker",
|
|
|
|
},
|
|
|
|
format="%d.%m.%Y"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
transfer_note = forms.CharField(
|
2021-08-04 13:32:35 +02:00
|
|
|
max_length=200,
|
2021-07-26 10:23:09 +02:00
|
|
|
required=False,
|
|
|
|
label_suffix=_(""),
|
2021-07-26 11:29:05 +02:00
|
|
|
label=_("Transfer note"),
|
|
|
|
help_text=_("Note for money transfer")
|
2021-07-26 10:23:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2021-07-26 11:29:05 +02:00
|
|
|
self.intervention = self.instance
|
2021-07-26 10:23:09 +02:00
|
|
|
self.form_title = _("Payment")
|
|
|
|
self.form_caption = _("Add a payment for intervention '{}'").format(self.intervention.title)
|
2021-08-11 14:17:43 +02:00
|
|
|
self.add_placeholder_for_field("amount", "0,00")
|
2021-07-26 10:23:09 +02:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
with transaction.atomic():
|
2021-08-05 12:54:28 +02:00
|
|
|
created_action = UserActionLogEntry.objects.create(
|
2021-08-02 11:52:20 +02:00
|
|
|
user=self.user,
|
2021-08-03 17:22:41 +02:00
|
|
|
action=UserAction.CREATED,
|
2021-08-02 11:52:20 +02:00
|
|
|
)
|
2021-08-05 12:54:28 +02:00
|
|
|
edited_action = UserActionLogEntry.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
action=UserAction.EDITED,
|
|
|
|
comment=_("Added payment"),
|
|
|
|
)
|
2021-07-26 10:23:09 +02:00
|
|
|
pay = Payment.objects.create(
|
2021-08-05 12:54:28 +02:00
|
|
|
created=created_action,
|
2021-07-26 10:23:09 +02:00
|
|
|
amount=self.cleaned_data.get("amount", -1),
|
|
|
|
due_on=self.cleaned_data.get("due", None),
|
|
|
|
comment=self.cleaned_data.get("transfer_note", None),
|
|
|
|
intervention=self.intervention,
|
|
|
|
)
|
2021-08-05 12:54:28 +02:00
|
|
|
self.intervention.log.add(edited_action)
|
2021-08-19 13:02:31 +02:00
|
|
|
self.intervention.modified = edited_action
|
|
|
|
self.intervention.save()
|
2021-08-03 13:13:01 +02:00
|
|
|
return pay
|
|
|
|
|
|
|
|
|
|
|
|
class NewStateModalForm(BaseModalForm):
|
|
|
|
biotope_type = forms.CharField(
|
|
|
|
label=_("Biotope Type"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
|
|
|
help_text=_("Select the biotope type")
|
|
|
|
)
|
|
|
|
surface = forms.DecimalField(
|
|
|
|
min_value=0.00,
|
|
|
|
decimal_places=2,
|
|
|
|
label=_("Surface"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
|
|
|
help_text=_("in m²")
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("New state")
|
|
|
|
self.form_caption = _("Insert data for the new state")
|
2021-08-11 14:17:43 +02:00
|
|
|
self.add_placeholder_for_field("surface", "0,00")
|
2021-08-03 13:13:01 +02:00
|
|
|
|
|
|
|
def save(self, is_before_state: bool = False):
|
|
|
|
with transaction.atomic():
|
2021-08-05 12:54:28 +02:00
|
|
|
user_action = UserActionLogEntry.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
action=UserAction.EDITED,
|
|
|
|
comment=_("Added state")
|
|
|
|
)
|
|
|
|
self.instance.log.add(user_action)
|
2021-08-19 13:02:31 +02:00
|
|
|
self.instance.modified = user_action
|
|
|
|
self.instance.save()
|
2021-08-05 12:54:28 +02:00
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
state = CompensationState.objects.create(
|
|
|
|
biotope_type=self.cleaned_data["biotope_type"],
|
|
|
|
surface=self.cleaned_data["surface"],
|
|
|
|
)
|
|
|
|
if is_before_state:
|
|
|
|
self.instance.before_states.add(state)
|
|
|
|
else:
|
|
|
|
self.instance.after_states.add(state)
|
|
|
|
return state
|
|
|
|
|
2021-08-03 17:22:41 +02:00
|
|
|
def process_request(self, request: HttpRequest, msg_success: str = _("Object removed"), msg_error: str = FORM_INVALID, redirect_url: str = None):
|
|
|
|
""" Generic processing of request
|
|
|
|
|
|
|
|
Wraps the request processing logic, so we don't need the same code everywhere a RemoveModalForm is being used
|
|
|
|
|
2021-08-10 13:12:15 +02:00
|
|
|
+++
|
|
|
|
The generic method from super class can not be used, since we need to do some request parameter check in here.
|
|
|
|
+++
|
|
|
|
|
2021-08-03 17:22:41 +02:00
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
msg_success (str): The message in case of successful removing
|
|
|
|
msg_error (str): The message in case of an error
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
redirect_url = redirect_url if redirect_url is not None else request.META.get("HTTP_REFERER", "home")
|
|
|
|
template = self.template
|
|
|
|
if request.method == "POST":
|
|
|
|
if self.is_valid():
|
2021-08-19 09:06:35 +02:00
|
|
|
# Modal forms send one POST for checking on data validity. This can be used to return possible errors
|
|
|
|
# on the form. A second POST (if no errors occured) is sent afterwards and needs to process the
|
|
|
|
# saving/commiting of the data to the database. is_ajax() performs this check. The first request is
|
|
|
|
# an ajax call, the second is a regular form POST.
|
|
|
|
if not is_ajax(request.META):
|
|
|
|
is_before_state = bool(request.GET.get("before", False))
|
|
|
|
self.save(is_before_state=is_before_state)
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
msg_success
|
|
|
|
)
|
|
|
|
return HttpResponseRedirect(redirect_url)
|
2021-08-03 17:22:41 +02:00
|
|
|
else:
|
2021-08-19 09:06:35 +02:00
|
|
|
context = {
|
|
|
|
"form": self,
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-08-03 17:22:41 +02:00
|
|
|
elif request.method == "GET":
|
|
|
|
context = {
|
|
|
|
"form": self,
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
|
|
|
|
class NewDeadlineModalForm(BaseModalForm):
|
|
|
|
type = forms.ChoiceField(
|
|
|
|
label=_("Deadline Type"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
|
|
|
help_text=_("Select the deadline type"),
|
2021-08-04 10:44:02 +02:00
|
|
|
choices=DeadlineType.choices,
|
|
|
|
widget=forms.Select(
|
|
|
|
attrs={
|
|
|
|
"class": "custom-select"
|
|
|
|
}
|
|
|
|
)
|
2021-08-03 13:13:01 +02:00
|
|
|
)
|
|
|
|
date = forms.DateField(
|
|
|
|
label=_("Date"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
|
|
|
help_text=_("Select date"),
|
|
|
|
widget=forms.DateInput(
|
|
|
|
attrs={
|
|
|
|
"type": "date",
|
|
|
|
"data-provide": "datepicker",
|
|
|
|
},
|
|
|
|
format="%d.%m.%Y"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
comment = forms.CharField(
|
|
|
|
required=False,
|
2021-08-04 11:56:56 +02:00
|
|
|
max_length=200,
|
2021-08-03 13:13:01 +02:00
|
|
|
label=_("Comment"),
|
|
|
|
label_suffix=_(""),
|
2021-08-04 11:56:56 +02:00
|
|
|
help_text=_("Additional comment, maximum {} letters").format(200),
|
2021-08-03 13:13:01 +02:00
|
|
|
widget=forms.Textarea(
|
|
|
|
attrs={
|
|
|
|
"cols": 30,
|
|
|
|
"rows": 5,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("New deadline")
|
|
|
|
self.form_caption = _("Insert data for the new deadline")
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
with transaction.atomic():
|
2021-08-05 12:54:28 +02:00
|
|
|
created_action = UserActionLogEntry.objects.create(
|
2021-08-03 13:13:01 +02:00
|
|
|
user=self.user,
|
2021-08-04 10:44:02 +02:00
|
|
|
action=UserAction.CREATED
|
2021-08-03 13:13:01 +02:00
|
|
|
)
|
|
|
|
deadline = Deadline.objects.create(
|
|
|
|
type=self.cleaned_data["type"],
|
|
|
|
date=self.cleaned_data["date"],
|
|
|
|
comment=self.cleaned_data["comment"],
|
2021-08-05 12:54:28 +02:00
|
|
|
created=created_action,
|
|
|
|
)
|
|
|
|
edited_action = UserActionLogEntry.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
action=UserAction.EDITED,
|
|
|
|
comment=_("Added deadline")
|
2021-08-03 13:13:01 +02:00
|
|
|
)
|
2021-08-19 13:02:31 +02:00
|
|
|
self.instance.modified = edited_action
|
|
|
|
self.instance.save()
|
2021-08-05 12:54:28 +02:00
|
|
|
self.instance.log.add(edited_action)
|
2021-08-03 13:13:01 +02:00
|
|
|
self.instance.deadlines.add(deadline)
|
|
|
|
return deadline
|
|
|
|
|
2021-08-04 10:44:02 +02:00
|
|
|
|
|
|
|
class NewActionModalForm(BaseModalForm):
|
|
|
|
action_type = forms.CharField(
|
|
|
|
label=_("Action Type"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
2021-08-04 11:02:26 +02:00
|
|
|
help_text=_("Select the action type"),
|
2021-08-04 10:44:02 +02:00
|
|
|
)
|
|
|
|
unit = forms.ChoiceField(
|
|
|
|
label=_("Unit"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
|
|
|
help_text=_("Select the unit"),
|
|
|
|
choices=UnitChoices.choices,
|
|
|
|
widget=forms.Select(
|
|
|
|
attrs={
|
|
|
|
"class": "custom-select"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
amount = forms.DecimalField(
|
|
|
|
label=_("Amount"),
|
|
|
|
label_suffix="",
|
|
|
|
required=True,
|
|
|
|
help_text=_("Insert the amount"),
|
|
|
|
decimal_places=2,
|
|
|
|
min_value=0.00,
|
|
|
|
)
|
|
|
|
comment = forms.CharField(
|
|
|
|
required=False,
|
2021-08-04 11:56:56 +02:00
|
|
|
max_length=200,
|
2021-08-04 10:44:02 +02:00
|
|
|
label=_("Comment"),
|
|
|
|
label_suffix=_(""),
|
2021-08-04 11:56:56 +02:00
|
|
|
help_text=_("Additional comment, maximum {} letters").format(200),
|
2021-08-04 10:44:02 +02:00
|
|
|
widget=forms.Textarea(
|
|
|
|
attrs={
|
|
|
|
"cols": 30,
|
|
|
|
"rows": 5,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.form_title = _("New action")
|
|
|
|
self.form_caption = _("Insert data for the new action")
|
2021-08-11 14:17:43 +02:00
|
|
|
self.add_placeholder_for_field("amount", "0,00")
|
2021-08-04 10:44:02 +02:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
user_action = UserActionLogEntry.objects.create(
|
|
|
|
user=self.user,
|
2021-08-05 12:54:28 +02:00
|
|
|
action=UserAction.CREATED,
|
2021-08-04 10:44:02 +02:00
|
|
|
)
|
|
|
|
comp_action = CompensationAction.objects.create(
|
|
|
|
action_type=self.cleaned_data["action_type"],
|
|
|
|
amount=self.cleaned_data["amount"],
|
|
|
|
unit=self.cleaned_data["unit"],
|
2021-08-04 11:56:56 +02:00
|
|
|
comment=self.cleaned_data["comment"],
|
2021-08-04 10:44:02 +02:00
|
|
|
created=user_action,
|
|
|
|
)
|
2021-08-05 12:54:28 +02:00
|
|
|
edited_action = UserActionLogEntry.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
action=UserAction.EDITED,
|
|
|
|
comment=_("Added action"),
|
|
|
|
)
|
2021-08-19 13:02:31 +02:00
|
|
|
self.instance.modified = edited_action
|
|
|
|
self.instance.save()
|
2021-08-05 12:54:28 +02:00
|
|
|
self.instance.log.add(edited_action)
|
2021-08-04 10:44:02 +02:00
|
|
|
self.instance.actions.add(comp_action)
|
|
|
|
return comp_action
|
|
|
|
|