diff --git a/analysis/templates/analysis/reports/detail.html b/analysis/templates/analysis/reports/detail.html
index 1ba78d54..7a5aee0a 100644
--- a/analysis/templates/analysis/reports/detail.html
+++ b/analysis/templates/analysis/reports/detail.html
@@ -3,10 +3,28 @@
{% block body %}
-
+
{% trans 'Evaluation report' %} {{office.long_name}}
{% trans 'From' %} {{report.date_from.date}} {% trans 'to' %} {{report.date_to.date}}
+
diff --git a/analysis/utils/report.py b/analysis/utils/report.py
index c9c7a1bd..8d945f6d 100644
--- a/analysis/utils/report.py
+++ b/analysis/utils/report.py
@@ -5,10 +5,13 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 18.10.21
"""
+from tempfile import NamedTemporaryFile
+
from django.contrib.gis.db.models import MultiPolygonField
from django.contrib.gis.db.models.functions import NumGeometries
from django.db.models import Count, Sum, Q
from django.db.models.functions import Cast
+from openpyxl import Workbook
from analysis.settings import LKOMPVZVO_PUBLISH_DATE
from codelist.models import KonovaCode
@@ -16,6 +19,7 @@ from codelist.settings import CODELIST_LAW_ID
from compensation.models import Compensation, Payment, EcoAccountDeduction, EcoAccount
from intervention.models import Intervention
from konova.models import Geometry
+from konova.utils.generators import generate_random_string
class TimespanReport:
@@ -363,3 +367,12 @@ class TimespanReport:
self.compensation_report = self.CompensationReport(self.office_id, date_from, date_to)
self.eco_account_report = self.EcoAccountReport(self.office_id, date_from, date_to)
self.old_intervention_report = self.OldInterventionReport(self.office_id, date_from, date_to)
+
+ def to_excel_file(self):
+ workbook = Workbook()
+ tmp_file = NamedTemporaryFile()
+ ws = workbook.active
+ ws["A1"] = "TEST"
+ workbook.save(tmp_file.name)
+ tmp_file.seek(0)
+ return tmp_file
diff --git a/analysis/views.py b/analysis/views.py
index dab3b276..fcefa15f 100644
--- a/analysis/views.py
+++ b/analysis/views.py
@@ -1,8 +1,7 @@
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.http import HttpRequest, FileResponse
+from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone
from analysis.forms import TimespanReportForm
@@ -55,18 +54,17 @@ def detail_report_view(request: HttpRequest, id: str):
Returns:
"""
+ # Try to resolve the requested office id
+ cons_office = get_object_or_404(
+ KonovaCode,
+ id=id
+ )
+ # Try to resolve the date parameters into Date objects -> redirect if this fails
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)))
+ df = request.GET.get("df", None)
+ dt = request.GET.get("dt", None)
+ date_from = timezone.make_aware(timezone.datetime.fromisoformat(df))
+ date_to = timezone.make_aware(timezone.datetime.fromisoformat(dt))
except ValueError:
messages.error(
request,
@@ -75,11 +73,24 @@ def detail_report_view(request: HttpRequest, id: str):
)
return redirect("analysis:reports")
+ # Check whether the html default rendering is requested or an alternative
+ format_param = request.GET.get("format", "html")
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)
\ No newline at end of file
+
+ if format_param == "html":
+ template = "analysis/reports/detail.html"
+ context = {
+ "office": cons_office,
+ "report": report,
+ }
+ context = BaseContext(request, context).context
+ return render(request, template, context)
+ elif format_param == "excel":
+ stream = report.to_excel_file()
+ response = FileResponse(
+ filename=stream.name,
+ )
+ response['Content-Disposition'] = f'attachement;"'
+ return response
+ else:
+ raise NotImplementedError
diff --git a/requirements.txt b/requirements.txt
index 360af590..ac893917 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -11,11 +11,14 @@ django-filter==2.4.0
django-fontawesome-5==1.0.18
django-simple-sso==1.1.0
django-tables2==2.3.4
+et-xmlfile==1.1.0
idna==2.10
importlib-metadata==2.1.1
-itsdangerous
-psycopg2-binary
+itsdangerous==0.24
+openpyxl==3.0.9
+psycopg2-binary==2.9.1
pytz==2020.4
+qrcode==7.3.1
requests==2.25.0
six==1.15.0
soupsieve==2.2.1