#26 Annual conservation report
* enhancements for report2excel functionality * improvements for report2html layout +
This commit is contained in:
@@ -47,28 +47,30 @@ class TempExcelFile:
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ws = self._workbook.active
|
||||
# Always activate sheet protection
|
||||
ws.protection.sheet = True
|
||||
ws.protection.enable()
|
||||
_rows = ws.iter_rows(start_row)
|
||||
for row in _rows:
|
||||
for cell in row:
|
||||
val = cell.value
|
||||
if val in self._template_map:
|
||||
attr = self._template_map[val]
|
||||
# If keyword '_iter' can be found inside the placeholder value it's an iterable and we
|
||||
# need to process it differently
|
||||
if isinstance(attr, dict):
|
||||
# Read the iterable object and related attributes from the dict
|
||||
_iter_obj = attr.get("iterable", None)
|
||||
_attrs = attr.get("attrs", [])
|
||||
self._add_cells_from_iterable(ws, cell, _iter_obj, _attrs)
|
||||
# Since the sheet length did change now, we need to rerun this function starting with the new
|
||||
# row counter
|
||||
self._replace_template_placeholders(start_row=cell.row + len(_iter_obj))
|
||||
else:
|
||||
cell.value = attr
|
||||
sheets = self._workbook.worksheets
|
||||
for sheet in sheets:
|
||||
ws = sheet
|
||||
# Always activate sheet protection
|
||||
ws.protection.sheet = True
|
||||
ws.protection.enable()
|
||||
_rows = ws.iter_rows(start_row)
|
||||
for row in _rows:
|
||||
for cell in row:
|
||||
val = cell.value
|
||||
if val in self._template_map:
|
||||
attr = self._template_map[val]
|
||||
# If keyword '_iter' can be found inside the placeholder value it's an iterable and we
|
||||
# need to process it differently
|
||||
if isinstance(attr, dict):
|
||||
# Read the iterable object and related attributes from the dict
|
||||
_iter_obj = attr.get("iterable", None)
|
||||
_attrs = attr.get("attrs", [])
|
||||
self._add_cells_from_iterable(ws, cell, _iter_obj, _attrs)
|
||||
# Since the sheet length did change now, we need to rerun this function starting with the new
|
||||
# row counter
|
||||
self._replace_template_placeholders(start_row=cell.row + len(_iter_obj))
|
||||
else:
|
||||
cell.value = attr
|
||||
self._workbook.save(self._file.name)
|
||||
self._file.seek(0)
|
||||
self.stream = self._file.read()
|
||||
|
||||
Binary file not shown.
@@ -379,14 +379,10 @@ class TimespanReport:
|
||||
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()
|
||||
@@ -401,23 +397,15 @@ class TimespanReport:
|
||||
|
||||
def __init__(self, id: str, date_from: str, date_to: str):
|
||||
# First fetch all eco account for this office
|
||||
self.queryset_total = EcoAccount.objects.filter(
|
||||
self.queryset = EcoAccount.objects.filter(
|
||||
responsible__conservation_office__id=id,
|
||||
deleted=None,
|
||||
created__timestamp__gte=date_from,
|
||||
created__timestamp__lte=date_to,
|
||||
)
|
||||
self.queryset_recorded = self.queryset_total.filter(
|
||||
self.queryset_recorded = self.queryset.filter(
|
||||
recorded__isnull=False
|
||||
)
|
||||
# Then fetch the old ones (pre-LKompVzVo)
|
||||
self.queryset_old = self.queryset_total.filter(
|
||||
recorded__timestamp__lte=LKOMPVZVO_PUBLISH_DATE,
|
||||
)
|
||||
# Then fetch the default queryset with the new ones (post-LKompVzVo)
|
||||
self.queryset = self.queryset_total.filter(
|
||||
recorded__timestamp__gte=LKOMPVZVO_PUBLISH_DATE,
|
||||
)
|
||||
# Fetch all related deductions
|
||||
self.queryset_deductions = EcoAccountDeduction.objects.filter(
|
||||
account__id__in=self.queryset.values_list("id")
|
||||
@@ -427,15 +415,29 @@ class TimespanReport:
|
||||
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_recorded_count = 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()
|
||||
self._define_excel_map()
|
||||
|
||||
def _define_excel_map(self):
|
||||
""" Define the excel map, which holds values for each placeholder used in the template
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.excel_map = {
|
||||
"acc_total": self.queryset_count,
|
||||
"acc_recorded": self.queryset_recorded_count,
|
||||
"acc_deduc_recorded": self.queryset_deductions_recorded_count,
|
||||
"acc_deduc_surface_recorded": self.recorded_deductions_sq_m,
|
||||
"acc_deduc_total": self.queryset_deductions_count,
|
||||
"acc_deduc_surface_total": self.deductions_sq_m,
|
||||
}
|
||||
|
||||
def _create_report(self):
|
||||
""" Creates all report information
|
||||
@@ -453,32 +455,76 @@ class TimespanReport:
|
||||
sum=Sum("surface")
|
||||
)["sum"] or 0
|
||||
|
||||
class OldInterventionReport:
|
||||
queryset = Compensation.objects.none()
|
||||
queryset_checked = Compensation.objects.none()
|
||||
queryset_recorded = Compensation.objects.none()
|
||||
class OldDataReport:
|
||||
"""
|
||||
Evaluates 'old data' (registered (zugelassen) before 16.06.2018)
|
||||
"""
|
||||
queryset_intervention = Intervention.objects.none()
|
||||
queryset_intervention_recorded = Intervention.objects.none()
|
||||
queryset_intervention_count = -1
|
||||
queryset_intervention_recorded_count = -1
|
||||
|
||||
queryset_count = -1
|
||||
queryset_checked_count = -1
|
||||
queryset_recorded_count = -1
|
||||
queryset_comps = Compensation.objects.none()
|
||||
queryset_comps_recorded = Compensation.objects.none()
|
||||
queryset_comps_count = -1
|
||||
queryset_comps_recorded_count = -1
|
||||
|
||||
queryset_acc = EcoAccount.objects.none()
|
||||
queryset_acc_recorded = EcoAccount.objects.none()
|
||||
queryset_acc_count = -1
|
||||
queryset_acc_recorded_count = -1
|
||||
|
||||
def __init__(self, id: str, date_from: str, date_to: str):
|
||||
self.queryset = Intervention.objects.filter(
|
||||
self.queryset_intervention = Intervention.objects.filter(
|
||||
legal__registration_date__lte=LKOMPVZVO_PUBLISH_DATE,
|
||||
responsible__conservation_office__id=id,
|
||||
deleted=None,
|
||||
created__timestamp__gte=date_from,
|
||||
created__timestamp__lte=date_to,
|
||||
)
|
||||
self.queryset_checked = self.queryset.filter(
|
||||
checked__isnull=False
|
||||
)
|
||||
self.queryset_recorded = self.queryset.filter(
|
||||
self.queryset_intervention_recorded = self.queryset_intervention.filter(
|
||||
recorded__isnull=False
|
||||
)
|
||||
self.queryset_count = self.queryset.count()
|
||||
self.queryset_checked = self.queryset_checked.count()
|
||||
self.queryset_recorded = self.queryset_recorded.count()
|
||||
self.queryset_intervention_count = self.queryset_intervention.count()
|
||||
self.queryset_intervention_recorded_count = self.queryset_intervention_recorded.count()
|
||||
|
||||
self.queryset_comps = Compensation.objects.filter(
|
||||
intervention__in=self.queryset_intervention
|
||||
)
|
||||
self.queryset_comps_recorded = Compensation.objects.filter(
|
||||
intervention__in=self.queryset_intervention_recorded,
|
||||
)
|
||||
self.queryset_comps_count = self.queryset_comps.count()
|
||||
self.queryset_comps_recorded_count = self.queryset_comps_recorded.count()
|
||||
|
||||
self.queryset_acc = EcoAccount.objects.filter(
|
||||
#legal__registration_date__lte=LKOMPVZVO_PUBLISH_DATE,
|
||||
responsible__conservation_office__id=id,
|
||||
deleted=None,
|
||||
created__timestamp__gte=date_from,
|
||||
created__timestamp__lte=date_to,
|
||||
)
|
||||
self.queryset_acc_recorded = self.queryset_acc.filter(
|
||||
recorded__isnull=False,
|
||||
)
|
||||
self.queryset_acc_count = self.queryset_acc.count()
|
||||
self.queryset_acc_recorded_count = self.queryset_acc_recorded.count()
|
||||
self._define_excel_map()
|
||||
|
||||
def _define_excel_map(self):
|
||||
""" Define the excel map, which holds values for each placeholder used in the template
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.excel_map = {
|
||||
"old_i_recorded": self.queryset_intervention_recorded_count,
|
||||
"old_i_total": self.queryset_intervention_count,
|
||||
"old_c_recorded": self.queryset_comps_recorded_count,
|
||||
"old_c_total": self.queryset_comps_count,
|
||||
"old_ea_recorded": self.queryset_acc_recorded_count,
|
||||
"old_ea_total": self.queryset_acc_count,
|
||||
}
|
||||
|
||||
def __init__(self, office_id: str, date_from: str, date_to: str):
|
||||
self.office_id = office_id
|
||||
@@ -488,7 +534,7 @@ class TimespanReport:
|
||||
self.intervention_report = self.InterventionReport(self.office_id, date_from, date_to)
|
||||
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)
|
||||
self.old_data_report = self.OldDataReport(self.office_id, date_from, date_to)
|
||||
|
||||
# Build excel map
|
||||
self.excel_map = {
|
||||
@@ -497,3 +543,5 @@ class TimespanReport:
|
||||
}
|
||||
self.excel_map.update(self.intervention_report.excel_map)
|
||||
self.excel_map.update(self.compensation_report.excel_map)
|
||||
self.excel_map.update(self.eco_account_report.excel_map)
|
||||
self.excel_map.update(self.old_data_report.excel_map)
|
||||
|
||||
Reference in New Issue
Block a user