service.py

changeset 24
e6bdb9c54096
parent 22
3d094a804af8
child 25
cb423946cf33
--- a/service.py	Tue Jun 20 09:39:42 2017 +0300
+++ b/service.py	Tue Jun 20 10:27:52 2017 +0300
@@ -6,7 +6,7 @@
 import locale
 
 from misc import *
-from busroute import supista_reitti
+from busroute import reduce_schedule
 
 app = Flask(__name__)
 
@@ -58,138 +58,133 @@
 
 def sign(schedule_entry):
 	from math import ceil
-	ajomatka = schedule_entry['trip'].length - schedule_entry['stop'].ajettu_matka
-	sign = supista_reitti(schedule_entry['trip'].suppea_reitti(schedule_entry['stop']), ajomatka = ajomatka)
-	sign = [tr(paikka, 'paikat') for paikka in sign]
+	trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance
+	sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length)
+	sign = [tr(place, 'paikat') for place in sign]
 	sign_representation = ' - '.join(sign)
 	#if len(sign_representation) > 25:
 	#	k = ceil(len(sign) / 2)
 	#	sign_representation = ' - '.join(sign[:k]) + '\n' + ' - '.join(sign[k:])
 	return sign_representation
 
-@app.route('/pysäkki/<tunniste>')
-def pysäkkiaikataulu(tunniste):
-	from buses import pysäkit
-	aikataulu = []
+@app.route('/pysäkki/<reference>')
+def bus_stop_schedule(reference):
+	from buses import bus_stops
+	schedule = []
 	try:
-		pysäkki = pysäkit[tunniste]
+		bus_stop = bus_stops[reference]
 	except KeyError:
 		abort(404)
-	for schedule_entry in pysäkki.schedule(100):
-		aikataulu.append({
-			'aika': time_representation(schedule_entry['time']),
-			'linja': schedule_entry['trip'].linja.viite,
-			'kyltti': sign(schedule_entry),
-			'ajovuoro': schedule_entry['stop'].ajo.nimi,
-			'yö': is_night_time(schedule_entry['time']),
+	for schedule_entry in bus_stop.schedule(100):
+		schedule.append({
+			'time': time_representation(schedule_entry['time']),
+			'route': schedule_entry['trip'].route.reference,
+			'sign': sign(schedule_entry),
+			'trip': schedule_entry['stop'].trip.name,
+			'night': is_night_time(schedule_entry['time']),
 		})
-	from os import path
-	tausta = path.join('static', 'tausta-' + (pysäkki.alue or 'pysäkki').lower().replace(' ', '-') + '.png')
-	if not path.isfile(tausta):
-		tausta = None
 	return render_template(
 		'pysäkki.html',
-		aikataulu = aikataulu,
-		nimi = tunniste + ' ' + tr(pysäkki.nimi, 'pysäkit'),
-		linkki_karttaan = pysäkki.sijainti.link_to_map,
-		alue = pysäkki.alue,
-		sijainti = pysäkki.sijainti,
-		cluster = pysäkki.cluster.url_name if len(pysäkki.cluster.stops) > 1 else None,
+		schedule = schedule,
+		name = reference + ' ' + tr(bus_stop.name, 'bus_stops'),
+		link_to_map = bus_stop.location.link_to_map,
+		region = bus_stop.region,
+		location = bus_stop.location,
+		cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None,
 		tr = tr,
-		tausta = tausta,
 	)
 
-def time_representation(aika, suhteellinen = True):
-	erotus = aika - nyt()
-	if suhteellinen and erotus > timedelta(0) and erotus < timedelta(minutes = 1):
+def time_representation(time, relative = True):
+	time_difference = time - now()
+	if relative and time_difference > timedelta(0) and time_difference < timedelta(minutes = 1):
 		return tr('right-now', 'misc-text')
-	elif suhteellinen and erotus > timedelta(0) and erotus < timedelta(minutes = 10):
-		return '%dm' % round(erotus.seconds / 60)
-	elif aika.date() == tänään():
-		return '%d:%02d' % (aika.hour, aika.minute)
-	elif erotus < timedelta(7):
+	elif relative and time_difference > timedelta(0) and time_difference < timedelta(minutes = 10):
+		return '%dm' % round(time_difference.seconds / 60)
+	elif time.date() == today():
+		return '%d:%02d' % (time.hour, time.minute)
+	elif time_difference < timedelta(7):
 		with activate_locale():
-			return aika.strftime('%-a %H:%M')
+			return time.strftime('%-a %H:%M')
 	else:
 		with activate_locale():
-			return aika.strftime('%-d.%-m. %H:%M')
+			return time.strftime('%-d.%-m. %H:%M')
 
 @app.route('/pysäkkiryhmä/<cluster_name>')
 def cluster_schedule(cluster_name):
-	from buses import pysäkit, clusters_by_name
-	aikataulu = []
+	from buses import bus_stops, clusters_by_name
+	schedule = []
 	try:
 		cluster = clusters_by_name[cluster_name]
 	except KeyError:
 		abort(404)
 	for schedule_entry in cluster.schedule(100):
-		aikataulu.append({
-			'aika': time_representation(schedule_entry['time']),
-			'linja': schedule_entry['trip'].linja.viite,
-			'kyltti': sign(schedule_entry),
-			'ajovuoro': schedule_entry['stop'].ajo.nimi,
-			'yö': is_night_time(schedule_entry['time']),
-			'stop_id': schedule_entry['stop'].pysäkki.tunniste,
-			'stop_name': tr(schedule_entry['stop'].pysäkki.nimi, 'pysäkit'),
+		schedule.append({
+			'time': time_representation(schedule_entry['time']),
+			'route': schedule_entry['trip'].route.reference,
+			'sign': sign(schedule_entry),
+			'trip': schedule_entry['stop'].trip.name,
+			'night': is_night_time(schedule_entry['time']),
+			'stop_id': schedule_entry['stop'].stop.reference,
+			'stop_name': tr(schedule_entry['stop'].stop.name, 'bus_stops'),
 		})
 	return render_template(
 		'cluster.html',
-		aikataulu = aikataulu,
-		nimi = cluster.name,
-		linkki_karttaan = cluster.center.link_to_map,
-		sijainti = cluster.center,
+		schedule = schedule,
+		name = cluster.name,
+		link_to_map = cluster.center.link_to_map,
+		location = cluster.center,
 		stops_in_cluster = sorted(
 			({
-				'id': stop.tunniste,
-				'name': tr(stop.nimi, 'pysäkit'),
+				'id': stop.reference,
+				'name': tr(stop.name, 'bus_stops'),
 			} for stop in cluster.stops),
 			key = lambda stop: (len(stop['id']), stop['id'])
 		),
 		tr = tr,
 	)
 
-@app.route('/ajovuoro/<numero>')
-def ajoreitti(numero):
+@app.route('/ajovuoro/<trip_reference>')
+def trip(trip_reference):
 	from flask import request
-	from buses import ajot
+	from buses import all_trips
 	try:
-		ajovuoro = ajot[numero]
+		trip = all_trips[trip_reference]
 	except KeyError:
 		abort(404)
-	reitti = []
-	suppea_reitti = []
-	käydyt_alueet = set()
-	for pysähdys in ajovuoro.reitti:
-		aika = datetime.combine(tänään(), time()) + pysähdys.saapumisaika
-		muotoiltu_aika = time_representation(aika)
-		reitti.append({
-			'aika': muotoiltu_aika,
-			'tunniste': pysähdys.pysäkki.tunniste,
-			'alue': tr(pysähdys.pysäkki.alue or '', 'paikat'),
-			'nimi': tr(pysähdys.pysäkki.nimi, 'pysäkit'),
+	schedule = []
+	concise_schedule = []
+	used_areas = set()
+	for halt in trip.schedule:
+		stop_time = datetime.combine(today(), time()) + halt.arrival_time
+		formatted_time = time_representation(stop_time)
+		schedule.append({
+			'time': formatted_time,
+			'reference': halt.stop.reference,
+			'region': tr(halt.stop.region or '', 'paikat'),
+			'name': tr(halt.stop.name, 'bus_stops'),
 		})
-		alue = pysähdys.pysäkki.alue
-		if alue:
-			if alue not in käydyt_alueet:
-				suppea_reitti.append({
-					'aika': muotoiltu_aika,
-					'alue': alue or '',
+		region = halt.stop.region
+		if region:
+			if region not in used_areas:
+				concise_schedule.append({
+					'time': formatted_time,
+					'region': region or '',
 				})
-				käydyt_alueet.add(alue)
-	kyltti = supista_reitti([k['alue'] for k in suppea_reitti], kokonainen = True, ajomatka = ajovuoro.length)
-	kyltti = [kyltti[0], kyltti[-1]]
-	for entry in suppea_reitti:
-		entry['alue'] = tr(entry['alue'], 'paikat')
+				used_areas.add(region)
+	sign = reduce_schedule([k['region'] for k in concise_schedule], whole = True, trip_length = trip.length)
+	sign = [sign[0], sign[-1]]
+	for entry in concise_schedule:
+		entry['region'] = tr(entry['region'], 'paikat')
 	return render_template('ajovuoro.html',
-		reitti = reitti,
-		suppea_reitti = suppea_reitti,
-		numero = numero,
-		linja = ajovuoro.linja.viite,
-		selite = ' - '.join(tr(paikka, 'paikat') for paikka in kyltti),
-		yö = is_night_time(datetime.combine(tänään(), time()) + ajovuoro.reitti[-1].saapumisaika),
+		schedule = schedule,
+		concise_schedule = concise_schedule,
+		trip_reference = trip_reference,
+		route = trip.route.reference,
+		description = ' - '.join(tr(place, 'paikat') for place in sign),
+		night = is_night_time(datetime.combine(today(), time()) + trip.schedule[-1].arrival_time),
 		tr = tr,
-		length = ajovuoro.length / 1000
-		)
+		length = trip.length / 1000
+	)
 
 @app.route('/static/<path:path>')
 def static_file(path):

mercurial