Skip to content

Commit 0f329ca

Browse files
committed
Appearance split for returning members
1 parent 553cb1e commit 0f329ca

File tree

1 file changed

+42
-51
lines changed

1 file changed

+42
-51
lines changed

pombola/south_africa/views/person.py

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import requests
88
import urllib
99
import datetime
10+
import pytz
1011

1112
from .constants import API_REQUESTS_TIMEOUT
1213
from urlparse import urlsplit
@@ -244,6 +245,7 @@ def get_attendance_stats_raw(self, data):
244245
return {}
245246

246247
attendance_by_year = {}
248+
split_date = datetime.datetime(2024, 6, 1, tzinfo=pytz.utc) # Ensure split_date is timezone-aware
247249

248250
years = sorted(set(dateutil.parser.parse(x['meeting']['date']).year for x in data), reverse=True)
249251
minister_positions_by_year = self.get_active_minister_positions(years)
@@ -253,28 +255,41 @@ def get_attendance_stats_raw(self, data):
253255
# Don't count alternate member attendances
254256
continue
255257
attendance = x['attendance']
256-
meeting_date = dateutil.parser.parse(x['meeting']['date'])
258+
meeting_date = dateutil.parser.parse(x['meeting']['date']).astimezone(pytz.utc) # Ensure meeting_date is timezone-aware
257259
year = meeting_date.year
258260

259-
minister_at_date = self.active_position_at_date(minister_positions_by_year[year], meeting_date)
261+
if year == 2024:
262+
split_key = 'before' if meeting_date < split_date else 'after'
263+
split_year = "{}_{}".format(year, split_key)
264+
else:
265+
split_year = str(year)
266+
267+
base_year = year if year != 2024 else 2024 # Use 2024 as the base year for splitting
268+
minister_at_date = self.active_position_at_date(minister_positions_by_year[base_year], meeting_date)
260269
position = 'minister' if minister_at_date else 'mp'
261270

262-
year_dict = attendance_by_year.setdefault(year, {})
271+
year_dict = attendance_by_year.setdefault(split_year, {})
263272
position_dict = year_dict.setdefault(position, {})
264273
position_dict.setdefault(attendance, 0)
265274
position_dict[attendance] += 1
266275

267276
# Add zero minister attendance if person was active minister during a year,
268-
# but no reocrds was returned for that year.
269-
for year, positions in minister_positions_by_year.iteritems():
277+
# but no records were returned for that year.
278+
for year, positions in minister_positions_by_year.items():
270279
if positions:
271280
# There were active minister positions
272-
if 'minister' not in attendance_by_year[year].keys():
281+
if year == 2024:
282+
for split_key in ['before', 'after']:
283+
split_year = "{}_{}".format(year, split_key)
284+
if 'minister' not in attendance_by_year.get(split_year, {}).keys():
285+
# No minister attendance recorded
286+
attendance_by_year.setdefault(split_year, {})['minister'] = {'P': 0}
287+
elif 'minister' not in attendance_by_year.get(str(year), {}).keys():
273288
# No minister attendance recorded
274-
attendance_by_year[year]['minister'] = {'P': 0}
289+
attendance_by_year[str(year)]['minister'] = {'P': 0}
275290

276291
return attendance_by_year
277-
292+
278293
def get_meetings_attended(self, data, limit=None):
279294
api_url_re = r'/committee-meeting/(\d+)/'
280295
meeting_url_template = 'https://pmg.org.za/committee-meeting/{}/'
@@ -322,53 +337,29 @@ def get_position_for_year(self, year):
322337
def get_attendance_stats(self, attendance_by_year):
323338
sorted_keys = sorted(attendance_by_year.keys(), reverse=True)
324339
return_data = []
325-
split_date = "2024-06-01"
326340

327341
for year in sorted_keys:
328342
year_dict = attendance_by_year[year]
329-
if year == 2024:
330-
before_split = {}
331-
after_split = {}
332-
for position in year_dict.keys():
333-
before_split[position] = {k: v for k, v in year_dict[position].items() if k < split_date}
334-
after_split[position] = {k: v for k, v in year_dict[position].items() if k >= split_date}
343+
344+
for position in sorted(year_dict.keys()):
345+
attendance = sum((year_dict[position][x] for x in year_dict[position] if x in self.present_values))
346+
meeting_count = sum((year_dict[position][x] for x in year_dict[position]))
347+
if meeting_count == 0:
348+
percentage = 0
349+
else:
350+
percentage = 100 * attendance / meeting_count
351+
352+
year_label = year.replace('_before', ' (6th Parliament)').replace('_after', ' (7th Parliament)')
335353

336-
for split_period, data in [("before", before_split), ("after", after_split)]:
337-
for position in sorted(data.keys()):
338-
attendance = sum((data[position][x] for x in data[position] if x in self.present_values))
339-
meeting_count = sum((data[position][x] for x in data[position]))
340-
if meeting_count == 0:
341-
percentage = 0
342-
else:
343-
percentage = 100 * attendance / meeting_count
344-
345-
return_data.append(
346-
{
347-
'year': year,
348-
'attended': attendance,
349-
'total': meeting_count,
350-
'percentage': percentage,
351-
'position': position if position == 'mp' else 'minister/deputy',
352-
}
353-
)
354-
else:
355-
for position in sorted(year_dict.keys()):
356-
attendance = sum((year_dict[position][x] for x in year_dict[position] if x in self.present_values))
357-
meeting_count = sum((year_dict[position][x] for x in year_dict[position]))
358-
if meeting_count == 0:
359-
percentage = 0
360-
else:
361-
percentage = 100 * attendance / meeting_count
362-
363-
return_data.append(
364-
{
365-
'year': year,
366-
'attended': attendance,
367-
'total': meeting_count,
368-
'percentage': percentage,
369-
'position': position if position == 'mp' else 'minister/deputy',
370-
}
371-
)
354+
return_data.append(
355+
{
356+
'year': year_label,
357+
'attended': attendance,
358+
'total': meeting_count,
359+
'percentage': percentage,
360+
'position': position if position == 'mp' else 'minister/deputy',
361+
}
362+
)
372363

373364
return return_data
374365

0 commit comments

Comments
 (0)