2021-08-09 13:16:50 +02:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 09.08.21
|
|
|
|
|
|
|
|
"""
|
2021-10-05 16:35:24 +02:00
|
|
|
from django.contrib import messages
|
2021-08-09 14:39:36 +02:00
|
|
|
from django.db.models import Sum
|
2021-08-09 15:09:43 +02:00
|
|
|
from django.urls import reverse
|
2021-08-09 13:16:50 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2021-10-05 16:35:24 +02:00
|
|
|
from django.http import HttpRequest, Http404, JsonResponse
|
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
2021-08-09 13:16:50 +02:00
|
|
|
|
2021-10-06 13:10:10 +02:00
|
|
|
from compensation.forms.forms import NewEcoAccountForm, EditEcoAccountForm
|
2021-11-15 17:09:17 +01:00
|
|
|
from compensation.forms.modalForms import NewStateModalForm, NewActionModalForm, NewDeadlineModalForm, \
|
2022-02-10 10:21:18 +01:00
|
|
|
NewEcoAccountDocumentModalForm, RemoveCompensationActionModalForm, RemoveCompensationStateModalForm
|
2021-11-09 13:06:22 +01:00
|
|
|
from compensation.models import EcoAccount, EcoAccountDocument, CompensationState, CompensationAction
|
2021-08-09 13:16:50 +02:00
|
|
|
from compensation.tables import EcoAccountTable
|
2022-02-09 14:49:56 +01:00
|
|
|
from intervention.forms.modalForms import NewDeductionModalForm, ShareModalForm, RemoveEcoAccountDeductionModalForm, \
|
|
|
|
EditEcoAccountDeductionModalForm
|
2021-08-09 13:16:50 +02:00
|
|
|
from konova.contexts import BaseContext
|
2021-11-09 13:06:22 +01:00
|
|
|
from konova.decorators import any_group_check, default_group_required, conservation_office_group_required, \
|
|
|
|
shared_access_required
|
2022-02-10 10:21:18 +01:00
|
|
|
from konova.forms import RemoveModalForm, SimpleGeomForm, NewDocumentModalForm, RecordModalForm, RemoveDeadlineModalForm
|
2022-02-02 11:26:02 +01:00
|
|
|
from konova.models import Deadline
|
2021-08-09 14:39:36 +02:00
|
|
|
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
2022-01-20 12:23:19 +01:00
|
|
|
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
2021-09-01 16:24:49 +02:00
|
|
|
from konova.utils.documents import get_document, remove_document
|
2021-10-13 17:35:11 +02:00
|
|
|
from konova.utils.generators import generate_qr_code
|
2021-11-10 09:11:24 +01:00
|
|
|
from konova.utils.message_templates import IDENTIFIER_REPLACED, FORM_INVALID, DATA_UNSHARED, DATA_UNSHARED_EXPLANATION, \
|
2022-02-04 16:56:08 +01:00
|
|
|
CANCEL_ACC_RECORDED_OR_DEDUCTED, DEDUCTION_REMOVED, DEDUCTION_ADDED, DOCUMENT_ADDED, COMPENSATION_STATE_REMOVED, \
|
2022-02-09 14:49:56 +01:00
|
|
|
COMPENSATION_STATE_ADDED, COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_ADDED, DEADLINE_ADDED, DEADLINE_REMOVED, \
|
|
|
|
DEDUCTION_EDITED
|
2021-08-09 14:39:36 +02:00
|
|
|
from konova.utils.user_checks import in_group
|
2021-08-09 13:16:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@any_group_check
|
2021-08-09 15:09:43 +02:00
|
|
|
def index_view(request: HttpRequest):
|
2021-08-09 13:16:50 +02:00
|
|
|
"""
|
|
|
|
Renders the index view for eco accounts
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A rendered view
|
|
|
|
"""
|
|
|
|
template = "generic_index.html"
|
|
|
|
eco_accounts = EcoAccount.objects.filter(
|
|
|
|
deleted=None,
|
|
|
|
)
|
|
|
|
table = EcoAccountTable(
|
|
|
|
request=request,
|
|
|
|
queryset=eco_accounts
|
|
|
|
)
|
|
|
|
context = {
|
|
|
|
"table": table,
|
2022-01-20 12:23:19 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("Eco-account - Overview"),
|
2021-08-09 13:16:50 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2021-08-09 15:09:43 +02:00
|
|
|
def new_view(request: HttpRequest):
|
2021-10-05 16:35:24 +02:00
|
|
|
"""
|
|
|
|
Renders a view for a new eco account creation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-10-06 16:00:17 +02:00
|
|
|
template = "compensation/form/view.html"
|
2021-10-05 16:35:24 +02:00
|
|
|
data_form = NewEcoAccountForm(request.POST or None)
|
|
|
|
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
|
|
|
if request.method == "POST":
|
|
|
|
if data_form.is_valid() and geom_form.is_valid():
|
|
|
|
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
|
|
|
acc = data_form.save(request.user, geom_form)
|
|
|
|
if generated_identifier != acc.identifier:
|
|
|
|
messages.info(
|
|
|
|
request,
|
|
|
|
IDENTIFIER_REPLACED.format(
|
|
|
|
generated_identifier,
|
|
|
|
acc.identifier
|
|
|
|
)
|
|
|
|
)
|
|
|
|
messages.success(request, _("Eco-Account {} added").format(acc.identifier))
|
2022-02-02 11:26:02 +01:00
|
|
|
return redirect("compensation:acc:detail", id=acc.id)
|
2021-10-05 16:35:24 +02:00
|
|
|
else:
|
2021-10-20 13:23:35 +02:00
|
|
|
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
2021-10-05 16:35:24 +02:00
|
|
|
else:
|
|
|
|
# For clarification: nothing in this case
|
|
|
|
pass
|
|
|
|
context = {
|
|
|
|
"form": data_form,
|
|
|
|
"geom_form": geom_form,
|
2022-01-20 12:23:19 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("New Eco-Account"),
|
2021-10-05 16:35:24 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-11-09 13:06:22 +01:00
|
|
|
@default_group_required
|
2021-10-05 16:35:24 +02:00
|
|
|
def new_id_view(request: HttpRequest):
|
|
|
|
""" JSON endpoint
|
|
|
|
|
|
|
|
Provides fetching of free identifiers for e.g. AJAX calls
|
|
|
|
|
|
|
|
"""
|
|
|
|
tmp = EcoAccount()
|
|
|
|
identifier = tmp.generate_new_identifier()
|
|
|
|
while EcoAccount.objects.filter(identifier=identifier).exists():
|
|
|
|
identifier = tmp.generate_new_identifier()
|
|
|
|
return JsonResponse(
|
|
|
|
data={
|
2022-01-27 11:37:38 +01:00
|
|
|
"gen_data": identifier
|
2021-10-05 16:35:24 +02:00
|
|
|
}
|
|
|
|
)
|
2021-08-09 13:16:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-09 15:09:43 +02:00
|
|
|
def edit_view(request: HttpRequest, id: str):
|
2021-10-06 13:10:10 +02:00
|
|
|
"""
|
|
|
|
Renders a view for editing compensations
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-10-06 16:00:17 +02:00
|
|
|
template = "compensation/form/view.html"
|
2021-10-06 13:10:10 +02:00
|
|
|
# Get object from db
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
|
|
|
# Create forms, initialize with values from db/from POST request
|
|
|
|
data_form = EditEcoAccountForm(request.POST or None, instance=acc)
|
|
|
|
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=acc)
|
|
|
|
if request.method == "POST":
|
|
|
|
if data_form.is_valid() and geom_form.is_valid():
|
|
|
|
# The data form takes the geom form for processing, as well as the performing user
|
|
|
|
acc = data_form.save(request.user, geom_form)
|
|
|
|
messages.success(request, _("Eco-Account {} edited").format(acc.identifier))
|
2022-02-02 11:26:02 +01:00
|
|
|
return redirect("compensation:acc:detail", id=acc.id)
|
2021-10-06 13:10:10 +02:00
|
|
|
else:
|
2021-10-20 13:23:35 +02:00
|
|
|
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
2021-10-06 13:10:10 +02:00
|
|
|
else:
|
|
|
|
# For clarification: nothing in this case
|
|
|
|
pass
|
|
|
|
context = {
|
|
|
|
"form": data_form,
|
|
|
|
"geom_form": geom_form,
|
2022-01-20 12:23:19 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("Edit {}").format(acc.identifier),
|
2021-10-06 13:10:10 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-08-09 13:16:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@any_group_check
|
2021-10-13 09:26:46 +02:00
|
|
|
def detail_view(request: HttpRequest, id: str):
|
2021-08-09 14:39:36 +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/eco_account/view.html"
|
2021-10-14 14:12:33 +02:00
|
|
|
acc = get_object_or_404(
|
|
|
|
EcoAccount.objects.prefetch_related(
|
|
|
|
"deadlines",
|
|
|
|
).select_related(
|
|
|
|
'geometry',
|
|
|
|
'responsible',
|
|
|
|
),
|
|
|
|
id=id
|
|
|
|
)
|
2021-08-09 14:39:36 +02:00
|
|
|
geom_form = SimpleGeomForm(instance=acc)
|
2022-01-05 14:13:26 +01:00
|
|
|
parcels = acc.get_underlying_parcels()
|
2021-08-09 14:39:36 +02:00
|
|
|
_user = request.user
|
|
|
|
is_data_shared = acc.is_shared_with(_user)
|
|
|
|
|
|
|
|
# Order states according to surface
|
2021-10-14 14:12:33 +02:00
|
|
|
before_states = acc.before_states.order_by("-surface")
|
|
|
|
after_states = acc.after_states.order_by("-surface")
|
2021-08-09 14:39:36 +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
|
|
|
|
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
|
|
|
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
|
|
|
diff_states = abs(sum_before_states - sum_after_states)
|
2021-08-30 11:29:15 +02:00
|
|
|
# Calculate rest of available surface for deductions
|
2021-10-05 16:35:24 +02:00
|
|
|
available_total, available_relative = acc.get_available_rest()
|
2021-08-09 14:39:36 +02:00
|
|
|
|
2021-10-14 14:12:33 +02:00
|
|
|
# Prefetch related data to decrease the amount of db connections
|
2021-08-30 11:34:35 +02:00
|
|
|
deductions = acc.deductions.filter(
|
2021-08-26 15:45:24 +02:00
|
|
|
intervention__deleted=None,
|
|
|
|
)
|
2021-10-14 14:12:33 +02:00
|
|
|
actions = acc.actions.all()
|
2021-08-26 15:45:24 +02:00
|
|
|
|
2021-12-15 15:10:35 +01:00
|
|
|
request = acc.set_status_messages(request)
|
2021-10-25 08:51:06 +02:00
|
|
|
|
2021-08-09 14:39:36 +02:00
|
|
|
context = {
|
|
|
|
"obj": acc,
|
|
|
|
"geom_form": geom_form,
|
2022-01-05 14:13:26 +01:00
|
|
|
"parcels": parcels,
|
2021-08-09 14:39:36 +02:00
|
|
|
"has_access": is_data_shared,
|
|
|
|
"before_states": before_states,
|
|
|
|
"after_states": after_states,
|
|
|
|
"sum_before_states": sum_before_states,
|
|
|
|
"sum_after_states": sum_after_states,
|
|
|
|
"diff_states": diff_states,
|
2021-10-05 16:35:24 +02:00
|
|
|
"available": available_relative,
|
|
|
|
"available_total": available_total,
|
2021-08-09 14:39:36 +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-10 13:57:03 +02:00
|
|
|
"LANIS_LINK": acc.get_LANIS_link(),
|
2021-08-30 11:29:15 +02:00
|
|
|
"deductions": deductions,
|
2021-10-14 14:12:33 +02:00
|
|
|
"actions": actions,
|
2022-01-20 12:23:19 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: f"{acc.identifier} - {acc.title}",
|
2021-08-09 14:39:36 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-08-09 13:16:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-09 15:09:43 +02:00
|
|
|
def remove_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a modal view for removing the eco account
|
2021-08-09 13:16:50 +02:00
|
|
|
|
2021-08-09 15:09:43 +02:00
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The account's id
|
2021-08-09 13:16:50 +02:00
|
|
|
|
2021-08-09 15:09:43 +02:00
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2021-11-10 09:11:24 +01:00
|
|
|
|
|
|
|
# If the eco account has already been recorded OR there are already deductions, it can not be deleted by a regular
|
|
|
|
# default group user
|
|
|
|
if acc.recorded is not None or acc.deductions.exists():
|
|
|
|
user = request.user
|
|
|
|
if not in_group(user, ETS_GROUP):
|
|
|
|
messages.info(request, CANCEL_ACC_RECORDED_OR_DEDUCTED)
|
2022-02-02 11:26:02 +01:00
|
|
|
return redirect("compensation:acc:detail", id=id)
|
2021-11-10 09:11:24 +01:00
|
|
|
|
2021-11-17 14:33:05 +01:00
|
|
|
form = RemoveModalForm(request.POST or None, instance=acc, request=request)
|
2021-08-09 15:09:43 +02:00
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=_("Eco-account removed"),
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:index"),
|
2021-08-09 15:09:43 +02:00
|
|
|
)
|
2021-08-09 13:16:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-30 11:29:15 +02:00
|
|
|
def deduction_remove_view(request: HttpRequest, id: str, deduction_id: str):
|
|
|
|
""" Renders a modal view for removing deductions
|
2021-08-09 13:16:50 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The eco account's id
|
2021-08-30 11:29:15 +02:00
|
|
|
deduction_id (str): The deduction's id
|
2021-08-09 13:16:50 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
|
|
|
try:
|
2021-08-30 11:34:35 +02:00
|
|
|
eco_deduction = acc.deductions.get(id=deduction_id)
|
2021-08-09 13:16:50 +02:00
|
|
|
except ObjectDoesNotExist:
|
2021-08-30 11:29:15 +02:00
|
|
|
raise Http404("Unknown deduction")
|
2021-08-09 13:16:50 +02:00
|
|
|
|
2022-02-08 13:31:40 +01:00
|
|
|
form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=acc, deduction=eco_deduction, request=request)
|
2021-08-09 13:16:50 +02:00
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
2022-02-02 15:16:25 +01:00
|
|
|
msg_success=DEDUCTION_REMOVED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
2021-08-09 13:16:50 +02:00
|
|
|
)
|
|
|
|
|
2021-08-09 15:09:43 +02:00
|
|
|
|
2022-02-09 14:49:56 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
|
|
|
def deduction_edit_view(request: HttpRequest, id: str, deduction_id: str):
|
|
|
|
""" Renders a modal view for editing deductions
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The eco account's id
|
|
|
|
deduction_id (str): The deduction's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
|
|
|
try:
|
|
|
|
eco_deduction = acc.deductions.get(id=deduction_id)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404("Unknown deduction")
|
|
|
|
|
|
|
|
form = EditEcoAccountDeductionModalForm(request.POST or None, instance=acc, deduction=eco_deduction, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=DEDUCTION_EDITED,
|
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-09 15:09:43 +02:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-09 15:09:43 +02:00
|
|
|
def log_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a log view using modal
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The eco acount's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(EcoAccount, id=id)
|
|
|
|
template = "modal/modal_generic.html"
|
|
|
|
body_template = "log.html"
|
|
|
|
|
|
|
|
context = {
|
|
|
|
"modal_body_template": body_template,
|
2021-08-19 13:02:31 +02:00
|
|
|
"log": comp.log.all(),
|
2021-08-09 15:09:43 +02:00
|
|
|
"modal_title": _("Log"),
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
2021-08-10 17:19:42 +02:00
|
|
|
@login_required
|
|
|
|
@conservation_office_group_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-10 17:19:42 +02:00
|
|
|
def record_view(request: HttpRequest, id:str):
|
|
|
|
""" Renders a modal form for recording an eco account
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The account's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = RecordModalForm(request.POST or None, instance=acc, request=request)
|
2021-08-10 17:19:42 +02:00
|
|
|
msg_succ = _("{} unrecorded") if acc.recorded else _("{} recorded")
|
|
|
|
msg_succ = msg_succ.format(acc.identifier)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_succ
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-09 15:09:43 +02:00
|
|
|
@login_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-09 15:09:43 +02:00
|
|
|
def state_new_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for adding new states for an eco account
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The account's id to which the new state will be related
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewStateModalForm(request.POST or None, instance=acc, request=request)
|
2021-08-09 15:09:43 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 16:56:08 +01:00
|
|
|
msg_success=COMPENSATION_STATE_ADDED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
2021-08-09 15:09:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-09 15:09:43 +02:00
|
|
|
def action_new_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for adding new actions for an eco account
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The account's id to which the new state will be related
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewActionModalForm(request.POST or None, instance=acc, request=request)
|
2021-08-09 15:09:43 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 16:56:08 +01:00
|
|
|
msg_success=COMPENSATION_ACTION_ADDED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
2021-08-09 15:09:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-09 13:06:22 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
|
|
|
def state_remove_view(request: HttpRequest, id: str, state_id: str):
|
|
|
|
""" Renders a form for removing a compensation state
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
state_id (str): The state's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-08 13:16:20 +01:00
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2021-11-09 13:06:22 +01:00
|
|
|
state = get_object_or_404(CompensationState, id=state_id)
|
2022-02-08 13:31:40 +01:00
|
|
|
form = RemoveCompensationStateModalForm(request.POST or None, instance=acc, state=state, request=request)
|
2021-11-09 13:06:22 +01:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 16:56:08 +01:00
|
|
|
msg_success=COMPENSATION_STATE_REMOVED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
2021-11-09 13:06:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
|
|
|
def action_remove_view(request: HttpRequest, id: str, action_id: str):
|
|
|
|
""" Renders a form for removing a compensation action
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
id (str): The action's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-08 13:16:20 +01:00
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2021-11-09 13:06:22 +01:00
|
|
|
action = get_object_or_404(CompensationAction, id=action_id)
|
2022-02-08 13:31:40 +01:00
|
|
|
form = RemoveCompensationActionModalForm(request.POST or None, instance=acc, action=action, request=request)
|
2021-11-09 13:06:22 +01:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 16:56:08 +01:00
|
|
|
msg_success=COMPENSATION_ACTION_REMOVED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
|
|
|
def deadline_remove_view(request: HttpRequest, id: str, deadline_id: str):
|
|
|
|
""" Renders a form for removing deadlines from a compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
deadline_id (str): The deadline's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-07 09:56:37 +01:00
|
|
|
comp = get_object_or_404(EcoAccount, id=id)
|
2022-02-02 11:26:02 +01:00
|
|
|
deadline = get_object_or_404(Deadline, id=deadline_id)
|
2022-02-08 13:31:40 +01:00
|
|
|
form = RemoveDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
2022-02-02 11:26:02 +01:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-07 09:56:37 +01:00
|
|
|
msg_success=DEADLINE_REMOVED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
2021-11-09 13:06:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-09 15:09:43 +02:00
|
|
|
@login_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-09 15:09:43 +02:00
|
|
|
def deadline_new_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for adding new states for an eco account
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The account's id to which the new state will be related
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewDeadlineModalForm(request.POST or None, instance=acc, request=request)
|
2021-08-09 15:09:43 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-07 09:56:37 +01:00
|
|
|
msg_success=DEADLINE_ADDED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
2021-08-09 15:09:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-09 15:09:43 +02:00
|
|
|
def new_document_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a form for uploading new documents
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The account's id to which the new document will be related
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2022-02-10 10:21:18 +01:00
|
|
|
form = NewEcoAccountDocumentModalForm(request.POST or None, request.FILES or None, instance=acc, request=request)
|
2021-08-09 15:09:43 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 09:18:46 +01:00
|
|
|
msg_success=DOCUMENT_ADDED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data",
|
2021-08-10 10:42:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
@login_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@default_group_required
|
2021-09-01 16:24:49 +02:00
|
|
|
def get_document_view(request: HttpRequest, doc_id: str):
|
|
|
|
""" Returns the document as downloadable file
|
|
|
|
|
|
|
|
Wraps the generic document fetcher function from konova.utils.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
doc_id (str): The document id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
doc = get_object_or_404(EcoAccountDocument, id=doc_id)
|
2021-10-25 08:51:06 +02:00
|
|
|
user = request.user
|
|
|
|
instance = doc.instance
|
|
|
|
# File download only possible if related instance is shared with user
|
|
|
|
if not instance.users.filter(id=user.id):
|
|
|
|
messages.info(
|
|
|
|
request,
|
|
|
|
DATA_UNSHARED
|
|
|
|
)
|
2022-02-02 11:26:02 +01:00
|
|
|
return redirect("compensation:acc:detail", id=instance.id)
|
2021-09-01 16:24:49 +02:00
|
|
|
return get_document(doc)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-09-01 16:24:49 +02:00
|
|
|
def remove_document_view(request: HttpRequest, doc_id: str):
|
|
|
|
""" Removes the document from the database and file system
|
|
|
|
|
|
|
|
Wraps the generic functionality from konova.utils.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
doc_id (str): The document id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
doc = get_object_or_404(EcoAccountDocument, id=doc_id)
|
|
|
|
return remove_document(
|
|
|
|
request,
|
|
|
|
doc
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-10 10:42:04 +02:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-08-30 11:29:15 +02:00
|
|
|
def new_deduction_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a modal form view for creating deductions
|
2021-08-10 10:42:04 +02:00
|
|
|
|
|
|
|
Args:
|
2021-08-30 11:29:15 +02:00
|
|
|
request (HttpRequest): THe incoming request
|
|
|
|
id (str): The eco account's id
|
2021-08-10 10:42:04 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewDeductionModalForm(request.POST or None, instance=acc, request=request)
|
2021-08-10 10:42:04 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-02 15:16:25 +01:00
|
|
|
msg_success=DEDUCTION_ADDED,
|
2022-02-02 11:26:02 +01:00
|
|
|
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
2021-08-10 10:42:04 +02:00
|
|
|
)
|
2021-10-13 17:35:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
def report_view(request:HttpRequest, id: str):
|
|
|
|
""" Renders the public report view
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The id of the intervention
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Reuse the compensation report template since EcoAccounts are structurally identical
|
2021-10-14 14:12:33 +02:00
|
|
|
template = "compensation/report/eco_account/report.html"
|
2021-10-13 17:35:11 +02:00
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
|
|
|
|
2022-01-20 12:23:19 +01:00
|
|
|
tab_title = _("Report {}").format(acc.identifier)
|
2021-10-13 17:35:11 +02:00
|
|
|
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
2022-01-21 09:22:43 +01:00
|
|
|
if not acc.is_ready_for_publish():
|
2021-10-13 17:35:11 +02:00
|
|
|
template = "report/unavailable.html"
|
2022-01-20 12:23:19 +01:00
|
|
|
context = {
|
|
|
|
TAB_TITLE_IDENTIFIER: tab_title,
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-10-13 17:35:11 +02:00
|
|
|
|
2021-10-14 14:12:33 +02:00
|
|
|
# Prepare data for map viewer
|
|
|
|
geom_form = SimpleGeomForm(
|
|
|
|
instance=acc
|
|
|
|
)
|
2022-01-05 14:41:32 +01:00
|
|
|
parcels = acc.get_underlying_parcels()
|
2021-10-13 17:35:11 +02:00
|
|
|
qrcode_img = generate_qr_code(
|
2021-10-14 14:12:33 +02:00
|
|
|
request.build_absolute_uri(reverse("ema:report", args=(id,))),
|
2021-10-13 17:35:11 +02:00
|
|
|
10
|
|
|
|
)
|
|
|
|
qrcode_img_lanis = generate_qr_code(
|
|
|
|
acc.get_LANIS_link(),
|
|
|
|
7
|
|
|
|
)
|
2021-10-14 14:12:33 +02:00
|
|
|
# Order states by surface
|
|
|
|
before_states = acc.before_states.all().order_by("-surface").select_related("biotope_type__parent")
|
|
|
|
after_states = acc.after_states.all().order_by("-surface").select_related("biotope_type__parent")
|
|
|
|
actions = acc.actions.all().select_related("action_type__parent")
|
|
|
|
|
|
|
|
# Reduce amount of db fetched data to the bare minimum we need in the template (deduction's intervention id and identifier)
|
|
|
|
deductions = acc.deductions.all()\
|
|
|
|
.distinct("intervention")\
|
|
|
|
.select_related("intervention")\
|
2021-11-15 11:05:39 +01:00
|
|
|
.values_list("intervention__id", "intervention__identifier", "intervention__title", named=True)
|
2021-10-14 14:12:33 +02:00
|
|
|
|
2021-10-13 17:35:11 +02:00
|
|
|
context = {
|
|
|
|
"obj": acc,
|
|
|
|
"qrcode": qrcode_img,
|
|
|
|
"qrcode_lanis": qrcode_img_lanis,
|
2021-10-14 14:12:33 +02:00
|
|
|
"has_access": False, # disables action buttons during rendering
|
|
|
|
"before_states": before_states,
|
|
|
|
"after_states": after_states,
|
|
|
|
"geom_form": geom_form,
|
2022-01-05 14:41:32 +01:00
|
|
|
"parcels": parcels,
|
2021-10-14 14:12:33 +02:00
|
|
|
"actions": actions,
|
|
|
|
"deductions": deductions,
|
2022-01-20 12:23:19 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: tab_title,
|
2021-10-13 17:35:11 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-10-26 15:09:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def share_view(request: HttpRequest, id: str, token: str):
|
|
|
|
""" Performs sharing of an eco account
|
|
|
|
|
|
|
|
If token given in url is not valid, the user will be redirected to the dashboard
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): EcoAccount's id
|
|
|
|
token (str): Access token for EcoAccount
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = request.user
|
|
|
|
obj = get_object_or_404(EcoAccount, id=id)
|
|
|
|
# Check tokens
|
|
|
|
if obj.access_token == token:
|
|
|
|
# Send different messages in case user has already been added to list of sharing users
|
|
|
|
if obj.is_shared_with(user):
|
|
|
|
messages.info(
|
|
|
|
request,
|
|
|
|
_("{} has already been shared with you").format(obj.identifier)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("{} has been shared with you").format(obj.identifier)
|
|
|
|
)
|
2021-11-11 15:09:03 +01:00
|
|
|
obj.share_with(user)
|
2022-02-02 11:26:02 +01:00
|
|
|
return redirect("compensation:acc:detail", id=id)
|
2021-10-26 15:09:30 +02:00
|
|
|
else:
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
_("Share link invalid"),
|
|
|
|
extra_tags="danger",
|
|
|
|
)
|
|
|
|
return redirect("home")
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2021-11-10 09:11:24 +01:00
|
|
|
@shared_access_required(EcoAccount, "id")
|
2021-10-26 15:09:30 +02:00
|
|
|
def create_share_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders sharing form for an eco account
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): EcoAccount's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
obj = get_object_or_404(EcoAccount, id=id)
|
2021-11-16 12:54:28 +01:00
|
|
|
form = ShareModalForm(request.POST or None, instance=obj, request=request)
|
2021-10-26 15:09:30 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Share settings updated")
|
|
|
|
)
|