service.py

changeset 94
e27c18f080d1
parent 93
53ee9e945673
child 98
c07a77c8a070
equal deleted inserted replaced
93:53ee9e945673 94:e27c18f080d1
155 try: 155 try:
156 return place_abbreviations['abbreviations'][place] 156 return place_abbreviations['abbreviations'][place]
157 except KeyError: 157 except KeyError:
158 return place 158 return place
159 159
160 def trip_abbreviation(trip): 160 def trip_description(trip):
161 entries = [trip.from_place] 161 entries = [trip.from_place]
162 old_places = None 162 old_places = None
163 starting_halt = None 163 starting_halt = None
164 while True: 164 while True:
165 remaining_length = trip.length 165 remaining_length = trip.length
175 entries += [place] 175 entries += [place]
176 break 176 break
177 old_places = places 177 old_places = places
178 if trip.to_place not in entries: 178 if trip.to_place not in entries:
179 entries += [trip.to_place] 179 entries += [trip.to_place]
180 return trip.route.reference + ':' + '-'.join(map(place_abbreviation, entries)) 180 return entries
181
182 def trip_abbreviation(trip):
183 return trip.route.reference + ':' + '-'.join(map(place_abbreviation, trip_description(trip)))
181 184
182 def split_route_ref(route_ref): 185 def split_route_ref(route_ref):
183 try: 186 try:
184 return list(parse_route_ref(route_ref)) 187 return list(parse_route_ref(route_ref))
185 except ValueError: 188 except ValueError:
780 name = route.reference + ' ' + route.description, 783 name = route.reference + ' ' + route.description,
781 tr = tr, 784 tr = tr,
782 schedule = schedule, 785 schedule = schedule,
783 ) 786 )
784 787
788 @app.route('/interesting')
789 def interesting():
790 from buses import all_trips, services_for_day
791 from busroute import simplify_name
792 from collections import Counter, defaultdict
793 from datetime import datetime
794 trip_counts = Counter()
795 interesting_trips = defaultdict(list)
796 for trip_reference, trip in all_trips.items():
797 if len(trip.service.dates) <= 3:
798 for date in trip.service.dates:
799 moment = trip.schedule[0].departure_datetime(date)
800 if moment > datetime.now():
801 interesting_trips[date].append((trip, moment))
802 for date in interesting_trips:
803 interesting_trips[date].sort(key = lambda tuple: tuple[1])
804 interesting_trips = [(date, interesting_trips[date]) for date in sorted(interesting_trips.keys())]
805 data = [
806 {
807 'date': date,
808 'leaves': [{
809 'route': trip.route.reference,
810 'departure': moment,
811 'trip': trip.reference,
812 'stop': trip.schedule[0].stop,
813 'description': ' - '.join(
814 simplify_name(place, replace = True)
815 for place in trip_description(trip)
816 ),
817 } for trip, moment in leaves]
818 }
819 for date, leaves in interesting_trips
820 ]
821 return render_template(
822 'interesting.html',
823 data = data,
824 tr = tr,
825 )
826
785 @app.route('/') 827 @app.route('/')
786 def index(): 828 def index():
787 return redirect('stop_cluster/kauppatori') 829 return redirect('stop_cluster/kauppatori')
788 830
789 @app.route('/pysäkki/<reference>') 831 @app.route('/pysäkki/<reference>')

mercurial