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-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
|
|
|
|
from django.http import HttpRequest, Http404
|
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
|
2021-08-09 15:09:43 +02:00
|
|
|
from compensation.forms import NewStateModalForm, NewActionModalForm, NewDeadlineModalForm
|
2021-08-09 13:16:50 +02:00
|
|
|
from compensation.models import EcoAccount
|
|
|
|
from compensation.tables import EcoAccountTable
|
2021-08-10 10:42:04 +02:00
|
|
|
from intervention.forms import NewWithdrawForm
|
2021-08-09 13:16:50 +02:00
|
|
|
from konova.contexts import BaseContext
|
2021-08-10 17:19:42 +02:00
|
|
|
from konova.decorators import any_group_check, default_group_required, conservation_office_group_required
|
|
|
|
from konova.forms import RemoveModalForm, SimpleGeomForm, NewDocumentForm, RecordForm
|
2021-08-09 14:39:36 +02:00
|
|
|
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
|
|
|
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"
|
|
|
|
user = request.user
|
|
|
|
eco_accounts = EcoAccount.objects.filter(
|
|
|
|
deleted=None,
|
|
|
|
)
|
|
|
|
table = EcoAccountTable(
|
|
|
|
request=request,
|
|
|
|
queryset=eco_accounts
|
|
|
|
)
|
|
|
|
context = {
|
|
|
|
"table": table,
|
|
|
|
}
|
|
|
|
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-08-09 13:16:50 +02:00
|
|
|
# ToDo
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2021-08-09 15:09:43 +02:00
|
|
|
def edit_view(request: HttpRequest, id: str):
|
2021-08-09 13:16:50 +02:00
|
|
|
# ToDo
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@any_group_check
|
2021-08-09 15:09:43 +02:00
|
|
|
def open_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"
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
|
|
|
geom_form = SimpleGeomForm(instance=acc)
|
|
|
|
_user = request.user
|
|
|
|
is_data_shared = acc.is_shared_with(_user)
|
|
|
|
|
|
|
|
# Order states according to surface
|
|
|
|
before_states = acc.before_states.all().order_by("-surface")
|
|
|
|
after_states = acc.after_states.all().order_by("-surface")
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
# Calculate rest of available surface for withdraws
|
2021-08-11 14:17:43 +02:00
|
|
|
available = acc.get_available_rest(as_percentage=True)
|
2021-08-09 14:39:36 +02:00
|
|
|
|
|
|
|
context = {
|
|
|
|
"obj": acc,
|
|
|
|
"geom_form": geom_form,
|
|
|
|
"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-08-11 14:17:43 +02:00
|
|
|
"available": available,
|
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-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-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)
|
|
|
|
form = RemoveModalForm(request.POST or None, instance=acc, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=_("Eco-account removed"),
|
|
|
|
redirect_url=reverse("compensation:acc-index"),
|
|
|
|
)
|
2021-08-09 13:16:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
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:
|
2021-08-09 14:16:54 +02:00
|
|
|
eco_withdraw = acc.withdraws.get(id=withdraw_id)
|
2021-08-09 13:16:50 +02:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404("Unknown withdraw")
|
|
|
|
|
|
|
|
form = RemoveModalForm(request.POST or None, instance=eco_withdraw, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=_("Withdraw removed")
|
|
|
|
)
|
|
|
|
|
2021-08-09 15:09:43 +02:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
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,
|
|
|
|
"log": comp.log.all().order_by("-timestamp"),
|
|
|
|
"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
|
|
|
|
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)
|
|
|
|
form = RecordForm(request.POST or None, instance=acc, user=request.user)
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
form = NewStateModalForm(request.POST or None, instance=acc, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("State added")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
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)
|
|
|
|
form = NewActionModalForm(request.POST or None, instance=acc, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Action added")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
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)
|
|
|
|
form = NewDeadlineModalForm(request.POST or None, instance=acc, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Deadline added")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@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 account's id to which the new document will be related
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
|
|
|
form = NewDocumentForm(request.POST or None, request.FILES or None, instance=acc, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Document added")
|
2021-08-10 10:42:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
def new_withdraw_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a modal form view for creating withdraws
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request ():
|
|
|
|
id ():
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc = get_object_or_404(EcoAccount, id=id)
|
|
|
|
form = NewWithdrawForm(request.POST or None, instance=acc, user=request.user)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Withdraw added")
|
|
|
|
)
|