From f19ad5f639a0b798d6447952ee5bccf6315806b4 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Wed, 15 Jun 2022 17:44:43 +0200 Subject: [PATCH] # 175 Report law calculation bugfix * fixes bug where amount of used laws in intervention forms would not be calculated properly --- analysis/utils/report.py | 46 ++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/analysis/utils/report.py b/analysis/utils/report.py index 996f87b5..63a06b84 100644 --- a/analysis/utils/report.py +++ b/analysis/utils/report.py @@ -137,22 +137,36 @@ class TimespanReport: ).order_by( "long_name" ) - # Fetch all law ids which are used by any .legal object of an intervention object - intervention_laws_total = self.queryset.values_list("legal__laws__id") - intervention_laws_checked = self.queryset.filter(checked__isnull=False).values_list("legal__laws__id") - intervention_laws_recorded = self.queryset.filter(recorded__isnull=False).values_list( - "legal__laws__id") - # Count how often which law id appears in the above list, return only the long_name of the law and the resulting - # count (here 'num'). This is for keeping the db fetch as small as possible - # Compute the sum for total, checked and recorded - self.evaluated_laws = laws.annotate( - num=Count("id", filter=Q(id__in=intervention_laws_total)), - num_checked=Count("id", filter=Q(id__in=intervention_laws_checked)), - num_recorded=Count("id", filter=Q(id__in=intervention_laws_recorded)), - ).values_list("short_name", "long_name", "num_checked", "num_recorded", "num", named=True) - self.law_sum = self.evaluated_laws.aggregate(sum_num=Sum("num"))["sum_num"] - self.law_sum_checked = self.evaluated_laws.aggregate(sum_num_checked=Sum("num_checked"))["sum_num_checked"] - self.law_sum_recorded = self.evaluated_laws.aggregate(sum_num_recorded=Sum("num_recorded"))["sum_num_recorded"] + + evaluated_laws = [] + sum_num_checked = 0 + sum_num_recorded = 0 + sum_num = 0 + for law in laws: + num = self.queryset.filter( + legal__laws__atom_id=law.atom_id + ).count() + num_checked = self.queryset_checked.filter( + legal__laws__atom_id=law.atom_id + ).count() + num_recorded = self.queryset_recorded.filter( + legal__laws__atom_id=law.atom_id + ).count() + evaluated_laws.append({ + "short_name": law.short_name, + "long_name": law.long_name, + "num": num, + "num_checked": num_checked, + "num_recorded": num_recorded, + }) + sum_num += num + sum_num_checked += num_checked + sum_num_recorded += num_recorded + + self.evaluated_laws = evaluated_laws + self.law_sum = sum_num + self.law_sum_checked = sum_num_checked + self.law_sum_recorded = sum_num_recorded def _evaluate_compensations(self): """ Analyzes the types of compensation distribution