service.py

changeset 79
ba854da8c424
parent 77
83cd29dee853
child 85
62e753b7d3ff
equal deleted inserted replaced
78:ead971f9569c 79:ba854da8c424
436 stops_in_cluster = stops_in_cluster, 436 stops_in_cluster = stops_in_cluster,
437 amount_of_stops_in_cluster = len(stops_in_cluster), 437 amount_of_stops_in_cluster = len(stops_in_cluster),
438 tr = tr, 438 tr = tr,
439 ) 439 )
440 440
441
442 def hour_key(hour):
443 return (hour - 5) % 24
444
445 def day_class(weekday): 441 def day_class(weekday):
446 if weekday < 5: 442 if weekday < 5:
447 return 'working-day' 443 return 'working-day'
448 elif weekday == 5: 444 elif weekday == 5:
449 return 'saturday' 445 return 'saturday'
488 'route-splice': split_route_ref(route_ref), 484 'route-splice': split_route_ref(route_ref),
489 'trip': schedule_entry['stop'].trip.name, 485 'trip': schedule_entry['stop'].trip.name,
490 'night': is_night_time(schedule_entry['time']), 486 'night': is_night_time(schedule_entry['time']),
491 'minute': time.minute, 487 'minute': time.minute,
492 }) 488 })
493 week_model = [{'day': day, 'schedule': schedule, 'day-class': day_class(day.weekday())} for day, schedule in week_model.items()] 489 for day_offset in range(7):
490 from datetime import date, datetime, timedelta
491 day = date.today() + timedelta(day_offset)
492 try:
493 day_model = week_model[day]
494 except KeyError:
495 week_model[day] = {}
496 else:
497 def hour_key(x):
498 return (x - 5) % 24
499 # Fill in missing hours from 5am to last active hour
500 hours = set(day_model.keys()) | {5}
501 sorted_hours = sorted(hours, key = hour_key)
502 start_hour = sorted_hours[0]
503 end_hour = sorted_hours[-1] + 1
504 for hour in range(start_hour, end_hour):
505 hour_start = datetime(day.year, day.month, day.day, hour, 0)
506 if hour not in day_model and hour_start >= datetime.now():
507 day_model[hour] = []
508 # Sort the hours, so that 5am is first and 4am is last.
509 from collections import OrderedDict
510 week_model[day] = OrderedDict(
511 sorted(
512 day_model.items(),
513 key = lambda pair: hour_key(pair[0]),
514 )
515 )
516 week_model = [
517 {
518 'day': day,
519 'schedule': schedule,
520 'day-class': day_class(day.weekday())
521 } for day, schedule in week_model.items()
522 ]
494 week_model = sorted(week_model, key = lambda day: day['day']) 523 week_model = sorted(week_model, key = lambda day: day['day'])
495 from pprint import pprint
496 pprint(week_model)
497 return render_template( 524 return render_template(
498 'stop_week.html', 525 'stop_week.html',
499 ref = bus_stop.code, 526 ref = bus_stop.code,
500 name = tr(bus_stop.name, 'bus-stops'), 527 name = tr(bus_stop.name, 'bus-stops'),
501 tr = tr, 528 tr = tr,

mercurial