#26 Annual conservation report

* Adds download as excel sheet
* improves db access performance
This commit is contained in:
2021-10-21 14:58:54 +02:00
parent 8872155e61
commit ed01984987
10 changed files with 238 additions and 43 deletions

View File

@@ -5,13 +5,10 @@ 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
@@ -19,7 +16,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
from konova.sub_settings.django_settings import BASE_DIR, DEFAULT_DATE_FORMAT
class TimespanReport:
@@ -30,11 +27,19 @@ class TimespanReport:
date_from = -1
date_to = -1
# Excel map is used to map a cell value ("A1") to an attribute
excel_map = {}
excel_template_path = f"{BASE_DIR}/analysis/utils/excel/excel_report.xlsx"
class InterventionReport:
queryset = Intervention.objects.none()
queryset_checked = Intervention.objects.none()
queryset_recorded = Intervention.objects.none()
queryset_count = -1
queryset_checked_count = -1
queryset_recorded_count = -1
# Law related
law_sum = -1
law_sum_checked = -1
@@ -66,6 +71,10 @@ class TimespanReport:
self.queryset_recorded = self.queryset.filter(
recorded__isnull=False
)
self.queryset_count = self.queryset.count()
self.queryset_checked_count = self.queryset_checked.count()
self.queryset_recorded_count = self.queryset_recorded.count()
self._create_report()
def _create_report(self):
@@ -143,20 +152,32 @@ class TimespanReport:
queryset = Compensation.objects.none()
queryset_checked = Compensation.objects.none()
queryset_recorded = Compensation.objects.none()
queryset_count = -1
queryset_checked_count = -1
queryset_recorded_count = -1
queryset_registration_office_unb = Compensation.objects.none()
queryset_registration_office_unb_checked = Compensation.objects.none()
queryset_registration_office_unb_recorded = Compensation.objects.none()
queryset_registration_office_unb_count = -1
queryset_registration_office_unb_checked_count = -1
queryset_registration_office_unb_recorded_count = -1
num_single_surfaces_total_unb = -1
queryset_registration_office_tbp = Compensation.objects.none()
queryset_registration_office_tbp_checked = Compensation.objects.none()
queryset_registration_office_tbp_recorded = Compensation.objects.none()
queryset_registration_office_tbp = -1
queryset_registration_office_tbp_checked = -1
queryset_registration_office_tbp_recorded = -1
num_single_surfaces_total_tbp = -1
queryset_registration_office_other = Compensation.objects.none()
queryset_registration_office_other_checked = Compensation.objects.none()
queryset_registration_office_other_recorded = Compensation.objects.none()
queryset_registration_office_other = -1
queryset_registration_office_other_checked = -1
queryset_registration_office_other_recorded = -1
num_single_surfaces_total_other = -1
num_single_surfaces_total = -1
@@ -183,6 +204,11 @@ class TimespanReport:
self.queryset_recorded = self.queryset.filter(
intervention__recorded__isnull=False
)
self.queryset_count = self.queryset.count()
self.queryset_checked_count = self.queryset_checked.count()
self.queryset_recorded_count = self.queryset_recorded.count()
self._create_report()
def _create_report(self):
@@ -260,6 +286,9 @@ class TimespanReport:
self.queryset_registration_office_unb_checked = self.queryset_registration_office_unb.filter(
intervention__checked__isnull=False,
)
self.queryset_registration_office_unb_count = self.queryset_registration_office_unb.count()
self.queryset_registration_office_unb_checked_count = self.queryset_registration_office_unb_checked.count()
self.queryset_registration_office_unb_recorded_count = self.queryset_registration_office_unb_recorded.count()
self.queryset_registration_office_tbp = self.queryset.filter(
intervention__responsible__registration_office__parent__id=self.id_tbp
@@ -270,6 +299,9 @@ class TimespanReport:
self.queryset_registration_office_tbp_checked = self.queryset_registration_office_tbp.filter(
intervention__checked__isnull=False,
)
self.queryset_registration_office_tbp_count = self.queryset_registration_office_tbp.count()
self.queryset_registration_office_tbp_checked_count = self.queryset_registration_office_tbp_checked.count()
self.queryset_registration_office_tbp_recorded_count = self.queryset_registration_office_tbp_recorded.count()
self.queryset_registration_office_other = self.queryset.exclude(
Q(id__in=self.queryset_registration_office_tbp) | Q(id__in=self.queryset_registration_office_unb)
@@ -280,14 +312,26 @@ class TimespanReport:
self.queryset_registration_office_other_checked = self.queryset_registration_office_other.filter(
intervention__checked__isnull=False,
)
self.queryset_registration_office_other_count = self.queryset_registration_office_other.count()
self.queryset_registration_office_other_checked_count = self.queryset_registration_office_other_checked.count()
self.queryset_registration_office_other_recorded_count = self.queryset_registration_office_other_recorded.count()
class EcoAccountReport:
queryset_total = EcoAccount.objects.none()
queryset = EcoAccount.objects.none()
queryset_recorded = EcoAccount.objects.none()
queryset_old = EcoAccount.objects.none()
queryset_total_count = -1
queryset_count = -1
queryset_recorded_count = -1
queryset_old_count = -1
queryset_deductions = EcoAccountDeduction.objects.none()
queryset_deductions_recorded = EcoAccountDeduction.objects.none()
queryset_has_deductions = EcoAccountDeduction.objects.none()
queryset_deductions_count = -1
queryset_deductions_recorded_count = -1
queryset_has_deductions_count = -1
# Total size of deductions
deductions_sq_m = -1
@@ -320,6 +364,15 @@ class TimespanReport:
self.queryset_deductions_recorded = self.queryset_deductions.filter(
intervention__recorded__isnull=False
)
self.queryset_total_count = self.queryset_total.count()
self.queryset_count = self.queryset.count()
self.queryset_old_count = self.queryset_old.count()
self.queryset_recorded = self.queryset_recorded.count()
self.queryset_deductions_count = self.queryset_deductions.count()
self.queryset_deductions_recorded_count = self.queryset_deductions_recorded.count()
self.queryset_has_deductions_count = self.queryset_has_deductions.count()
self._create_report()
def _create_report(self):
@@ -343,6 +396,10 @@ class TimespanReport:
queryset_checked = Compensation.objects.none()
queryset_recorded = Compensation.objects.none()
queryset_count = -1
queryset_checked_count = -1
queryset_recorded_count = -1
def __init__(self, id: str, date_from: str, date_to: str):
self.queryset = Intervention.objects.filter(
legal__registration_date__lte=LKOMPVZVO_PUBLISH_DATE,
@@ -357,6 +414,9 @@ class TimespanReport:
self.queryset_recorded = self.queryset.filter(
recorded__isnull=False
)
self.queryset_count = self.queryset.count()
self.queryset_checked = self.queryset_checked.count()
self.queryset_recorded = self.queryset_recorded.count()
def __init__(self, office_id: str, date_from: str, date_to: str):
self.office_id = office_id
@@ -368,11 +428,32 @@ class TimespanReport:
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
# Build excel map
self.excel_map = {
"date_from": date_from.strftime(DEFAULT_DATE_FORMAT),
"date_to": date_to.strftime(DEFAULT_DATE_FORMAT),
"i_checked": self.intervention_report.queryset_checked_count,
"i_recorded": self.intervention_report.queryset_recorded_count,
"i_total": self.intervention_report.queryset_count,
"i_compensations_checked": self.intervention_report.compensation_sum_checked,
"i_compensations_recorded": self.intervention_report.compensation_sum_recorded,
"i_compensations_total": self.intervention_report.compensation_sum,
"i_payments_recorded": self.intervention_report.payment_sum_recorded,
"i_payments_checked": self.intervention_report.payment_sum_checked,
"i_payments_total": self.intervention_report.payment_sum,
"i_deductions_recorded": self.intervention_report.deduction_sum_recorded,
"i_deductions_checked": self.intervention_report.deduction_sum_checked,
"i_deductions_total": self.intervention_report.deduction_sum,
"i_laws_iter": {
"iterable": self.intervention_report.evaluated_laws,
"attrs": [
"short_name",
"num_checked",
"num_recorded",
"num",
]
},
"i_laws_checked": self.intervention_report.law_sum_checked,
"i_laws_recorded": self.intervention_report.law_sum_recorded,
"i_laws_total": self.intervention_report.law_sum,
}