2021-07-01 13:36:07 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-08-03 13:13:01 +02:00
|
|
|
from django.db.models import Sum
|
2021-10-04 09:55:59 +02:00
|
|
|
from django.http import HttpRequest, JsonResponse
|
2021-10-04 16:18:59 +02:00
|
|
|
from django.shortcuts import render
|
2021-07-26 10:23:09 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-10-04 16:18:59 +02:00
|
|
|
from compensation.forms.forms import NewCompensationForm, EditCompensationForm
|
2021-11-15 17:09:17 +01:00
|
|
|
from compensation.forms.modalForms import NewStateModalForm, NewDeadlineModalForm, NewActionModalForm, \
|
2022-02-10 11:02:30 +01:00
|
|
|
NewCompensationDocumentModalForm, RemoveCompensationActionModalForm, RemoveCompensationStateModalForm, \
|
2022-02-10 12:42:41 +01:00
|
|
|
EditCompensationStateModalForm, EditCompensationActionModalForm, EditDeadlineModalForm
|
2021-09-01 16:24:49 +02:00
|
|
|
from compensation.models import Compensation, CompensationState, CompensationAction, CompensationDocument
|
2021-08-09 13:16:50 +02:00
|
|
|
from compensation.tables import CompensationTable
|
2021-10-04 13:23:14 +02:00
|
|
|
from intervention.models import Intervention
|
2021-07-01 13:36:07 +02:00
|
|
|
from konova.contexts import BaseContext
|
|
|
|
from konova.decorators import *
|
2022-02-10 10:44:44 +01:00
|
|
|
from konova.forms import RemoveModalForm, SimpleGeomForm, RemoveDeadlineModalForm, EditDocumentModalForm
|
2022-02-02 10:17:59 +01:00
|
|
|
from konova.models import Deadline
|
2022-01-20 12:19:08 +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-17 12:09:49 +01:00
|
|
|
from konova.utils.message_templates import FORM_INVALID, IDENTIFIER_REPLACED, DATA_UNSHARED_EXPLANATION, \
|
2022-02-04 16:56:08 +01:00
|
|
|
CHECKED_RECORDED_RESET, COMPENSATION_ADDED_TEMPLATE, COMPENSATION_REMOVED_TEMPLATE, DOCUMENT_ADDED, \
|
2022-02-07 09:56:37 +01:00
|
|
|
COMPENSATION_STATE_REMOVED, COMPENSATION_STATE_ADDED, COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_ADDED, \
|
2022-02-10 12:42:41 +01:00
|
|
|
DEADLINE_ADDED, DEADLINE_REMOVED, DOCUMENT_EDITED, COMPENSATION_STATE_EDITED, COMPENSATION_ACTION_EDITED, \
|
|
|
|
DEADLINE_EDITED
|
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,
|
2022-01-20 12:19:08 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("Compensations - Overview"),
|
2021-07-01 13:36:07 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-10-04 13:23:14 +02:00
|
|
|
@shared_access_required(Intervention, "intervention_id")
|
|
|
|
def new_view(request: HttpRequest, intervention_id: str = None):
|
2021-10-04 09:55:59 +02:00
|
|
|
"""
|
|
|
|
Renders a view for a new compensation creation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-10-06 16:00:17 +02:00
|
|
|
template = "compensation/form/view.html"
|
2021-10-04 13:23:14 +02:00
|
|
|
data_form = NewCompensationForm(request.POST or None, intervention_id=intervention_id)
|
2021-10-04 09:55:59 +02:00
|
|
|
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)
|
|
|
|
comp = data_form.save(request.user, geom_form)
|
|
|
|
if generated_identifier != comp.identifier:
|
|
|
|
messages.info(
|
|
|
|
request,
|
|
|
|
IDENTIFIER_REPLACED.format(
|
|
|
|
generated_identifier,
|
|
|
|
comp.identifier
|
|
|
|
)
|
|
|
|
)
|
2022-02-03 15:29:22 +01:00
|
|
|
messages.success(request, COMPENSATION_ADDED_TEMPLATE.format(comp.identifier))
|
2021-10-13 09:26:46 +02:00
|
|
|
return redirect("compensation:detail", id=comp.id)
|
2021-10-04 09:55:59 +02:00
|
|
|
else:
|
2021-10-20 13:23:35 +02:00
|
|
|
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
2021-10-04 09:55:59 +02:00
|
|
|
else:
|
|
|
|
# For clarification: nothing in this case
|
|
|
|
pass
|
|
|
|
context = {
|
|
|
|
"form": data_form,
|
|
|
|
"geom_form": geom_form,
|
2022-01-20 12:19:08 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("New compensation"),
|
2021-10-04 09:55:59 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
2021-10-04 09:55:59 +02:00
|
|
|
def new_id_view(request: HttpRequest):
|
|
|
|
""" JSON endpoint
|
|
|
|
|
|
|
|
Provides fetching of free identifiers for e.g. AJAX calls
|
|
|
|
|
|
|
|
"""
|
|
|
|
tmp = Compensation()
|
2021-10-05 16:35:24 +02:00
|
|
|
identifier = tmp.generate_new_identifier()
|
2021-10-04 09:55:59 +02:00
|
|
|
while Compensation.objects.filter(identifier=identifier).exists():
|
2021-10-05 16:35:24 +02:00
|
|
|
identifier = tmp.generate_new_identifier()
|
2021-10-04 09:55:59 +02:00
|
|
|
return JsonResponse(
|
|
|
|
data={
|
2022-01-27 11:37:38 +01:00
|
|
|
"gen_data": identifier
|
2021-10-04 09:55:59 +02:00
|
|
|
}
|
|
|
|
)
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-02 16:23:29 +02:00
|
|
|
@default_group_required
|
2021-11-09 13:06:22 +01:00
|
|
|
@shared_access_required(Compensation, "id")
|
2021-07-01 13:36:07 +02:00
|
|
|
def edit_view(request: HttpRequest, id: str):
|
2021-10-04 16:18:59 +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-04 16:18:59 +02:00
|
|
|
# Get object from db
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
# Create forms, initialize with values from db/from POST request
|
|
|
|
data_form = EditCompensationForm(request.POST or None, instance=comp)
|
|
|
|
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=comp)
|
|
|
|
if request.method == "POST":
|
|
|
|
if data_form.is_valid() and geom_form.is_valid():
|
2021-11-17 12:09:49 +01:00
|
|
|
# Preserve state of intervention recorded/checked to determine whether the user must be informed or not
|
|
|
|
# about a change of the recorded/checked state
|
|
|
|
intervention_recorded = comp.intervention.recorded is not None
|
|
|
|
intervention_checked = comp.intervention.checked is not None
|
|
|
|
|
2021-10-04 16:18:59 +02:00
|
|
|
# The data form takes the geom form for processing, as well as the performing user
|
|
|
|
comp = data_form.save(request.user, geom_form)
|
2021-11-17 12:09:49 +01:00
|
|
|
if intervention_recorded or intervention_checked:
|
|
|
|
messages.info(request, CHECKED_RECORDED_RESET)
|
2021-10-04 16:18:59 +02:00
|
|
|
messages.success(request, _("Compensation {} edited").format(comp.identifier))
|
2021-10-13 09:26:46 +02:00
|
|
|
return redirect("compensation:detail", id=comp.id)
|
2021-10-04 16:18:59 +02:00
|
|
|
else:
|
2021-10-20 13:23:35 +02:00
|
|
|
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
2021-10-04 16:18:59 +02:00
|
|
|
else:
|
|
|
|
# For clarification: nothing in this case
|
|
|
|
pass
|
|
|
|
context = {
|
|
|
|
"form": data_form,
|
|
|
|
"geom_form": geom_form,
|
2022-01-20 12:19:08 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("Edit {}").format(comp.identifier),
|
2021-10-04 16:18:59 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-08-03 09:45:41 +02:00
|
|
|
@any_group_check
|
2021-10-13 09:26:46 +02:00
|
|
|
def detail_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:
|
|
|
|
|
|
|
|
"""
|
2021-08-09 15:09:43 +02:00
|
|
|
template = "compensation/detail/compensation/view.html"
|
2021-08-03 13:13:01 +02:00
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
geom_form = SimpleGeomForm(instance=comp)
|
2022-01-05 14:13:26 +01:00
|
|
|
parcels = comp.get_underlying_parcels()
|
2021-08-03 13:13:01 +02:00
|
|
|
_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
|
2021-10-14 08:21:51 +02:00
|
|
|
before_states = comp.before_states.all().prefetch_related("biotope_type").order_by("-surface")
|
|
|
|
after_states = comp.after_states.all().prefetch_related("biotope_type").order_by("-surface")
|
|
|
|
actions = comp.actions.all().prefetch_related("action_type")
|
2021-08-03 17:22:41 +02:00
|
|
|
|
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)
|
|
|
|
|
2021-12-15 15:10:35 +01:00
|
|
|
request = comp.set_status_messages(request)
|
2021-10-25 08:51:06 +02:00
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
context = {
|
2021-08-09 14:39:36 +02:00
|
|
|
"obj": comp,
|
2021-08-03 13:13:01 +02:00
|
|
|
"geom_form": geom_form,
|
2022-01-05 14:13:26 +01:00
|
|
|
"parcels": parcels,
|
2021-08-03 13:13:01 +02:00
|
|
|
"has_access": is_data_shared,
|
2021-10-14 08:21:51 +02:00
|
|
|
"actions": actions,
|
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-10 13:57:03 +02:00
|
|
|
"LANIS_LINK": comp.get_LANIS_link(),
|
2022-01-20 12:19:08 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: f"{comp.identifier} - {comp.title}",
|
2021-08-03 13:13:01 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
2021-08-05 12:54:28 +02:00
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
2021-08-05 12:54:28 +02:00
|
|
|
def log_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a log view using modal
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, 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-05 12:54:28 +02:00
|
|
|
"modal_title": _("Log"),
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
2021-08-03 13:13:01 +02:00
|
|
|
return render(request, template, context)
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
2021-07-01 13:36:07 +02:00
|
|
|
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)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = RemoveModalForm(request.POST or None, instance=comp, request=request)
|
2021-08-02 10:53:34 +02:00
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
2022-02-03 15:29:22 +01:00
|
|
|
msg_success=COMPENSATION_REMOVED_TEMPLATE.format(comp.identifier),
|
2021-08-19 13:44:06 +02:00
|
|
|
redirect_url=reverse("compensation:index"),
|
2021-08-02 10:53:34 +02:00
|
|
|
)
|
2021-08-09 14:39:36 +02:00
|
|
|
|
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
2021-08-03 13:13:01 +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 compensation's id to which the new document will be related
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
2022-02-10 10:21:18 +01:00
|
|
|
form = NewCompensationDocumentModalForm(request.POST or None, request.FILES or None, instance=comp, request=request)
|
2021-08-03 13:13:01 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 09:18:46 +01:00
|
|
|
msg_success=DOCUMENT_ADDED,
|
2022-02-02 10:17:59 +01:00
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
2021-08-03 13:13:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
2022-02-10 10:44:44 +01:00
|
|
|
@shared_access_required(Compensation, "id")
|
|
|
|
def get_document_view(request: HttpRequest, id: str, doc_id: str):
|
2021-09-01 16:24:49 +02:00
|
|
|
""" Returns the document as downloadable file
|
|
|
|
|
|
|
|
Wraps the generic document fetcher function from konova.utils.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
2022-02-10 10:44:44 +01:00
|
|
|
id (str): The compensation id
|
2021-09-01 16:24:49 +02:00
|
|
|
doc_id (str): The document id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-10 10:44:44 +01:00
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
2021-09-01 16:24:49 +02:00
|
|
|
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
|
|
|
return get_document(doc)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
2022-02-10 10:44:44 +01:00
|
|
|
@shared_access_required(Compensation, "id")
|
|
|
|
def remove_document_view(request: HttpRequest, id: str, doc_id: str):
|
2021-09-01 16:24:49 +02:00
|
|
|
""" Removes the document from the database and file system
|
|
|
|
|
|
|
|
Wraps the generic functionality from konova.utils.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
2022-02-10 10:44:44 +01:00
|
|
|
id (str): The compensation id
|
2021-09-01 16:24:49 +02:00
|
|
|
doc_id (str): The document id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-10 10:44:44 +01:00
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
2021-09-01 16:24:49 +02:00
|
|
|
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
|
|
|
return remove_document(
|
|
|
|
request,
|
|
|
|
doc
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-10 10:44:44 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
|
|
|
def edit_document_view(request: HttpRequest, id: str, 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
|
|
|
|
id (str): The compensation id
|
|
|
|
doc_id (str): The document id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
|
|
|
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=comp, document=doc, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
DOCUMENT_EDITED,
|
|
|
|
reverse("compensation:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
2021-08-03 13:13:01 +02:00
|
|
|
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)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewStateModalForm(request.POST or None, instance=comp, request=request)
|
2021-08-03 13:13:01 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 16:56:08 +01:00
|
|
|
msg_success=COMPENSATION_STATE_ADDED,
|
2022-02-02 10:17:59 +01:00
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
2021-08-03 13:13:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-04 10:44:02 +02:00
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
2021-08-04 10:44:02 +02:00
|
|
|
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)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewActionModalForm(request.POST or None, instance=comp, request=request)
|
2021-08-04 10:44:02 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 16:56:08 +01:00
|
|
|
msg_success=COMPENSATION_ACTION_ADDED,
|
2022-02-02 10:17:59 +01:00
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
2021-08-04 10:44:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-10 11:31:13 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
|
|
|
def action_edit_view(request: HttpRequest, id: str, action_id: str):
|
|
|
|
""" Renders a form for editing actions for a compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
action_id (str): The action's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
action = get_object_or_404(CompensationAction, id=action_id)
|
|
|
|
form = EditCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=COMPENSATION_ACTION_EDITED,
|
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
2021-08-03 13:13:01 +02:00
|
|
|
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)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewDeadlineModalForm(request.POST or None, instance=comp, request=request)
|
2021-08-03 13:13:01 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-07 09:56:37 +01:00
|
|
|
msg_success=DEADLINE_ADDED,
|
2022-02-02 10:17:59 +01:00
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-10 12:42:41 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
|
|
|
def deadline_edit_view(request: HttpRequest, id: str, deadline_id: str):
|
|
|
|
""" Renders a form for editing deadlines from a compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
deadline_id (str): The deadline's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
deadline = get_object_or_404(Deadline, id=deadline_id)
|
|
|
|
form = EditDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=DEADLINE_EDITED,
|
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-02 10:17:59 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "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(Compensation, id=id)
|
2022-02-02 10:17:59 +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 10:17:59 +01:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-07 09:56:37 +01:00
|
|
|
msg_success=DEADLINE_REMOVED,
|
2022-02-02 10:17:59 +01:00
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
2021-08-03 13:13:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
2021-11-09 13:06:22 +01:00
|
|
|
@shared_access_required(Compensation, "id")
|
|
|
|
def state_remove_view(request: HttpRequest, id: str, state_id: str):
|
2021-08-04 10:44:02 +02:00
|
|
|
""" Renders a form for removing a compensation state
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
2021-11-09 13:06:22 +01:00
|
|
|
id (str): The compensation's id
|
|
|
|
state_id (str): The state's id
|
2021-08-04 10:44:02 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-08 13:16:20 +01:00
|
|
|
comp = get_object_or_404(Compensation, 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=comp, state=state, request=request)
|
2021-08-03 13:13:01 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 16:56:08 +01:00
|
|
|
msg_success=COMPENSATION_STATE_REMOVED,
|
2022-02-02 10:17:59 +01:00
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
2021-08-03 13:13:01 +02:00
|
|
|
)
|
2021-08-04 10:44:02 +02:00
|
|
|
|
|
|
|
|
2022-02-10 11:02:30 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Compensation, "id")
|
|
|
|
def state_edit_view(request: HttpRequest, id: str, state_id: str):
|
|
|
|
""" Renders a form for editing a compensation state
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
state_id (str): The state's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
state = get_object_or_404(CompensationState, id=state_id)
|
|
|
|
form = EditCompensationStateModalForm(request.POST or None, instance=comp, state=state, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=COMPENSATION_STATE_EDITED,
|
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-04 10:44:02 +02:00
|
|
|
@login_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@default_group_required
|
2021-11-09 13:06:22 +01:00
|
|
|
@shared_access_required(Compensation, "id")
|
|
|
|
def action_remove_view(request: HttpRequest, id: str, action_id: str):
|
2021-08-04 10:44:02 +02:00
|
|
|
""" Renders a form for removing a compensation action
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
2021-11-09 13:06:22 +01:00
|
|
|
id (str): The compensation's id
|
2021-08-04 10:44:02 +02:00
|
|
|
id (str): The action's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-08 13:16:20 +01:00
|
|
|
comp = get_object_or_404(Compensation, 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=comp, action=action, request=request)
|
2021-08-04 10:44:02 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 16:56:08 +01:00
|
|
|
msg_success=COMPENSATION_ACTION_REMOVED,
|
2022-02-02 10:17:59 +01:00
|
|
|
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
2021-08-04 10:44:02 +02:00
|
|
|
)
|
2021-10-13 17:35:11 +02:00
|
|
|
|
|
|
|
|
2021-11-09 13:06:22 +01:00
|
|
|
def report_view(request: HttpRequest, id: str):
|
2021-10-13 17:35:11 +02:00
|
|
|
""" 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 compensations are structurally identical
|
|
|
|
template = "compensation/report/compensation/report.html"
|
|
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
|
|
|
2022-01-20 12:19:08 +01:00
|
|
|
tab_title = _("Report {}").format(comp.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:26:41 +01:00
|
|
|
if not comp.is_ready_for_publish():
|
2021-10-13 17:35:11 +02:00
|
|
|
template = "report/unavailable.html"
|
2022-01-20 12:19:08 +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
|
|
|
|
|
|
|
# Prepare data for map viewer
|
|
|
|
geom_form = SimpleGeomForm(
|
|
|
|
instance=comp
|
|
|
|
)
|
2022-01-05 14:41:32 +01:00
|
|
|
parcels = comp.get_underlying_parcels()
|
2021-10-13 17:35:11 +02:00
|
|
|
qrcode_img = generate_qr_code(
|
|
|
|
request.build_absolute_uri(reverse("compensation:report", args=(id,))),
|
|
|
|
10
|
|
|
|
)
|
|
|
|
qrcode_img_lanis = generate_qr_code(
|
|
|
|
comp.get_LANIS_link(),
|
|
|
|
7
|
|
|
|
)
|
|
|
|
# Order states by surface
|
2021-10-14 08:21:51 +02:00
|
|
|
before_states = comp.before_states.all().order_by("-surface").prefetch_related("biotope_type")
|
|
|
|
after_states = comp.after_states.all().order_by("-surface").prefetch_related("biotope_type")
|
|
|
|
actions = comp.actions.all().prefetch_related("action_type")
|
|
|
|
|
2021-10-13 17:35:11 +02:00
|
|
|
context = {
|
|
|
|
"obj": comp,
|
|
|
|
"qrcode": qrcode_img,
|
|
|
|
"qrcode_lanis": qrcode_img_lanis,
|
|
|
|
"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 08:21:51 +02:00
|
|
|
"actions": actions,
|
2022-01-20 12:19:08 +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)
|