service.py

changeset 31
60045b362d71
parent 30
a5bfd99bc2a3
child 38
d0ae1996abdd
--- a/service.py	Sat Jun 24 19:38:05 2017 +0300
+++ b/service.py	Wed Jun 28 12:20:05 2017 +0300
@@ -35,11 +35,14 @@
 		ini = ConfigParser()
 		ini.read(path.join(file_path))
 		self.languages[language_name] = ini
-	def __call__(self, name, section, language = None):
+	def __call__(self, name, *sections, language = None):
 		language = language or language_for_page()
-		try:
-			return self.languages[language][section][name]
-		except KeyError:
+		for section in sections:
+			try:
+				return self.languages[language][section][name]
+			except KeyError:
+				pass
+		else:
 			return name
 
 tr = Translator()
@@ -79,7 +82,7 @@
 		bus_stop = bus_stops[reference]
 	except KeyError:
 		abort(404)
-	for schedule_entry in bus_stop.schedule(100):
+	for schedule_entry in bus_stop.schedule(max_amount = 100, arrivals = True):
 		schedule.append({
 			'time': time_representation(schedule_entry['time']),
 			'route': schedule_entry['trip'].route.reference,
@@ -108,10 +111,10 @@
 		return '%d:%02d' % (time.hour, time.minute)
 	elif time_difference < timedelta(7):
 		with activate_locale():
-			return time.strftime('%-a %H:%M')
+			return time.strftime('%-a %H:%M').replace(' ', '\xa0')
 	else:
 		with activate_locale():
-			return time.strftime('%-d.%-m. %H:%M')
+			return time.strftime('%-d.%-m. %H:%M').replace(' ', '\xa0')
 
 @app.route('/pysäkkiryhmä/<cluster_name>')
 def cluster_schedule(cluster_name):
@@ -121,7 +124,7 @@
 		cluster = clusters_by_name[cluster_name]
 	except KeyError:
 		abort(404)
-	for schedule_entry in cluster.schedule(100):
+	for schedule_entry in cluster.schedule(max_amount = 100):
 		schedule.append({
 			'time': time_representation(schedule_entry['time']),
 			'route': schedule_entry['trip'].route.reference,
@@ -130,20 +133,20 @@
 			'night': is_night_time(schedule_entry['time']),
 			'stop_id': schedule_entry['stop'].stop.reference,
 			'stop_code': schedule_entry['stop'].stop.code, 
-			'stop_name': tr(schedule_entry['stop'].stop.name, 'bus_stops'),
+			'stop_name': tr(schedule_entry['stop'].stop.name, 'pysäkit'),
 		})
 	stops_in_cluster = sorted(
 		({
 			'id': stop.reference,
 			'code': stop.code,
-			'name': tr(stop.name, 'bus_stops'),
+			'name': tr(stop.name, 'pysäkit'),
 		} for stop in cluster.stops),
 		key = lambda stop: (len(stop['id']), stop['id'])
 	)
 	return render_template(
 		'cluster.html',
 		schedule = schedule,
-		name = cluster.name,
+		name = tr(cluster.name, 'paikat', 'pysäkkiryhmät', 'pysäkit'),
 		link_to_map = cluster.center.link_to_map,
 		location = cluster.center,
 		stops_in_cluster = stops_in_cluster,
@@ -155,41 +158,40 @@
 def trip(trip_reference):
 	from flask import request
 	from buses import all_trips
+	from busroute import simplify_name
 	try:
 		trip = all_trips[trip_reference]
 	except KeyError:
 		abort(404)
 	schedule = []
-	concise_schedule = []
-	used_areas = set()
+	region = ''
 	for halt in trip.schedule:
 		stop_time = datetime.combine(today(), time()) + halt.arrival_time
 		formatted_time = time_representation(stop_time)
-		schedule.append({
+		if halt.stop.region != region and not (region and not halt.stop.region):
+			if len(schedule) and not schedule[-1]['name']:
+				schedule[-1]['name'] = tr(halt.stop.region or '', 'paikat')
+			else:
+				schedule.append({
+					'name': tr(halt.stop.region or '', 'paikat'),
+					'time': formatted_time,
+					'stops': [],
+					'index': len(schedule),
+				})
+			region = halt.stop.region
+		schedule[-1]['stops'].append({
 			'time': formatted_time,
 			'id': halt.stop.reference,
 			'code': halt.stop.code,
-			'region': tr(halt.stop.region or '', 'paikat'),
 			'name': tr(halt.stop.name, 'bus_stops'),
 		})
-		region = halt.stop.region
-		if region:
-			if region not in used_areas:
-				concise_schedule.append({
-					'time': formatted_time,
-					'region': region or '',
-				})
-				used_areas.add(region)
-	sign = reduce_schedule([k['region'] for k in concise_schedule], whole = True, trip_length = trip.length)
+	sign = trip.concise_schedule()
 	try:
-		sign = [sign[0], sign[-1]]
+		sign = [simplify_name(sign[0]), simplify_name(sign[-1])]
 	except IndexError:
 		sign = [trip.schedule[0].stop.name, trip.schedule[-1].stop.name]
-	for entry in concise_schedule:
-		entry['region'] = tr(entry['region'], 'paikat')
 	return render_template('trip.html',
 		schedule = schedule,
-		concise_schedule = concise_schedule,
 		trip_reference = trip_reference,
 		route = trip.route.reference,
 		description = ' - '.join(tr(place, 'paikat') for place in sign),

mercurial