<s> import json <EOL> from datetime import datetime , timedelta <EOL> from flask import ( flash , <EOL> make_response , <EOL> render_template , <EOL> request ) <EOL> from breezeminder . app import app <EOL> from breezeminder . forms . marta import MartaRouteStatusForm <EOL> from breezeminder . models . marta import ( Route , <EOL> Schedule , <EOL> Stop , <EOL> ScheduledStop , <EOL> Bus ) <EOL> from breezeminder . util . views import same_origin , nocache <EOL> @ nocache <EOL> def status ( ) : <EOL> form = MartaRouteStatusForm ( request . form ) <EOL> context = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : form <EOL> } <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) : <EOL> try : <EOL> context [ '<STR_LIT>' ] = Route . objects . get ( name = form . route . data ) <EOL> print context [ '<STR_LIT>' ] <EOL> except Route . DoesNotExist : <EOL> flash ( '<STR_LIT>' % form . route . data , '<STR_LIT:error>' ) <EOL> return render_template ( '<STR_LIT>' , ** context ) <EOL> @ same_origin <EOL> def route_details ( route_id ) : <EOL> route = Route . objects . get_or_404 ( name = route_id ) <EOL> schedule = Schedule . objects . for_today ( ) <EOL> resp = make_response ( json . dumps ( route . to_json ( schedule = schedule ) ) ) <EOL> resp . cache_control . no_cache = True <EOL> resp . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> return resp <EOL> @ same_origin <EOL> def route_realtime ( route_id ) : <EOL> current = datetime . now ( ) - timedelta ( hours = <NUM_LIT:1> ) <EOL> qs = Bus . objects . filter ( route = route_id , <EOL> status_time__gte = current , <EOL> is_stale = False ) <EOL> data = [ bus . to_json ( ) for bus in qs ] <EOL> resp = make_response ( json . dumps ( data ) ) <EOL> resp . cache_control . no_cache = True <EOL> resp . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> return resp <EOL> @ same_origin <EOL> def route_upcoming ( route_id ) : <EOL> route = Route . objects . get_or_404 ( name = route_id ) <EOL> schedule = Schedule . objects . for_today ( ) <EOL> data = { } <EOL> start = ScheduledStop . arrival_now ( ) <EOL> qs = ScheduledStop . objects . filter ( route_id = route . id , <EOL> schedule_id = schedule . id , <EOL> arrival__gt = start , <EOL> arrival__lt = start + <NUM_LIT> ) <EOL> if '<STR_LIT>' in request . args : <EOL> try : <EOL> stop = Stop . objects . get ( id = request . args [ '<STR_LIT>' ] ) <EOL> except ( Stop . DoesNotExist , Stop . MultipleObjectsReturned , ValueError ) : <EOL> pass <EOL> else : <EOL> qs = qs . filter ( stop_id = stop . id ) <EOL> for stop in qs . only ( '<STR_LIT>' , '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) : <EOL> if stop . stop_id not in data : <EOL> data [ stop . stop_id ] = { <EOL> '<STR_LIT>' : stop . arrival - start , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> if len ( data [ stop . stop_id ] [ '<STR_LIT>' ] ) >= <NUM_LIT:3> : <EOL> continue <EOL> data [ stop . stop_id ] [ '<STR_LIT>' ] . append ( <EOL> ScheduledStop . seconds_to_timestring ( stop . arrival ) <EOL> ) <EOL> resp = make_response ( json . dumps ( data ) ) <EOL> resp . cache_control . no_cache = True <EOL> resp . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> return resp <EOL> app . add_url_rule ( '<STR_LIT>' , '<STR_LIT>' , status , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> app . add_url_rule ( '<STR_LIT>' , '<STR_LIT>' , route_details ) <EOL> app . add_url_rule ( '<STR_LIT>' , '<STR_LIT>' , route_realtime ) <EOL> app . add_url_rule ( '<STR_LIT>' , '<STR_LIT>' , route_upcoming ) </s>
