mpeltriaux
6b839b7f60
* adds index form for selecting timespan and office of interest * adds timespan support for TimespanReport * fixes naive datetime issues * fixes missing error message css tag * adds/updates translations
85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
from django.http import HttpRequest
|
|
from django.shortcuts import render, redirect
|
|
from django.utils import timezone
|
|
|
|
from analysis.forms import TimespanReportForm
|
|
from analysis.utils.report import TimespanReport
|
|
from codelist.models import KonovaCode
|
|
from konova.contexts import BaseContext
|
|
from konova.decorators import conservation_office_group_required
|
|
from konova.utils.message_templates import FORM_INVALID, PARAMS_INVALID
|
|
|
|
|
|
@login_required
|
|
@conservation_office_group_required
|
|
def index_reports_view(request: HttpRequest):
|
|
"""
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "analysis/reports/index.html"
|
|
form = TimespanReportForm(request.POST or None)
|
|
if request.method == "POST":
|
|
if form.is_valid():
|
|
redirect_url = form.save()
|
|
return redirect(redirect_url)
|
|
else:
|
|
messages.error(
|
|
request,
|
|
FORM_INVALID,
|
|
extra_tags="danger",
|
|
)
|
|
context = {
|
|
"form": form
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
@conservation_office_group_required
|
|
def detail_report_view(request: HttpRequest, id: str):
|
|
""" Renders the detailed report for a conservation office
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The conservation_office KonovaCode id
|
|
|
|
Returns:
|
|
|
|
"""
|
|
try:
|
|
cons_office = KonovaCode.objects.get(id=id)
|
|
except (ObjectDoesNotExist, ValueError) as e:
|
|
messages.error(
|
|
request,
|
|
PARAMS_INVALID,
|
|
extra_tags="danger",
|
|
)
|
|
return redirect("analysis:reports")
|
|
try:
|
|
date_from = timezone.make_aware(timezone.datetime.fromisoformat(request.GET.get("df", None)))
|
|
date_to = timezone.make_aware(timezone.datetime.fromisoformat(request.GET.get("dt", None)))
|
|
except ValueError:
|
|
messages.error(
|
|
request,
|
|
PARAMS_INVALID,
|
|
extra_tags="danger",
|
|
)
|
|
return redirect("analysis:reports")
|
|
|
|
report = TimespanReport(id, date_from, date_to)
|
|
template = "analysis/reports/detail.html"
|
|
context = {
|
|
"office": cons_office,
|
|
"report": report,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context) |