mpeltriaux
2553eb2825
* adds tests for views in intervention app * fixes bugs detected by test writing
561 lines
16 KiB
Python
561 lines
16 KiB
Python
from django.contrib.auth.decorators import login_required
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.http import HttpRequest, JsonResponse
|
|
from django.shortcuts import render
|
|
|
|
from intervention.forms.forms import NewInterventionForm, EditInterventionForm
|
|
from intervention.forms.modalForms import ShareInterventionModalForm, NewRevocationModalForm, \
|
|
RunCheckModalForm, NewDeductionModalForm
|
|
from intervention.models import Intervention, Revocation, InterventionDocument, RevocationDocument
|
|
from intervention.tables import InterventionTable
|
|
from konova.contexts import BaseContext
|
|
from konova.decorators import *
|
|
from konova.forms import SimpleGeomForm, NewDocumentForm, RemoveModalForm, RecordModalForm
|
|
from konova.sub_settings.django_settings import DEFAULT_DATE_FORMAT
|
|
from konova.utils.documents import remove_document, get_document
|
|
from konova.utils.generators import generate_qr_code
|
|
from konova.utils.message_templates import INTERVENTION_INVALID, FORM_INVALID, IDENTIFIER_REPLACED, \
|
|
DATA_UNSHARED_EXPLANATION, CHECKED_RECORDED_RESET
|
|
from konova.utils.user_checks import in_group
|
|
|
|
|
|
@login_required
|
|
@any_group_check
|
|
def index_view(request: HttpRequest):
|
|
"""
|
|
Renders the index view for Interventions
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
A rendered view
|
|
"""
|
|
template = "generic_index.html"
|
|
|
|
# Filtering by user access is performed in table filter inside of InterventionTableFilter class
|
|
interventions = Intervention.objects.filter(
|
|
deleted=None, # not deleted
|
|
).select_related(
|
|
"legal"
|
|
)
|
|
table = InterventionTable(
|
|
request=request,
|
|
queryset=interventions
|
|
)
|
|
context = {
|
|
"table": table,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
def new_view(request: HttpRequest):
|
|
"""
|
|
Renders a view for a new intervention creation
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "intervention/form/view.html"
|
|
data_form = NewInterventionForm(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)
|
|
intervention = data_form.save(request.user, geom_form)
|
|
if generated_identifier != intervention.identifier:
|
|
messages.info(
|
|
request,
|
|
IDENTIFIER_REPLACED.format(
|
|
generated_identifier,
|
|
intervention.identifier
|
|
)
|
|
)
|
|
messages.success(request, _("Intervention {} added").format(intervention.identifier))
|
|
return redirect("intervention:detail", id=intervention.id)
|
|
else:
|
|
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
|
else:
|
|
# For clarification: nothing in this case
|
|
pass
|
|
context = {
|
|
"form": data_form,
|
|
"geom_form": geom_form,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
def new_id_view(request: HttpRequest):
|
|
""" JSON endpoint
|
|
|
|
Provides fetching of free identifiers for e.g. AJAX calls
|
|
|
|
"""
|
|
tmp_intervention = Intervention()
|
|
identifier = tmp_intervention.generate_new_identifier()
|
|
while Intervention.objects.filter(identifier=identifier).exists():
|
|
identifier = tmp_intervention.generate_new_identifier()
|
|
return JsonResponse(
|
|
data={
|
|
"identifier": identifier
|
|
}
|
|
)
|
|
|
|
|
|
@login_required
|
|
@default_group_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 intervention's id to which the new document will be related
|
|
Returns:
|
|
|
|
"""
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
form = NewDocumentForm(request.POST or None, request.FILES or None, instance=intervention, user=request.user)
|
|
return form.process_request(
|
|
request,
|
|
msg_success=_("Document added")
|
|
)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
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)
|
|
# File download only possible if related instance is shared with user
|
|
if not doc.instance.users.filter(id=request.user.id):
|
|
messages.info(
|
|
request,
|
|
DATA_UNSHARED
|
|
)
|
|
return redirect("intervention:detail", id=doc.instance.id)
|
|
return get_document(doc)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
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(InterventionDocument, id=doc_id)
|
|
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
|
|
)
|
|
return redirect("intervention:detail", id=instance.id)
|
|
return get_document(doc)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
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(InterventionDocument, id=doc_id)
|
|
return remove_document(
|
|
request,
|
|
doc
|
|
)
|
|
|
|
|
|
@login_required
|
|
@any_group_check
|
|
def detail_view(request: HttpRequest, id: str):
|
|
""" Renders a detail view for viewing an intervention's data
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The intervention's id
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "intervention/detail/view.html"
|
|
|
|
# Fetch data, filter out deleted related data
|
|
intervention = get_object_or_404(
|
|
Intervention.objects.select_related(
|
|
"geometry",
|
|
"legal",
|
|
"responsible",
|
|
),
|
|
id=id
|
|
)
|
|
compensations = intervention.compensations.filter(
|
|
deleted=None,
|
|
)
|
|
_user = request.user
|
|
is_data_shared = intervention.is_shared_with(user=_user)
|
|
|
|
geom_form = SimpleGeomForm(
|
|
instance=intervention
|
|
)
|
|
|
|
# Inform user about revocation
|
|
if intervention.legal.revocation:
|
|
messages.error(
|
|
request,
|
|
_("This intervention has a revocation from {}").format(intervention.legal.revocation.date.strftime(DEFAULT_DATE_FORMAT)),
|
|
extra_tags="danger",
|
|
)
|
|
|
|
context = {
|
|
"obj": intervention,
|
|
"compensations": compensations,
|
|
"has_access": is_data_shared,
|
|
"geom_form": geom_form,
|
|
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
|
"is_zb_member": in_group(_user, ZB_GROUP),
|
|
"is_ets_member": in_group(_user, ETS_GROUP),
|
|
"LANIS_LINK": intervention.get_LANIS_link()
|
|
}
|
|
|
|
if not is_data_shared:
|
|
messages.info(request, DATA_UNSHARED_EXPLANATION)
|
|
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
def edit_view(request: HttpRequest, id: str):
|
|
"""
|
|
Renders a view for editing interventions
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "intervention/form/view.html"
|
|
# Get object from db
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
# Create forms, initialize with values from db/from POST request
|
|
data_form = EditInterventionForm(request.POST or None, instance=intervention)
|
|
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=intervention)
|
|
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
|
|
# 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
|
|
intervention = data_form.save(request.user, geom_form)
|
|
messages.success(request, _("Intervention {} edited").format(intervention.identifier))
|
|
if i_check or i_rec:
|
|
messages.info(request, CHECKED_RECORDED_RESET)
|
|
return redirect("intervention:detail", id=intervention.id)
|
|
else:
|
|
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
|
else:
|
|
# For clarification: nothing in this case
|
|
pass
|
|
context = {
|
|
"form": data_form,
|
|
"geom_form": geom_form,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
def remove_view(request: HttpRequest, id: str):
|
|
""" Renders a remove view for this intervention
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The uuid id as string
|
|
|
|
Returns:
|
|
|
|
"""
|
|
obj = Intervention.objects.get(id=id)
|
|
identifier = obj.identifier
|
|
form = RemoveModalForm(request.POST or None, instance=obj, user=request.user)
|
|
return form.process_request(
|
|
request,
|
|
_("{} removed").format(identifier),
|
|
redirect_url=reverse("intervention:index")
|
|
)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
def remove_revocation_view(request: HttpRequest, id: str):
|
|
""" Renders a remove view for a revocation
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The revocation's id as string
|
|
|
|
Returns:
|
|
|
|
"""
|
|
obj = Revocation.objects.get(id=id)
|
|
form = RemoveModalForm(request.POST or None, instance=obj, user=request.user)
|
|
return form.process_request(
|
|
request,
|
|
_("Revocation removed"),
|
|
)
|
|
|
|
|
|
@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
|
|
if intervention.is_shared_with(user):
|
|
messages.info(
|
|
request,
|
|
_("{} has already been shared with you").format(intervention.identifier)
|
|
)
|
|
else:
|
|
messages.success(
|
|
request,
|
|
_("{} has been shared with you").format(intervention.identifier)
|
|
)
|
|
intervention.users.add(user)
|
|
return redirect("intervention:detail", id=id)
|
|
else:
|
|
messages.error(
|
|
request,
|
|
_("Share link invalid"),
|
|
extra_tags="danger",
|
|
)
|
|
return redirect("home")
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
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)
|
|
form = ShareInterventionModalForm(request.POST or None, instance=intervention, request=request)
|
|
return form.process_request(
|
|
request,
|
|
msg_success=_("Share settings updated")
|
|
)
|
|
|
|
|
|
@login_required
|
|
@registration_office_group_required
|
|
def run_check_view(request: HttpRequest, id: str):
|
|
""" 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)
|
|
form = RunCheckModalForm(request.POST or None, instance=intervention, user=request.user)
|
|
return form.process_request(
|
|
request,
|
|
msg_success=_("Check performed"),
|
|
msg_error=INTERVENTION_INVALID
|
|
)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
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)
|
|
form = NewRevocationModalForm(request.POST or None, request.FILES or None, instance=intervention, user=request.user)
|
|
return form.process_request(
|
|
request,
|
|
msg_success=_("Revocation added")
|
|
)
|
|
|
|
|
|
@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 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,
|
|
"log": intervention.log.all(),
|
|
"modal_title": _("Log"),
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
@default_group_required
|
|
def new_deduction_view(request: HttpRequest, id: str):
|
|
""" Renders a modal form view for creating deductions
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The intervention's id which shall benefit from this deduction
|
|
|
|
Returns:
|
|
|
|
"""
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
form = NewDeductionModalForm(request.POST or None, instance=intervention, user=request.user)
|
|
return form.process_request(
|
|
request,
|
|
msg_success=_("Deduction added")
|
|
)
|
|
|
|
|
|
@login_required
|
|
@conservation_office_group_required
|
|
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)
|
|
form = RecordModalForm(request.POST or None, instance=intervention, user=request.user)
|
|
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:")
|
|
)
|
|
|
|
|
|
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)
|
|
|
|
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
|
if not intervention.recorded:
|
|
template = "report/unavailable.html"
|
|
return render(request, template, {})
|
|
|
|
# Prepare data for map viewer
|
|
geom_form = SimpleGeomForm(
|
|
instance=intervention
|
|
)
|
|
|
|
distinct_deductions = intervention.deductions.all().distinct(
|
|
"account"
|
|
)
|
|
qrcode_img = generate_qr_code(
|
|
request.build_absolute_uri(reverse("intervention:report", args=(id,))),
|
|
10
|
|
)
|
|
qrcode_img_lanis = generate_qr_code(
|
|
intervention.get_LANIS_link(),
|
|
7
|
|
)
|
|
context = {
|
|
"obj": intervention,
|
|
"deductions": distinct_deductions,
|
|
"qrcode": qrcode_img,
|
|
"qrcode_lanis": qrcode_img_lanis,
|
|
"geom_form": geom_form,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|