#26 Annual conservation report

* adds first try for excel file downloading (WIP)
This commit is contained in:
mpeltriaux 2021-10-20 15:25:08 +02:00
parent e22e390040
commit 419a48cff1
4 changed files with 69 additions and 24 deletions

View File

@ -3,10 +3,28 @@
{% block body %}
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="col-sm-12 col-md-12 col-lg-6">
<h3>{% trans 'Evaluation report' %} {{office.long_name}}</h3>
<h5>{% trans 'From' %} {{report.date_from.date}} {% trans 'to' %} {{report.date_to.date}}</h5>
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="d-flex justify-content-end">
<div class=" menu-elem dropdown">
<div class="btn btn" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<button class="btn btn-default" title="{% trans 'Download' %}">
{% fa5_icon 'download' %}
</button>
</div>
<div class="dropdown-menu">
<a href="{{request.url}}?format=excel&{{request.GET.urlencode}}">
<button class="dropdown-item" title="Excel">
{% fa5_icon 'file-excel' %} Excel
</button>
</a>
</div>
</div>
</div>
</div>
</div>
<hr>
<div class="col-sm-12 col-md-12 col-lg-12">

View File

@ -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

View File

@ -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)
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

View File

@ -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