service.py

changeset 29
2c78e68d7363
parent 27
f89504285945
child 30
a5bfd99bc2a3
equal deleted inserted replaced
28:670ffa424ded 29:2c78e68d7363
58 58
59 def sign(schedule_entry): 59 def sign(schedule_entry):
60 from math import ceil 60 from math import ceil
61 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance 61 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance
62 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length) 62 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length)
63 sign = [tr(place, 'paikat') for place in sign] 63 if sign:
64 sign_representation = ' - '.join(sign) 64 sign = [tr(place, 'paikat') for place in sign]
65 #if len(sign_representation) > 25: 65 sign_representation = ' - '.join(sign)
66 # k = ceil(len(sign) / 2) 66 #if len(sign_representation) > 25:
67 # sign_representation = ' - '.join(sign[:k]) + '\n' + ' - '.join(sign[k:]) 67 # k = ceil(len(sign) / 2)
68 return sign_representation 68 # sign_representation = ' - '.join(sign[:k]) + '\n' + ' - '.join(sign[k:])
69 return sign_representation
70 else:
71 return schedule_entry['trip'].schedule[-1].stop.name
69 72
70 @app.route('/pysäkki/<reference>') 73 @app.route('/pysäkki/<reference>')
71 def bus_stop_schedule(reference): 74 def bus_stop_schedule(reference):
72 from buses import bus_stops 75 from buses import bus_stops
73 schedule = [] 76 schedule = []
84 'night': is_night_time(schedule_entry['time']), 87 'night': is_night_time(schedule_entry['time']),
85 }) 88 })
86 return render_template( 89 return render_template(
87 'stop.html', 90 'stop.html',
88 schedule = schedule, 91 schedule = schedule,
89 name = reference + ' ' + tr(bus_stop.name, 'bus_stops'), 92 name = bus_stop.code + ' ' + tr(bus_stop.name, 'bus_stops'),
90 link_to_map = bus_stop.location.link_to_map, 93 link_to_map = bus_stop.location.link_to_map,
91 region = bus_stop.region, 94 region = bus_stop.region,
92 location = bus_stop.location, 95 location = bus_stop.location,
93 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, 96 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None,
94 tr = tr, 97 tr = tr,
123 'route': schedule_entry['trip'].route.reference, 126 'route': schedule_entry['trip'].route.reference,
124 'sign': sign(schedule_entry), 127 'sign': sign(schedule_entry),
125 'trip': schedule_entry['stop'].trip.name, 128 'trip': schedule_entry['stop'].trip.name,
126 'night': is_night_time(schedule_entry['time']), 129 'night': is_night_time(schedule_entry['time']),
127 'stop_id': schedule_entry['stop'].stop.reference, 130 'stop_id': schedule_entry['stop'].stop.reference,
131 'stop_code': schedule_entry['stop'].stop.code,
128 'stop_name': tr(schedule_entry['stop'].stop.name, 'bus_stops'), 132 'stop_name': tr(schedule_entry['stop'].stop.name, 'bus_stops'),
129 }) 133 })
130 stops_in_cluster = sorted( 134 stops_in_cluster = sorted(
131 ({ 135 ({
132 'id': stop.reference, 136 'id': stop.reference,
137 'code': stop.code,
133 'name': tr(stop.name, 'bus_stops'), 138 'name': tr(stop.name, 'bus_stops'),
134 } for stop in cluster.stops), 139 } for stop in cluster.stops),
135 key = lambda stop: (len(stop['id']), stop['id']) 140 key = lambda stop: (len(stop['id']), stop['id'])
136 ) 141 )
137 return render_template( 142 return render_template(
159 for halt in trip.schedule: 164 for halt in trip.schedule:
160 stop_time = datetime.combine(today(), time()) + halt.arrival_time 165 stop_time = datetime.combine(today(), time()) + halt.arrival_time
161 formatted_time = time_representation(stop_time) 166 formatted_time = time_representation(stop_time)
162 schedule.append({ 167 schedule.append({
163 'time': formatted_time, 168 'time': formatted_time,
164 'reference': halt.stop.reference, 169 'id': halt.stop.reference,
170 'code': halt.stop.code,
165 'region': tr(halt.stop.region or '', 'paikat'), 171 'region': tr(halt.stop.region or '', 'paikat'),
166 'name': tr(halt.stop.name, 'bus_stops'), 172 'name': tr(halt.stop.name, 'bus_stops'),
167 }) 173 })
168 region = halt.stop.region 174 region = halt.stop.region
169 if region: 175 if region:
172 'time': formatted_time, 178 'time': formatted_time,
173 'region': region or '', 179 'region': region or '',
174 }) 180 })
175 used_areas.add(region) 181 used_areas.add(region)
176 sign = reduce_schedule([k['region'] for k in concise_schedule], whole = True, trip_length = trip.length) 182 sign = reduce_schedule([k['region'] for k in concise_schedule], whole = True, trip_length = trip.length)
177 sign = [sign[0], sign[-1]] 183 try:
184 sign = [sign[0], sign[-1]]
185 except IndexError:
186 sign = [trip.schedule[0].stop.name, trip.schedule[-1].stop.name]
178 for entry in concise_schedule: 187 for entry in concise_schedule:
179 entry['region'] = tr(entry['region'], 'paikat') 188 entry['region'] = tr(entry['region'], 'paikat')
180 return render_template('trip.html', 189 return render_template('trip.html',
181 schedule = schedule, 190 schedule = schedule,
182 concise_schedule = concise_schedule, 191 concise_schedule = concise_schedule,

mercurial