2021-07-01 13:36:07 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2022-02-02 15:16:25 +01:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2021-07-01 13:36:07 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-02-02 15:16:25 +01:00
|
|
|
from django.http import HttpRequest, JsonResponse, Http404
|
2021-10-13 14:03:34 +02:00
|
|
|
from django.shortcuts import render
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-09-27 11:45:13 +02:00
|
|
|
from intervention.forms.forms import NewInterventionForm, EditInterventionForm
|
2021-11-16 12:54:28 +01:00
|
|
|
from intervention.forms.modalForms import ShareModalForm, NewRevocationModalForm, \
|
2022-02-10 10:21:18 +01:00
|
|
|
CheckModalForm, NewDeductionModalForm, NewInterventionDocumentModalForm, RemoveEcoAccountDeductionModalForm, \
|
2022-02-09 16:02:28 +01:00
|
|
|
RemoveRevocationModalForm, EditEcoAccountDeductionModalForm, EditRevocationModalForm
|
2021-09-01 16:24:49 +02:00
|
|
|
from intervention.models import Intervention, Revocation, InterventionDocument, RevocationDocument
|
2021-07-01 13:36:07 +02:00
|
|
|
from intervention.tables import InterventionTable
|
|
|
|
from konova.contexts import BaseContext
|
|
|
|
from konova.decorators import *
|
2022-02-10 10:21:18 +01:00
|
|
|
from konova.forms import SimpleGeomForm, RemoveModalForm, RecordModalForm, EditDocumentModalForm
|
2022-01-20 12:12:04 +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 remove_document, get_document
|
2021-10-13 14:03:34 +02:00
|
|
|
from konova.utils.generators import generate_qr_code
|
2021-10-25 08:51:06 +02:00
|
|
|
from konova.utils.message_templates import INTERVENTION_INVALID, FORM_INVALID, IDENTIFIER_REPLACED, \
|
2022-02-03 15:29:22 +01:00
|
|
|
CHECKED_RECORDED_RESET, DEDUCTION_REMOVED, DEDUCTION_ADDED, REVOCATION_ADDED, REVOCATION_REMOVED, \
|
2022-04-19 09:43:36 +02:00
|
|
|
COMPENSATION_REMOVED_TEMPLATE, DOCUMENT_ADDED, DEDUCTION_EDITED, REVOCATION_EDITED, DOCUMENT_EDITED, \
|
2022-05-30 10:26:34 +02:00
|
|
|
RECORDED_BLOCKS_EDIT, DATA_CHECKED_PREVIOUSLY_TEMPLATE
|
2021-08-02 16:23:29 +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):
|
|
|
|
"""
|
2021-07-01 15:08:22 +02:00
|
|
|
Renders the index view for Interventions
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A rendered view
|
|
|
|
"""
|
|
|
|
template = "generic_index.html"
|
2021-07-22 10:00:59 +02:00
|
|
|
|
|
|
|
# Filtering by user access is performed in table filter inside of InterventionTableFilter class
|
2021-07-20 14:05:44 +02:00
|
|
|
interventions = Intervention.objects.filter(
|
2021-08-02 11:52:20 +02:00
|
|
|
deleted=None, # not deleted
|
2021-10-14 14:12:33 +02:00
|
|
|
).select_related(
|
|
|
|
"legal"
|
2021-07-20 14:05:44 +02:00
|
|
|
)
|
2021-07-01 13:36:07 +02:00
|
|
|
table = InterventionTable(
|
|
|
|
request=request,
|
|
|
|
queryset=interventions
|
|
|
|
)
|
|
|
|
context = {
|
|
|
|
"table": table,
|
2022-01-20 12:12:04 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("Interventions - 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-07-01 13:36:07 +02:00
|
|
|
def new_view(request: HttpRequest):
|
|
|
|
"""
|
|
|
|
Renders a view for a new intervention creation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-10-06 16:00:17 +02:00
|
|
|
template = "intervention/form/view.html"
|
2021-09-23 15:05:17 +02:00
|
|
|
data_form = NewInterventionForm(request.POST or None)
|
|
|
|
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
2021-07-01 13:36:07 +02:00
|
|
|
if request.method == "POST":
|
2021-09-23 15:05:17 +02:00
|
|
|
if data_form.is_valid() and geom_form.is_valid():
|
|
|
|
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
|
|
|
intervention = data_form.save(request.user, geom_form)
|
|
|
|
if generated_identifier != intervention.identifier:
|
|
|
|
messages.info(
|
|
|
|
request,
|
2021-10-04 09:55:59 +02:00
|
|
|
IDENTIFIER_REPLACED.format(
|
2021-09-27 11:12:40 +02:00
|
|
|
generated_identifier,
|
|
|
|
intervention.identifier
|
|
|
|
)
|
2021-09-23 15:05:17 +02:00
|
|
|
)
|
|
|
|
messages.success(request, _("Intervention {} added").format(intervention.identifier))
|
2021-10-13 09:26:46 +02:00
|
|
|
return redirect("intervention:detail", id=intervention.id)
|
2021-07-01 13:36:07 +02:00
|
|
|
else:
|
2021-10-20 13:23:35 +02:00
|
|
|
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
2021-07-01 13:36:07 +02:00
|
|
|
else:
|
|
|
|
# For clarification: nothing in this case
|
|
|
|
pass
|
|
|
|
context = {
|
2021-09-29 14:49:17 +02:00
|
|
|
"form": data_form,
|
2021-09-23 15:05:17 +02:00
|
|
|
"geom_form": geom_form,
|
2022-01-20 12:12:04 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("New intervention"),
|
2021-07-01 13:36:07 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
2021-09-27 13:57:56 +02:00
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2021-09-27 13:57:56 +02:00
|
|
|
def new_id_view(request: HttpRequest):
|
|
|
|
""" JSON endpoint
|
|
|
|
|
|
|
|
Provides fetching of free identifiers for e.g. AJAX calls
|
|
|
|
|
|
|
|
"""
|
|
|
|
tmp_intervention = Intervention()
|
2021-10-05 16:35:24 +02:00
|
|
|
identifier = tmp_intervention.generate_new_identifier()
|
2021-09-27 13:57:56 +02:00
|
|
|
while Intervention.objects.filter(identifier=identifier).exists():
|
2021-10-05 16:35:24 +02:00
|
|
|
identifier = tmp_intervention.generate_new_identifier()
|
2021-09-27 13:57:56 +02:00
|
|
|
return JsonResponse(
|
|
|
|
data={
|
2022-01-27 11:37:38 +01:00
|
|
|
"gen_data": identifier
|
2021-09-27 13:57:56 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-26 15:16:16 +02:00
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "id")
|
2021-07-26 15:16:16 +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 intervention's id to which the new document will be related
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-07-26 15:34:30 +02:00
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2022-02-10 10:21:18 +01:00
|
|
|
form = NewInterventionDocumentModalForm(request.POST or None, request.FILES or None, instance=intervention, request=request)
|
2021-08-04 15:19:06 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-04 09:18:46 +01:00
|
|
|
msg_success=DOCUMENT_ADDED,
|
2022-02-02 09:32:34 +01:00
|
|
|
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
2021-08-04 15:19:06 +02:00
|
|
|
)
|
2021-07-26 15:16:16 +02:00
|
|
|
|
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2021-09-01 16:24:49 +02:00
|
|
|
def get_revocation_view(request: HttpRequest, doc_id: str):
|
|
|
|
""" Returns the revocation 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(RevocationDocument, id=doc_id)
|
2021-10-26 11:38:34 +02:00
|
|
|
# File download only possible if related instance is shared with user
|
2021-11-15 12:18:22 +01:00
|
|
|
if not doc.instance.legal.intervention.users.filter(id=request.user.id):
|
2021-10-26 11:38:34 +02:00
|
|
|
messages.info(
|
|
|
|
request,
|
|
|
|
DATA_UNSHARED
|
|
|
|
)
|
|
|
|
return redirect("intervention:detail", id=doc.instance.id)
|
2021-09-01 16:24:49 +02:00
|
|
|
return get_document(doc)
|
|
|
|
|
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2022-02-10 10:21:18 +01:00
|
|
|
@shared_access_required(Intervention, "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:21:18 +01:00
|
|
|
id (str): The intervention id
|
2021-09-01 16:24:49 +02:00
|
|
|
doc_id (str): The document id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-10 10:21:18 +01:00
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2021-09-01 16:24:49 +02:00
|
|
|
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
|
|
|
return get_document(doc)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2022-02-10 10:21:18 +01:00
|
|
|
@shared_access_required(Intervention, "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:21:18 +01:00
|
|
|
id (str): The intervention id
|
2021-09-01 16:24:49 +02:00
|
|
|
doc_id (str): The document id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-10 10:21:18 +01:00
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2021-09-01 16:24:49 +02:00
|
|
|
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
|
|
|
return remove_document(
|
|
|
|
request,
|
|
|
|
doc
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-10 10:21:18 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Intervention, "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 intervention id
|
|
|
|
doc_id (str): The document id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
|
|
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
|
|
|
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=intervention, document=doc, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
DOCUMENT_EDITED,
|
|
|
|
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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-07-22 13:19:14 +02:00
|
|
|
""" Renders a detail view for viewing an intervention's data
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The intervention's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-07-26 14:35:19 +02:00
|
|
|
template = "intervention/detail/view.html"
|
2021-08-02 10:53:34 +02:00
|
|
|
|
|
|
|
# Fetch data, filter out deleted related data
|
2021-10-14 14:12:33 +02:00
|
|
|
intervention = get_object_or_404(
|
|
|
|
Intervention.objects.select_related(
|
|
|
|
"geometry",
|
|
|
|
"legal",
|
|
|
|
"responsible",
|
|
|
|
),
|
|
|
|
id=id
|
|
|
|
)
|
2021-08-02 10:53:34 +02:00
|
|
|
compensations = intervention.compensations.filter(
|
2021-08-02 11:52:20 +02:00
|
|
|
deleted=None,
|
2021-08-02 10:53:34 +02:00
|
|
|
)
|
2021-08-02 16:23:29 +02:00
|
|
|
_user = request.user
|
|
|
|
is_data_shared = intervention.is_shared_with(user=_user)
|
2021-07-22 13:19:14 +02:00
|
|
|
|
|
|
|
geom_form = SimpleGeomForm(
|
2021-11-17 14:33:05 +01:00
|
|
|
instance=intervention,
|
2021-07-22 13:19:14 +02:00
|
|
|
)
|
2022-05-30 10:26:34 +02:00
|
|
|
last_checked = intervention.get_last_checked_action()
|
|
|
|
last_checked_tooltip = ""
|
|
|
|
if last_checked:
|
|
|
|
last_checked_tooltip = DATA_CHECKED_PREVIOUSLY_TEMPLATE.format(last_checked.get_timestamp_str_formatted(), last_checked.user)
|
2021-07-22 13:19:14 +02:00
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
context = {
|
2021-10-13 09:26:46 +02:00
|
|
|
"obj": intervention,
|
2022-05-30 10:26:34 +02:00
|
|
|
"last_checked": last_checked,
|
|
|
|
"last_checked_tooltip": last_checked_tooltip,
|
2021-08-02 10:53:34 +02:00
|
|
|
"compensations": compensations,
|
2021-08-02 16:23:29 +02:00
|
|
|
"has_access": is_data_shared,
|
2021-07-22 13:19:14 +02:00
|
|
|
"geom_form": geom_form,
|
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),
|
2022-01-20 12:12:04 +01:00
|
|
|
"LANIS_LINK": intervention.get_LANIS_link(),
|
|
|
|
TAB_TITLE_IDENTIFIER: f"{intervention.identifier} - {intervention.title}",
|
2021-07-01 13:36:07 +02:00
|
|
|
}
|
2021-07-22 16:06:13 +02:00
|
|
|
|
2021-12-15 13:59:52 +01:00
|
|
|
request = intervention.set_status_messages(request)
|
2021-07-22 16:06:13 +02:00
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "id")
|
2021-07-01 13:36:07 +02:00
|
|
|
def edit_view(request: HttpRequest, id: str):
|
|
|
|
"""
|
|
|
|
Renders a view for editing interventions
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-10-06 16:00:17 +02:00
|
|
|
template = "intervention/form/view.html"
|
2021-09-27 11:12:40 +02:00
|
|
|
# Get object from db
|
2021-07-01 13:36:07 +02:00
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2022-04-19 09:43:36 +02:00
|
|
|
if intervention.is_recorded:
|
|
|
|
messages.info(
|
|
|
|
request,
|
|
|
|
RECORDED_BLOCKS_EDIT
|
|
|
|
)
|
|
|
|
return redirect("intervention:detail", id=id)
|
|
|
|
|
2021-09-27 11:12:40 +02:00
|
|
|
# Create forms, initialize with values from db/from POST request
|
2021-09-27 09:40:00 +02:00
|
|
|
data_form = EditInterventionForm(request.POST or None, instance=intervention)
|
|
|
|
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=intervention)
|
2021-07-01 13:36:07 +02:00
|
|
|
if request.method == "POST":
|
2021-09-27 09:40:00 +02:00
|
|
|
if data_form.is_valid() and geom_form.is_valid():
|
2021-09-27 11:12:40 +02:00
|
|
|
# The data form takes the geom form for processing, as well as the performing user
|
2021-10-25 17:39:39 +02:00
|
|
|
# Save the current state of recorded|checked to inform the user in case of a status reset due to editing
|
|
|
|
i_rec = intervention.recorded is not None
|
|
|
|
i_check = intervention.checked is not None
|
2021-09-27 09:40:00 +02:00
|
|
|
intervention = data_form.save(request.user, geom_form)
|
2021-09-27 11:12:40 +02:00
|
|
|
messages.success(request, _("Intervention {} edited").format(intervention.identifier))
|
2021-10-25 17:39:39 +02:00
|
|
|
if i_check or i_rec:
|
|
|
|
messages.info(request, CHECKED_RECORDED_RESET)
|
2021-10-13 09:26:46 +02:00
|
|
|
return redirect("intervention:detail", id=intervention.id)
|
2021-07-01 13:36:07 +02:00
|
|
|
else:
|
2021-10-20 13:23:35 +02:00
|
|
|
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
2021-09-27 09:40:00 +02:00
|
|
|
else:
|
|
|
|
# For clarification: nothing in this case
|
|
|
|
pass
|
2021-07-01 13:36:07 +02:00
|
|
|
context = {
|
2021-09-29 14:49:17 +02:00
|
|
|
"form": data_form,
|
2021-09-27 09:40:00 +02:00
|
|
|
"geom_form": geom_form,
|
2022-01-20 12:12:04 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: _("Edit {}").format(intervention.identifier),
|
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-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "id")
|
2021-07-01 13:36:07 +02:00
|
|
|
def remove_view(request: HttpRequest, id: str):
|
2021-07-01 15:08:22 +02:00
|
|
|
""" Renders a remove view for this intervention
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The uuid id as string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-07-01 15:08:22 +02:00
|
|
|
obj = Intervention.objects.get(id=id)
|
2021-08-02 15:39:33 +02:00
|
|
|
identifier = obj.identifier
|
2021-11-17 14:33:05 +01:00
|
|
|
form = RemoveModalForm(request.POST or None, instance=obj, request=request)
|
2021-08-02 15:39:33 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
_("{} removed").format(identifier),
|
|
|
|
redirect_url=reverse("intervention:index")
|
2021-07-01 13:36:07 +02:00
|
|
|
)
|
2021-07-30 13:30:42 +02:00
|
|
|
|
|
|
|
|
2022-02-09 16:02:28 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Intervention, "id")
|
|
|
|
def edit_revocation_view(request: HttpRequest, id: str, revocation_id: str):
|
|
|
|
""" Renders a edit view for a revocation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The intervention's id as string
|
|
|
|
revocation_id (str): The revocation's id as string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
|
|
revocation = get_object_or_404(Revocation, id=revocation_id)
|
|
|
|
|
|
|
|
form = EditRevocationModalForm(request.POST or None, request.FILES or None, instance=intervention, revocation=revocation, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
REVOCATION_EDITED,
|
|
|
|
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-04 13:32:35 +02:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2022-02-08 13:27:42 +01:00
|
|
|
@shared_access_required(Intervention, "id")
|
2022-02-08 13:16:20 +01:00
|
|
|
def remove_revocation_view(request: HttpRequest, id: str, revocation_id: str):
|
2021-08-04 13:32:35 +02:00
|
|
|
""" Renders a remove view for a revocation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
2022-02-09 16:02:28 +01:00
|
|
|
id (str): The intervention's id as string
|
|
|
|
revocation_id (str): The revocation's id as string
|
2021-08-04 13:32:35 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-02-08 13:16:20 +01:00
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
|
|
revocation = get_object_or_404(Revocation, id=revocation_id)
|
2022-02-02 09:32:34 +01:00
|
|
|
|
2022-02-08 13:31:40 +01:00
|
|
|
form = RemoveRevocationModalForm(request.POST or None, instance=intervention, revocation=revocation, request=request)
|
2021-08-04 13:32:35 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-03 12:10:23 +01:00
|
|
|
REVOCATION_REMOVED,
|
2022-02-08 13:16:20 +01:00
|
|
|
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
2021-08-04 13:32:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-30 13:30:42 +02:00
|
|
|
@login_required
|
|
|
|
def share_view(request: HttpRequest, id: str, token: str):
|
|
|
|
""" Performs sharing of an intervention
|
|
|
|
|
|
|
|
If token given in url is not valid, the user will be redirected to the dashboard
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): Intervention's id
|
|
|
|
token (str): Access token for intervention
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = request.user
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
|
|
# Check tokens
|
|
|
|
if intervention.access_token == token:
|
|
|
|
# Send different messages in case user has already been added to list of sharing users
|
2021-08-02 16:23:29 +02:00
|
|
|
if intervention.is_shared_with(user):
|
2021-07-30 13:30:42 +02:00
|
|
|
messages.info(
|
|
|
|
request,
|
|
|
|
_("{} has already been shared with you").format(intervention.identifier)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("{} has been shared with you").format(intervention.identifier)
|
|
|
|
)
|
2022-02-18 11:02:40 +01:00
|
|
|
intervention.share_with_user(user)
|
2021-10-13 09:26:46 +02:00
|
|
|
return redirect("intervention:detail", id=id)
|
2021-07-30 13:30:42 +02:00
|
|
|
else:
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
_("Share link invalid"),
|
|
|
|
extra_tags="danger",
|
|
|
|
)
|
|
|
|
return redirect("home")
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "id")
|
2021-07-30 13:30:42 +02:00
|
|
|
def create_share_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders sharing form for an intervention
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): Intervention's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = ShareModalForm(request.POST or None, instance=intervention, request=request)
|
2021-08-19 13:02:31 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Share settings updated")
|
|
|
|
)
|
2021-07-30 13:30:42 +02:00
|
|
|
|
|
|
|
|
2021-08-04 15:19:06 +02:00
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@registration_office_group_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "id")
|
2021-11-11 10:37:22 +01:00
|
|
|
def check_view(request: HttpRequest, id: str):
|
2021-08-04 15:19:06 +02:00
|
|
|
""" Renders check form for an intervention
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): Intervention's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = CheckModalForm(request.POST or None, instance=intervention, request=request)
|
2021-08-10 17:19:42 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_success=_("Check performed"),
|
|
|
|
msg_error=INTERVENTION_INVALID
|
|
|
|
)
|
2021-08-04 15:19:06 +02:00
|
|
|
|
|
|
|
|
2021-08-04 13:32:35 +02:00
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "id")
|
2021-08-04 13:32:35 +02:00
|
|
|
def new_revocation_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders sharing form for an intervention
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): Intervention's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewRevocationModalForm(request.POST or None, request.FILES or None, instance=intervention, request=request)
|
2021-08-19 13:02:31 +02:00
|
|
|
return form.process_request(
|
|
|
|
request,
|
2022-02-03 12:10:23 +01:00
|
|
|
msg_success=REVOCATION_ADDED,
|
2022-02-02 09:32:34 +01:00
|
|
|
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
2021-08-19 13:02:31 +02:00
|
|
|
)
|
2021-08-04 13:32:35 +02:00
|
|
|
|
|
|
|
|
2021-08-05 12:54:28 +02:00
|
|
|
@login_required
|
2021-10-26 11:38:34 +02:00
|
|
|
@default_group_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "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:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, 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": intervention.log.all(),
|
2021-08-05 12:54:28 +02:00
|
|
|
"modal_title": _("Log"),
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-08-10 10:42:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@default_group_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "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:
|
|
|
|
request (HttpRequest): The incoming request
|
2021-08-30 11:29:15 +02:00
|
|
|
id (str): The intervention's id which shall benefit from this deduction
|
2021-08-10 10:42:04 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = NewDeductionModalForm(request.POST or None, instance=intervention, 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 09:32:34 +01:00
|
|
|
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data",
|
2021-08-10 10:42:04 +02:00
|
|
|
)
|
2021-08-10 17:19:42 +02:00
|
|
|
|
|
|
|
|
2022-02-02 15:16:25 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Intervention, "id")
|
|
|
|
def remove_deduction_view(request: HttpRequest, id: str, deduction_id: str):
|
|
|
|
""" Renders a modal view for removing deductions
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The intervention's id
|
|
|
|
deduction_id (str): The deduction's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
|
|
try:
|
|
|
|
eco_deduction = intervention.deductions.get(id=deduction_id)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404("Unknown deduction")
|
|
|
|
|
2022-02-08 13:31:40 +01:00
|
|
|
form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request)
|
2022-02-02 15:16:25 +01:00
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=DEDUCTION_REMOVED,
|
|
|
|
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-09 14:49:56 +01:00
|
|
|
@login_required
|
|
|
|
@default_group_required
|
|
|
|
@shared_access_required(Intervention, "id")
|
|
|
|
def edit_deduction_view(request: HttpRequest, id: str, deduction_id: str):
|
|
|
|
""" Renders a modal view for removing deductions
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The intervention's id
|
|
|
|
deduction_id (str): The deduction's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
|
|
try:
|
|
|
|
eco_deduction = intervention.deductions.get(id=deduction_id)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404("Unknown deduction")
|
|
|
|
|
|
|
|
form = EditEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=DEDUCTION_EDITED,
|
|
|
|
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-10 17:19:42 +02:00
|
|
|
@login_required
|
|
|
|
@conservation_office_group_required
|
2021-10-27 14:44:49 +02:00
|
|
|
@shared_access_required(Intervention, "id")
|
2021-08-10 17:19:42 +02:00
|
|
|
def record_view(request: HttpRequest, id: str):
|
|
|
|
""" Renders a modal form for recording an intervention
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The intervention's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
2021-11-17 14:33:05 +01:00
|
|
|
form = RecordModalForm(request.POST or None, instance=intervention, request=request)
|
2021-08-10 17:19:42 +02:00
|
|
|
msg_succ = _("{} unrecorded") if intervention.recorded else _("{} recorded")
|
|
|
|
msg_succ = msg_succ.format(intervention.identifier)
|
|
|
|
return form.process_request(
|
|
|
|
request,
|
|
|
|
msg_succ,
|
|
|
|
msg_error=_("There are errors on this intervention:")
|
2021-10-13 14:03:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-03 15:29:22 +01:00
|
|
|
def remove_compensation_view(request:HttpRequest, id: str, comp_id: str):
|
|
|
|
""" Renders a modal view for removing the compensation
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The compensation's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
|
|
try:
|
|
|
|
comp = intervention.compensations.get(
|
|
|
|
id=comp_id
|
|
|
|
)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404("Unknown compensation")
|
|
|
|
form = RemoveModalForm(request.POST or None, instance=comp, request=request)
|
|
|
|
return form.process_request(
|
|
|
|
request=request,
|
|
|
|
msg_success=COMPENSATION_REMOVED_TEMPLATE.format(comp.identifier),
|
|
|
|
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-10-13 14:03:34 +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:
|
|
|
|
|
|
|
|
"""
|
|
|
|
template = "intervention/report/report.html"
|
|
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
|
|
|
2022-01-20 12:12:04 +01:00
|
|
|
tab_title = _("Report {}").format(intervention.identifier)
|
2021-10-13 14:03:34 +02:00
|
|
|
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
2022-01-21 09:02:56 +01:00
|
|
|
if not intervention.is_ready_for_publish():
|
2021-10-13 14:03:34 +02:00
|
|
|
template = "report/unavailable.html"
|
2022-01-20 12:12:04 +01:00
|
|
|
context = {
|
|
|
|
TAB_TITLE_IDENTIFIER: tab_title,
|
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|
2021-10-13 14:03:34 +02:00
|
|
|
|
2021-10-13 17:35:11 +02:00
|
|
|
# Prepare data for map viewer
|
|
|
|
geom_form = SimpleGeomForm(
|
|
|
|
instance=intervention
|
|
|
|
)
|
2022-01-05 14:41:32 +01:00
|
|
|
parcels = intervention.get_underlying_parcels()
|
2021-10-13 17:35:11 +02:00
|
|
|
|
2021-10-13 14:03:34 +02:00
|
|
|
distinct_deductions = intervention.deductions.all().distinct(
|
|
|
|
"account"
|
|
|
|
)
|
2022-04-13 14:57:05 +02:00
|
|
|
qrcode_url = request.build_absolute_uri(reverse("intervention:report", args=(id,)))
|
|
|
|
qrcode_img = generate_qr_code(qrcode_url, 10)
|
|
|
|
qrcode_lanis_url = intervention.get_LANIS_link()
|
|
|
|
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
|
|
|
|
2021-10-13 14:03:34 +02:00
|
|
|
context = {
|
|
|
|
"obj": intervention,
|
|
|
|
"deductions": distinct_deductions,
|
2022-04-13 14:57:05 +02:00
|
|
|
"qrcode": {
|
|
|
|
"img": qrcode_img,
|
|
|
|
"url": qrcode_url,
|
|
|
|
},
|
|
|
|
"qrcode_lanis": {
|
|
|
|
"img": qrcode_img_lanis,
|
|
|
|
"url": qrcode_lanis_url,
|
|
|
|
},
|
2021-10-13 17:35:11 +02:00
|
|
|
"geom_form": geom_form,
|
2022-01-05 14:41:32 +01:00
|
|
|
"parcels": parcels,
|
2022-01-20 12:12:04 +01:00
|
|
|
TAB_TITLE_IDENTIFIER: tab_title,
|
2021-10-13 14:03:34 +02:00
|
|
|
}
|
|
|
|
context = BaseContext(request, context).context
|
|
|
|
return render(request, template, context)
|