56 else: |
56 else: |
57 return 'fi' |
57 return 'fi' |
58 |
58 |
59 def sign(schedule_entry): |
59 def sign(schedule_entry): |
60 from math import ceil |
60 from math import ceil |
61 ajomatka = schedule_entry['trip'].length - schedule_entry['stop'].ajettu_matka |
61 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance |
62 sign = supista_reitti(schedule_entry['trip'].suppea_reitti(schedule_entry['stop']), ajomatka = ajomatka) |
62 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length) |
63 sign = [tr(paikka, 'paikat') for paikka in sign] |
63 sign = [tr(place, 'paikat') for place in sign] |
64 sign_representation = ' - '.join(sign) |
64 sign_representation = ' - '.join(sign) |
65 #if len(sign_representation) > 25: |
65 #if len(sign_representation) > 25: |
66 # k = ceil(len(sign) / 2) |
66 # k = ceil(len(sign) / 2) |
67 # sign_representation = ' - '.join(sign[:k]) + '\n' + ' - '.join(sign[k:]) |
67 # sign_representation = ' - '.join(sign[:k]) + '\n' + ' - '.join(sign[k:]) |
68 return sign_representation |
68 return sign_representation |
69 |
69 |
70 @app.route('/pysäkki/<tunniste>') |
70 @app.route('/pysäkki/<reference>') |
71 def pysäkkiaikataulu(tunniste): |
71 def bus_stop_schedule(reference): |
72 from buses import pysäkit |
72 from buses import bus_stops |
73 aikataulu = [] |
73 schedule = [] |
74 try: |
74 try: |
75 pysäkki = pysäkit[tunniste] |
75 bus_stop = bus_stops[reference] |
76 except KeyError: |
76 except KeyError: |
77 abort(404) |
77 abort(404) |
78 for schedule_entry in pysäkki.schedule(100): |
78 for schedule_entry in bus_stop.schedule(100): |
79 aikataulu.append({ |
79 schedule.append({ |
80 'aika': time_representation(schedule_entry['time']), |
80 'time': time_representation(schedule_entry['time']), |
81 'linja': schedule_entry['trip'].linja.viite, |
81 'route': schedule_entry['trip'].route.reference, |
82 'kyltti': sign(schedule_entry), |
82 'sign': sign(schedule_entry), |
83 'ajovuoro': schedule_entry['stop'].ajo.nimi, |
83 'trip': schedule_entry['stop'].trip.name, |
84 'yö': is_night_time(schedule_entry['time']), |
84 'night': is_night_time(schedule_entry['time']), |
85 }) |
85 }) |
86 from os import path |
|
87 tausta = path.join('static', 'tausta-' + (pysäkki.alue or 'pysäkki').lower().replace(' ', '-') + '.png') |
|
88 if not path.isfile(tausta): |
|
89 tausta = None |
|
90 return render_template( |
86 return render_template( |
91 'pysäkki.html', |
87 'pysäkki.html', |
92 aikataulu = aikataulu, |
88 schedule = schedule, |
93 nimi = tunniste + ' ' + tr(pysäkki.nimi, 'pysäkit'), |
89 name = reference + ' ' + tr(bus_stop.name, 'bus_stops'), |
94 linkki_karttaan = pysäkki.sijainti.link_to_map, |
90 link_to_map = bus_stop.location.link_to_map, |
95 alue = pysäkki.alue, |
91 region = bus_stop.region, |
96 sijainti = pysäkki.sijainti, |
92 location = bus_stop.location, |
97 cluster = pysäkki.cluster.url_name if len(pysäkki.cluster.stops) > 1 else None, |
93 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, |
98 tr = tr, |
94 tr = tr, |
99 tausta = tausta, |
|
100 ) |
95 ) |
101 |
96 |
102 def time_representation(aika, suhteellinen = True): |
97 def time_representation(time, relative = True): |
103 erotus = aika - nyt() |
98 time_difference = time - now() |
104 if suhteellinen and erotus > timedelta(0) and erotus < timedelta(minutes = 1): |
99 if relative and time_difference > timedelta(0) and time_difference < timedelta(minutes = 1): |
105 return tr('right-now', 'misc-text') |
100 return tr('right-now', 'misc-text') |
106 elif suhteellinen and erotus > timedelta(0) and erotus < timedelta(minutes = 10): |
101 elif relative and time_difference > timedelta(0) and time_difference < timedelta(minutes = 10): |
107 return '%dm' % round(erotus.seconds / 60) |
102 return '%dm' % round(time_difference.seconds / 60) |
108 elif aika.date() == tänään(): |
103 elif time.date() == today(): |
109 return '%d:%02d' % (aika.hour, aika.minute) |
104 return '%d:%02d' % (time.hour, time.minute) |
110 elif erotus < timedelta(7): |
105 elif time_difference < timedelta(7): |
111 with activate_locale(): |
106 with activate_locale(): |
112 return aika.strftime('%-a %H:%M') |
107 return time.strftime('%-a %H:%M') |
113 else: |
108 else: |
114 with activate_locale(): |
109 with activate_locale(): |
115 return aika.strftime('%-d.%-m. %H:%M') |
110 return time.strftime('%-d.%-m. %H:%M') |
116 |
111 |
117 @app.route('/pysäkkiryhmä/<cluster_name>') |
112 @app.route('/pysäkkiryhmä/<cluster_name>') |
118 def cluster_schedule(cluster_name): |
113 def cluster_schedule(cluster_name): |
119 from buses import pysäkit, clusters_by_name |
114 from buses import bus_stops, clusters_by_name |
120 aikataulu = [] |
115 schedule = [] |
121 try: |
116 try: |
122 cluster = clusters_by_name[cluster_name] |
117 cluster = clusters_by_name[cluster_name] |
123 except KeyError: |
118 except KeyError: |
124 abort(404) |
119 abort(404) |
125 for schedule_entry in cluster.schedule(100): |
120 for schedule_entry in cluster.schedule(100): |
126 aikataulu.append({ |
121 schedule.append({ |
127 'aika': time_representation(schedule_entry['time']), |
122 'time': time_representation(schedule_entry['time']), |
128 'linja': schedule_entry['trip'].linja.viite, |
123 'route': schedule_entry['trip'].route.reference, |
129 'kyltti': sign(schedule_entry), |
124 'sign': sign(schedule_entry), |
130 'ajovuoro': schedule_entry['stop'].ajo.nimi, |
125 'trip': schedule_entry['stop'].trip.name, |
131 'yö': is_night_time(schedule_entry['time']), |
126 'night': is_night_time(schedule_entry['time']), |
132 'stop_id': schedule_entry['stop'].pysäkki.tunniste, |
127 'stop_id': schedule_entry['stop'].stop.reference, |
133 'stop_name': tr(schedule_entry['stop'].pysäkki.nimi, 'pysäkit'), |
128 'stop_name': tr(schedule_entry['stop'].stop.name, 'bus_stops'), |
134 }) |
129 }) |
135 return render_template( |
130 return render_template( |
136 'cluster.html', |
131 'cluster.html', |
137 aikataulu = aikataulu, |
132 schedule = schedule, |
138 nimi = cluster.name, |
133 name = cluster.name, |
139 linkki_karttaan = cluster.center.link_to_map, |
134 link_to_map = cluster.center.link_to_map, |
140 sijainti = cluster.center, |
135 location = cluster.center, |
141 stops_in_cluster = sorted( |
136 stops_in_cluster = sorted( |
142 ({ |
137 ({ |
143 'id': stop.tunniste, |
138 'id': stop.reference, |
144 'name': tr(stop.nimi, 'pysäkit'), |
139 'name': tr(stop.name, 'bus_stops'), |
145 } for stop in cluster.stops), |
140 } for stop in cluster.stops), |
146 key = lambda stop: (len(stop['id']), stop['id']) |
141 key = lambda stop: (len(stop['id']), stop['id']) |
147 ), |
142 ), |
148 tr = tr, |
143 tr = tr, |
149 ) |
144 ) |
150 |
145 |
151 @app.route('/ajovuoro/<numero>') |
146 @app.route('/ajovuoro/<trip_reference>') |
152 def ajoreitti(numero): |
147 def trip(trip_reference): |
153 from flask import request |
148 from flask import request |
154 from buses import ajot |
149 from buses import all_trips |
155 try: |
150 try: |
156 ajovuoro = ajot[numero] |
151 trip = all_trips[trip_reference] |
157 except KeyError: |
152 except KeyError: |
158 abort(404) |
153 abort(404) |
159 reitti = [] |
154 schedule = [] |
160 suppea_reitti = [] |
155 concise_schedule = [] |
161 käydyt_alueet = set() |
156 used_areas = set() |
162 for pysähdys in ajovuoro.reitti: |
157 for halt in trip.schedule: |
163 aika = datetime.combine(tänään(), time()) + pysähdys.saapumisaika |
158 stop_time = datetime.combine(today(), time()) + halt.arrival_time |
164 muotoiltu_aika = time_representation(aika) |
159 formatted_time = time_representation(stop_time) |
165 reitti.append({ |
160 schedule.append({ |
166 'aika': muotoiltu_aika, |
161 'time': formatted_time, |
167 'tunniste': pysähdys.pysäkki.tunniste, |
162 'reference': halt.stop.reference, |
168 'alue': tr(pysähdys.pysäkki.alue or '', 'paikat'), |
163 'region': tr(halt.stop.region or '', 'paikat'), |
169 'nimi': tr(pysähdys.pysäkki.nimi, 'pysäkit'), |
164 'name': tr(halt.stop.name, 'bus_stops'), |
170 }) |
165 }) |
171 alue = pysähdys.pysäkki.alue |
166 region = halt.stop.region |
172 if alue: |
167 if region: |
173 if alue not in käydyt_alueet: |
168 if region not in used_areas: |
174 suppea_reitti.append({ |
169 concise_schedule.append({ |
175 'aika': muotoiltu_aika, |
170 'time': formatted_time, |
176 'alue': alue or '', |
171 'region': region or '', |
177 }) |
172 }) |
178 käydyt_alueet.add(alue) |
173 used_areas.add(region) |
179 kyltti = supista_reitti([k['alue'] for k in suppea_reitti], kokonainen = True, ajomatka = ajovuoro.length) |
174 sign = reduce_schedule([k['region'] for k in concise_schedule], whole = True, trip_length = trip.length) |
180 kyltti = [kyltti[0], kyltti[-1]] |
175 sign = [sign[0], sign[-1]] |
181 for entry in suppea_reitti: |
176 for entry in concise_schedule: |
182 entry['alue'] = tr(entry['alue'], 'paikat') |
177 entry['region'] = tr(entry['region'], 'paikat') |
183 return render_template('ajovuoro.html', |
178 return render_template('ajovuoro.html', |
184 reitti = reitti, |
179 schedule = schedule, |
185 suppea_reitti = suppea_reitti, |
180 concise_schedule = concise_schedule, |
186 numero = numero, |
181 trip_reference = trip_reference, |
187 linja = ajovuoro.linja.viite, |
182 route = trip.route.reference, |
188 selite = ' - '.join(tr(paikka, 'paikat') for paikka in kyltti), |
183 description = ' - '.join(tr(place, 'paikat') for place in sign), |
189 yö = is_night_time(datetime.combine(tänään(), time()) + ajovuoro.reitti[-1].saapumisaika), |
184 night = is_night_time(datetime.combine(today(), time()) + trip.schedule[-1].arrival_time), |
190 tr = tr, |
185 tr = tr, |
191 length = ajovuoro.length / 1000 |
186 length = trip.length / 1000 |
192 ) |
187 ) |
193 |
188 |
194 @app.route('/static/<path:path>') |
189 @app.route('/static/<path:path>') |
195 def static_file(path): |
190 def static_file(path): |
196 return send_from_directory(path.join('static', path)) |
191 return send_from_directory(path.join('static', path)) |
197 |
192 |