1+ from collections import defaultdict
12from django .db .models import Count , Sum
23from conferences .models .conference import Conference
34from helpers .constants import GENDERS
@@ -126,18 +127,19 @@ def _aggregate_data_by_country_type(self, filtered_grants, statuses):
126127 country_type_data = filtered_grants .values ("country_type" , "status" ).annotate (
127128 total = Count ("id" )
128129 )
129- country_type_summary = {
130- country_type : {status [0 ]: 0 for status in statuses }
131- for country_type in Grant .CountryType .values
132- }
130+ country_type_summary = defaultdict (
131+ lambda : {status [0 ]: 0 for status in statuses }
132+ )
133133
134134 for data in country_type_data :
135135 country_type = data ["country_type" ]
136136 status = data ["status" ]
137137 total = data ["total" ]
138138 country_type_summary [country_type ][status ] += total
139139
140- return country_type_summary
140+ print ("country_type_summary" )
141+ print (country_type_summary )
142+ return dict (country_type_summary )
141143
142144 def _aggregate_data_by_gender (self , filtered_grants , statuses ):
143145 """
@@ -146,20 +148,14 @@ def _aggregate_data_by_gender(self, filtered_grants, statuses):
146148 gender_data = filtered_grants .values ("gender" , "status" ).annotate (
147149 total = Count ("id" )
148150 )
149- gender_summary = {
150- gender : {status [0 ]: 0 for status in statuses } for gender , _ in GENDERS
151- }
152- gender_summary ["" ] = {
153- status [0 ]: 0 for status in statuses
154- } # For unspecified genders
155-
151+ gender_summary = defaultdict (lambda : {status [0 ]: 0 for status in statuses })
156152 for data in gender_data :
157153 gender = data ["gender" ] if data ["gender" ] else ""
158154 status = data ["status" ]
159155 total = data ["total" ]
160156 gender_summary [gender ][status ] += total
161157
162- return gender_summary
158+ return dict ( gender_summary )
163159
164160 def _aggregate_financial_data_by_status (self , filtered_grants , statuses ):
165161 """
@@ -168,7 +164,6 @@ def _aggregate_financial_data_by_status(self, filtered_grants, statuses):
168164 financial_data = filtered_grants .values ("status" ).annotate (
169165 total_amount_sum = Sum ("total_amount" )
170166 )
171- print (financial_data )
172167 financial_summary = {status [0 ]: 0 for status in statuses }
173168 overall_total = 0
174169
@@ -188,10 +183,7 @@ def _aggregate_data_by_grant_type(self, filtered_grants, statuses):
188183 grant_type_data = filtered_grants .values ("grant_type" , "status" ).annotate (
189184 total = Count ("id" )
190185 )
191- grant_type_summary = {
192- grant_type : {status [0 ]: 0 for status in statuses }
193- for grant_type in Grant .GrantType .values
194- }
186+ grant_type_summary = defaultdict (lambda : {status [0 ]: 0 for status in statuses })
195187
196188 for data in grant_type_data :
197189 grant_types = data ["grant_type" ]
@@ -200,7 +192,7 @@ def _aggregate_data_by_grant_type(self, filtered_grants, statuses):
200192 for grant_type in grant_types :
201193 grant_type_summary [grant_type ][status ] += total
202194
203- return grant_type_summary
195+ return dict ( grant_type_summary )
204196
205197 def _aggregate_data_by_speaker_status (self , filtered_grants , statuses ):
206198 """
@@ -233,10 +225,9 @@ def _aggregate_data_by_speaker_status(self, filtered_grants, statuses):
233225 .annotate (total = Count ("id" ))
234226 )
235227
236- speaker_status_summary = {
237- "proposed_speaker" : {status [0 ]: 0 for status in statuses },
238- "confirmed_speaker" : {status [0 ]: 0 for status in statuses },
239- }
228+ speaker_status_summary = defaultdict (
229+ lambda : {status [0 ]: 0 for status in statuses }
230+ )
240231
241232 for data in proposed_speaker_data :
242233 status = data ["status" ]
@@ -248,7 +239,7 @@ def _aggregate_data_by_speaker_status(self, filtered_grants, statuses):
248239 total = data ["total" ]
249240 speaker_status_summary ["confirmed_speaker" ][status ] += total
250241
251- return speaker_status_summary
242+ return dict ( speaker_status_summary )
252243
253244 def _aggregate_data_by_approved_type (self , filtered_grants , statuses ):
254245 """
@@ -257,21 +248,17 @@ def _aggregate_data_by_approved_type(self, filtered_grants, statuses):
257248 approved_type_data = filtered_grants .values ("approved_type" , "status" ).annotate (
258249 total = Count ("id" )
259250 )
260- approved_type_summary = {
261- approved_type : {status [0 ]: 0 for status in statuses }
262- for approved_type in Grant .ApprovedType .values
263- }
264- approved_type_summary [None ] = {
265- status [0 ]: 0 for status in statuses
266- } # For unspecified genders
251+ approved_type_summary = defaultdict (
252+ lambda : {status [0 ]: 0 for status in statuses }
253+ )
267254
268255 for data in approved_type_data :
269256 approved_type = data ["approved_type" ]
270257 status = data ["status" ]
271258 total = data ["total" ]
272259 approved_type_summary [approved_type ][status ] += total
273260
274- return approved_type_summary
261+ return dict ( approved_type_summary )
275262
276263 def _aggregate_data_by_requested_needs_summary (self , filtered_grants , statuses ):
277264 """
@@ -303,15 +290,12 @@ def _aggregate_data_by_occupation(self, filtered_grants, statuses):
303290 occupation_data = filtered_grants .values ("occupation" , "status" ).annotate (
304291 total = Count ("id" )
305292 )
306- occupation_summary = {
307- occupation : {status [0 ]: 0 for status in statuses }
308- for occupation in Grant .Occupation .values
309- }
293+ occupation_summary = defaultdict (lambda : {status [0 ]: 0 for status in statuses })
310294
311295 for data in occupation_data :
312296 occupation = data ["occupation" ]
313297 status = data ["status" ]
314298 total = data ["total" ]
315299 occupation_summary [occupation ][status ] += total
316300
317- return occupation_summary
301+ return dict ( occupation_summary )
0 commit comments