Skip to content

Commit fdfe60d

Browse files
authored
Use defaultdict to prevent KeyError (#4319)
1 parent 35398d9 commit fdfe60d

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

backend/grants/summary.py

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import defaultdict
12
from django.db.models import Count, Sum
23
from conferences.models.conference import Conference
34
from helpers.constants import GENDERS
@@ -126,18 +127,17 @@ 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+
return dict(country_type_summary)
141141

142142
def _aggregate_data_by_gender(self, filtered_grants, statuses):
143143
"""
@@ -146,20 +146,14 @@ def _aggregate_data_by_gender(self, filtered_grants, statuses):
146146
gender_data = filtered_grants.values("gender", "status").annotate(
147147
total=Count("id")
148148
)
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-
149+
gender_summary = defaultdict(lambda: {status[0]: 0 for status in statuses})
156150
for data in gender_data:
157151
gender = data["gender"] if data["gender"] else ""
158152
status = data["status"]
159153
total = data["total"]
160154
gender_summary[gender][status] += total
161155

162-
return gender_summary
156+
return dict(gender_summary)
163157

164158
def _aggregate_financial_data_by_status(self, filtered_grants, statuses):
165159
"""
@@ -168,7 +162,6 @@ def _aggregate_financial_data_by_status(self, filtered_grants, statuses):
168162
financial_data = filtered_grants.values("status").annotate(
169163
total_amount_sum=Sum("total_amount")
170164
)
171-
print(financial_data)
172165
financial_summary = {status[0]: 0 for status in statuses}
173166
overall_total = 0
174167

@@ -188,10 +181,7 @@ def _aggregate_data_by_grant_type(self, filtered_grants, statuses):
188181
grant_type_data = filtered_grants.values("grant_type", "status").annotate(
189182
total=Count("id")
190183
)
191-
grant_type_summary = {
192-
grant_type: {status[0]: 0 for status in statuses}
193-
for grant_type in Grant.GrantType.values
194-
}
184+
grant_type_summary = defaultdict(lambda: {status[0]: 0 for status in statuses})
195185

196186
for data in grant_type_data:
197187
grant_types = data["grant_type"]
@@ -200,7 +190,7 @@ def _aggregate_data_by_grant_type(self, filtered_grants, statuses):
200190
for grant_type in grant_types:
201191
grant_type_summary[grant_type][status] += total
202192

203-
return grant_type_summary
193+
return dict(grant_type_summary)
204194

205195
def _aggregate_data_by_speaker_status(self, filtered_grants, statuses):
206196
"""
@@ -233,10 +223,9 @@ def _aggregate_data_by_speaker_status(self, filtered_grants, statuses):
233223
.annotate(total=Count("id"))
234224
)
235225

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-
}
226+
speaker_status_summary = defaultdict(
227+
lambda: {status[0]: 0 for status in statuses}
228+
)
240229

241230
for data in proposed_speaker_data:
242231
status = data["status"]
@@ -248,7 +237,7 @@ def _aggregate_data_by_speaker_status(self, filtered_grants, statuses):
248237
total = data["total"]
249238
speaker_status_summary["confirmed_speaker"][status] += total
250239

251-
return speaker_status_summary
240+
return dict(speaker_status_summary)
252241

253242
def _aggregate_data_by_approved_type(self, filtered_grants, statuses):
254243
"""
@@ -257,21 +246,17 @@ def _aggregate_data_by_approved_type(self, filtered_grants, statuses):
257246
approved_type_data = filtered_grants.values("approved_type", "status").annotate(
258247
total=Count("id")
259248
)
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
249+
approved_type_summary = defaultdict(
250+
lambda: {status[0]: 0 for status in statuses}
251+
)
267252

268253
for data in approved_type_data:
269254
approved_type = data["approved_type"]
270255
status = data["status"]
271256
total = data["total"]
272257
approved_type_summary[approved_type][status] += total
273258

274-
return approved_type_summary
259+
return dict(approved_type_summary)
275260

276261
def _aggregate_data_by_requested_needs_summary(self, filtered_grants, statuses):
277262
"""
@@ -303,15 +288,12 @@ def _aggregate_data_by_occupation(self, filtered_grants, statuses):
303288
occupation_data = filtered_grants.values("occupation", "status").annotate(
304289
total=Count("id")
305290
)
306-
occupation_summary = {
307-
occupation: {status[0]: 0 for status in statuses}
308-
for occupation in Grant.Occupation.values
309-
}
291+
occupation_summary = defaultdict(lambda: {status[0]: 0 for status in statuses})
310292

311293
for data in occupation_data:
312294
occupation = data["occupation"]
313295
status = data["status"]
314296
total = data["total"]
315297
occupation_summary[occupation][status] += total
316298

317-
return occupation_summary
299+
return dict(occupation_summary)

0 commit comments

Comments
 (0)