7
7
import requests
8
8
import urllib
9
9
import datetime
10
+ import pytz
10
11
11
12
from .constants import API_REQUESTS_TIMEOUT
12
13
from urlparse import urlsplit
@@ -244,6 +245,7 @@ def get_attendance_stats_raw(self, data):
244
245
return {}
245
246
246
247
attendance_by_year = {}
248
+ split_date = datetime .datetime (2024 , 6 , 1 , tzinfo = pytz .utc ) # Ensure split_date is timezone-aware
247
249
248
250
years = sorted (set (dateutil .parser .parse (x ['meeting' ]['date' ]).year for x in data ), reverse = True )
249
251
minister_positions_by_year = self .get_active_minister_positions (years )
@@ -253,28 +255,41 @@ def get_attendance_stats_raw(self, data):
253
255
# Don't count alternate member attendances
254
256
continue
255
257
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
257
259
year = meeting_date .year
258
260
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 )
260
269
position = 'minister' if minister_at_date else 'mp'
261
270
262
- year_dict = attendance_by_year .setdefault (year , {})
271
+ year_dict = attendance_by_year .setdefault (split_year , {})
263
272
position_dict = year_dict .setdefault (position , {})
264
273
position_dict .setdefault (attendance , 0 )
265
274
position_dict [attendance ] += 1
266
275
267
276
# 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 ():
270
279
if positions :
271
280
# 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 ():
273
288
# No minister attendance recorded
274
- attendance_by_year [year ]['minister' ] = {'P' : 0 }
289
+ attendance_by_year [str ( year ) ]['minister' ] = {'P' : 0 }
275
290
276
291
return attendance_by_year
277
-
292
+
278
293
def get_meetings_attended (self , data , limit = None ):
279
294
api_url_re = r'/committee-meeting/(\d+)/'
280
295
meeting_url_template = 'https://pmg.org.za/committee-meeting/{}/'
@@ -322,53 +337,29 @@ def get_position_for_year(self, year):
322
337
def get_attendance_stats (self , attendance_by_year ):
323
338
sorted_keys = sorted (attendance_by_year .keys (), reverse = True )
324
339
return_data = []
325
- split_date = "2024-06-01"
326
340
327
341
for year in sorted_keys :
328
342
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)' )
335
353
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
+ )
372
363
373
364
return return_data
374
365
0 commit comments