2021-07-01 13:36:07 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-08-02 10:14:34 +02:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2021-08-03 13:13:01 +02:00
|
|
|
from django.db.models import Sum
|
2021-08-02 10:14:34 +02:00
|
|
|
from django.http import HttpRequest, Http404
|
2021-07-26 10:23:09 +02:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-08-04 10:44:02 +02:00
|
|
|
from compensation.forms import NewPaymentForm, NewStateModalForm, NewDeadlineModalForm, NewActionModalForm
|
|
|
|
from compensation.models import Compensation, EcoAccount, Payment, CompensationState, CompensationAction
|
2021-07-01 13:36:07 +02:00
|
|
|
from compensation.tables import CompensationTable, EcoAccountTable
|
2021-07-26 10:23:09 +02:00
|
|
|
from intervention.models import Intervention
|
2021-07-01 13:36:07 +02:00
|
|
|
from konova.contexts import BaseContext
|
|
|
|
from konova.decorators import *
|
2021-08-03 13:13:01 +02:00
|
|
|
from konova.forms import RemoveModalForm, SimpleGeomForm, NewDocumentForm
|
2021-08-02 10:14:34 +02:00
|
|
|
from konova.utils.message_templates import FORM_INVALID
|
2021-08-03 13:13:01 +02:00
|
|
|
from konova.utils.user_checks import in_group
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-03 09:45:41 +02:00
|
|
|
@any_group_check
|
2021-07-01 13:36:07 +02:00
|
|
|
def index_view(request: HttpRequest):
|
|
|
|
"""
|
|
|
|
Renders the index view for compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A rendered view
|
|
|
|
"""
|
|
|
|
template = "generic_index.html"
|
2021-07-21 14:17:18 +02:00
|
|
|
compensations = Compensation.objects.filter(
|
2021-08-02 15:39:33 +02:00
|
|
|
deleted=None, # only show those which are not deleted individually
|
|
|
|
intervention__deleted=None, # and don't show the ones whose intervention has been deleted
|
2021-07-21 14:17:18 +02:00
|
|
|
)
|
2021-07-01 13:36:07 +02:00
|
|
|
table = CompensationTable(
|
|
|
|
request=request,
|
|
|
|
queryset=compensations
|
|
|
|
)
|
|
|
|
context = {
|
|
|
|
"table": table,
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-07-01 13:36:07 +02:00
|
|
|
def new_view(request: HttpRequest):
|
|
|
|
# ToDo
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-07-01 13:36:07 +02:00
|
|
|
def edit_view(request: HttpRequest, id: str):
|
|
|
|
# ToDo
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-03 09:45:41 +02:00
|
|
|
@any_group_check
|
2021-07-01 13:36:07 +02:00
|
|
|
def open_view(request: HttpRequest, id: str):
|
2021-08-03 13:13:01 +02:00
|
|
|
""" Renders a detail view for a compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
template = "compensation/detail/view.html"
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
geom_form = SimpleGeomForm(instance=comp)
|
|
|
|
_user = request.user
|
|
|
|
is_data_shared = comp.intervention.is_shared_with(_user)
|
|
|
|
|
2021-08-03 17:22:41 +02:00
|
|
|
# Order states according to surface
|
|
|
|
before_states = comp.before_states.all().order_by("-surface")
|
|
|
|
after_states = comp.after_states.all().order_by("-surface")
|
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
# Precalculate logical errors between before- and after-states
|
|
|
|
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
2021-08-03 17:22:41 +02:00
|
|
|
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
|
|
|
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
2021-08-03 13:13:01 +02:00
|
|
|
diff_states = abs(sum_before_states - sum_after_states)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
"comp": comp,
|
|
|
|
"geom_form": geom_form,
|
|
|
|
"has_access": is_data_shared,
|
2021-08-03 17:22:41 +02:00
|
|
|
"before_states": before_states,
|
|
|
|
"after_states": after_states,
|
2021-08-03 13:13:01 +02:00
|
|
|
"sum_before_states": sum_before_states,
|
|
|
|
"sum_after_states": sum_after_states,
|
|
|
|
"diff_states": diff_states,
|
2021-08-04 08:41:21 +02:00
|
|
|
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
|
|
|
"is_zb_member": in_group(_user, ZB_GROUP),
|
|
|
|
"is_ets_member": in_group(_user, ETS_GROUP),
|
2021-08-03 13:13:01 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def remove_view(request: HttpRequest, id: str):
|
2021-08-02 10:53:34 +02:00
|
|
|
""" Renders a modal view for removing the compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
form = RemoveModalForm(request.POST or None, instance=comp, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=_("Compensation removed"),
|
2021-08-03 13:13:01 +02:00
|
|
|
redirect_url="",
|
2021-08-02 10:53:34 +02:00
|
|
|
)
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-03 09:45:41 +02:00
|
|
|
@any_group_check
|
2021-07-01 13:36:07 +02:00
|
|
|
def account_index_view(request: HttpRequest):
|
|
|
|
"""
|
|
|
|
Renders the index view for eco accounts
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A rendered view
|
|
|
|
"""
|
|
|
|
template = "generic_index.html"
|
|
|
|
user = request.user
|
|
|
|
eco_accounts = EcoAccount.objects.filter(
|
2021-08-02 11:52:20 +02:00
|
|
|
deleted=None,
|
2021-07-01 13:36:07 +02:00
|
|
|
)
|
|
|
|
table = EcoAccountTable(
|
|
|
|
request=request,
|
|
|
|
queryset=eco_accounts
|
|
|
|
)
|
|
|
|
context = {
|
|
|
|
"table": table,
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-07-01 13:36:07 +02:00
|
|
|
def account_new_view(request: HttpRequest):
|
|
|
|
# ToDo
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-07-01 13:36:07 +02:00
|
|
|
def account_edit_view(request: HttpRequest, id: str):
|
|
|
|
# ToDo
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-03 09:45:41 +02:00
|
|
|
@any_group_check
|
2021-07-01 13:36:07 +02:00
|
|
|
def account_open_view(request: HttpRequest, id: str):
|
|
|
|
# ToDo
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def account_remove_view(request: HttpRequest, id: str):
|
|
|
|
# ToDo
|
|
|
|
pass
|
2021-07-26 10:23:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-07-26 10:23:09 +02:00
|
|
|
def new_payment_view(request: HttpRequest, intervention_id: str):
|
|
|
|
""" Renders a modal view for adding new payments
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
intervention_id (str): The intervention's id for which a new payment shall be added
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=intervention_id)
|
2021-07-26 11:29:05 +02:00
|
|
|
form = NewPaymentForm(request.POST or None, instance=intervention, user=request.user)
|
2021-07-26 15:16:16 +02:00
|
|
|
template = form.template
|
2021-07-26 10:23:09 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
if form.is_valid():
|
|
|
|
payment = form.save()
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("Payment added")
|
|
|
|
)
|
|
|
|
return redirect(request.META.get("HTTP_REFERER", "home"))
|
|
|
|
else:
|
|
|
|
messages.info(
|
|
|
|
request,
|
2021-08-02 10:14:34 +02:00
|
|
|
FORM_INVALID
|
2021-07-26 10:23:09 +02:00
|
|
|
)
|
|
|
|
return redirect(request.META.get("HTTP_REFERER", "home"))
|
|
|
|
elif request.method == "GET":
|
|
|
|
context = {
|
|
|
|
"form": form,
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2021-07-26 11:29:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-07-26 11:29:05 +02:00
|
|
|
def payment_remove_view(request: HttpRequest, id: str):
|
2021-08-02 10:14:34 +02:00
|
|
|
""" Renders a modal view for removing payments
|
2021-07-26 11:29:05 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The payment's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
payment = get_object_or_404(Payment, id=id)
|
|
|
|
form = RemoveModalForm(request.POST or None, instance=payment, user=request.user)
|
2021-08-02 10:53:34 +02:00
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=_("Payment removed"),
|
|
|
|
)
|
2021-08-02 10:14:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-08-02 10:14:34 +02:00
|
|
|
def withdraw_remove_view(request: HttpRequest, id: str, withdraw_id: str):
|
|
|
|
""" Renders a modal view for removing withdraws
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The eco account's id
|
|
|
|
withdraw_id (str): The withdraw's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
|
|
|
try:
|
|
|
|
eco_withdraw = acc.eco_withdraws.get(id=withdraw_id)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404("Unknown withdraw")
|
|
|
|
|
|
|
|
form = RemoveModalForm(request.POST or None, instance=eco_withdraw, user=request.user)
|
2021-08-02 10:53:34 +02:00
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=_("Withdraw removed")
|
|
|
|
)
|
2021-08-03 13:13:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def new_document_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for uploading new documents
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id to which the new document will be related
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
form = NewDocumentForm(request.POST or None, request.FILES or None, instance=comp, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Document added")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def state_new_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for adding new states for a compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id to which the new state will be related
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
form = NewStateModalForm(request.POST or None, instance=comp, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("State added")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-04 10:44:02 +02:00
|
|
|
@login_required
|
|
|
|
def action_new_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for adding new actions for a compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id to which the new state will be related
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
form = NewActionModalForm(request.POST or None, instance=comp, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Action added")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
@login_required
|
|
|
|
def deadline_new_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for adding new states for a compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id to which the new state will be related
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
form = NewDeadlineModalForm(request.POST or None, instance=comp, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Deadline added")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def state_remove_view(request: HttpRequest, id: str):
|
2021-08-04 10:44:02 +02:00
|
|
|
""" Renders a form for removing a compensation state
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The state's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-08-03 13:13:01 +02:00
|
|
|
state = get_object_or_404(CompensationState, id=id)
|
|
|
|
form = RemoveModalForm(request.POST or None, instance=state, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("State removed")
|
|
|
|
)
|
2021-08-04 10:44:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def action_remove_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for removing a compensation action
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The action's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
action = get_object_or_404(CompensationAction, id=id)
|
|
|
|
form = RemoveModalForm(request.POST or None, instance=action, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Action removed")
|
|
|
|
)
|