Thu, 14 Dec 2017 13:59:40 +0200
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
service.py | file | annotate | diff | comparison | revisions |
--- a/service.py Mon Dec 11 13:00:59 2017 +0200 +++ b/service.py Thu Dec 14 13:59:40 2017 +0200 @@ -438,10 +438,6 @@ tr = tr, ) - -def hour_key(hour): - return (hour - 5) % 24 - def day_class(weekday): if weekday < 5: return 'working-day' @@ -490,10 +486,41 @@ 'night': is_night_time(schedule_entry['time']), 'minute': time.minute, }) - week_model = [{'day': day, 'schedule': schedule, 'day-class': day_class(day.weekday())} for day, schedule in week_model.items()] + for day_offset in range(7): + from datetime import date, datetime, timedelta + day = date.today() + timedelta(day_offset) + try: + day_model = week_model[day] + except KeyError: + week_model[day] = {} + else: + def hour_key(x): + return (x - 5) % 24 + # Fill in missing hours from 5am to last active hour + hours = set(day_model.keys()) | {5} + sorted_hours = sorted(hours, key = hour_key) + start_hour = sorted_hours[0] + end_hour = sorted_hours[-1] + 1 + for hour in range(start_hour, end_hour): + hour_start = datetime(day.year, day.month, day.day, hour, 0) + if hour not in day_model and hour_start >= datetime.now(): + day_model[hour] = [] + # Sort the hours, so that 5am is first and 4am is last. + from collections import OrderedDict + week_model[day] = OrderedDict( + sorted( + day_model.items(), + key = lambda pair: hour_key(pair[0]), + ) + ) + week_model = [ + { + 'day': day, + 'schedule': schedule, + 'day-class': day_class(day.weekday()) + } for day, schedule in week_model.items() + ] week_model = sorted(week_model, key = lambda day: day['day']) - from pprint import pprint - pprint(week_model) return render_template( 'stop_week.html', ref = bus_stop.code,