def solr_to_pelias_param(cls, solr_params): """ convert SOLR dict params dict of params for Pelias :see: https://trimet.org/solr/select?q=3&rows=6 :see: https://trimet.org/solr/select?q=3&rows=6&wt=json&fq=type:stop :see: https://ws-st.trimet.org/pelias/v1/autocomplete?text=13135&size=1&layers=address&sources=osm """ ret_val = {} text = html_utils.get_first_param(solr_params, 'q') if text: ret_val['text'] = text size = html_utils.get_first_param(solr_params, 'rows') if size: ret_val['size'] = size format = html_utils.get_first_param(solr_params, 'wt') if format and format == "xml": ret_val['format'] = 'xml' layers = html_utils.get_first_param(solr_params, 'fq') if layers: layers = layers.replace('%3A', ':') if layers == 'type:stop': ret_val['layers'] = 'stops' elif layers == 'type:pr': ret_val['layers'] = 'pr' return ret_val
def stop_schedule(request): html_tabs = stop_sched = alerts = None stop_id = html_utils.get_first_param(request, 'stop_id') route = html_utils.get_first_param(request, 'route') try: url = 'stop_schedule.html?stop_id={0}&route={1}'.format(stop_id, route) html_tabs = schedule_tabs.get_tabs(request, url) stop_sched = request.model.get_stop_schedule(request.query_string, **request.params) alerts = transit_utils.get_stoptime_alerts(stop_sched) except Exception, e: log.warning('{0} exception:{1}'.format(request.path, e))
def do_from_to_geocode_check(request): ''' checks whether we have proper coordinates for the from & to params if we're missing a coordinate, we'll geocode and see if there's a direct hit if no direct hit, we return the geocode_paaram that tells the ambiguous redirect page what to do... @return: a modified query string, and any extra params needed for the geocoder ''' geocode_param = None query_string = request.query_string # step 1: check for from & to coord information in the url has_from_coord = has_coord(request, 'from') has_to_coord = has_coord(request, 'to') # step 2: check we need to geocode the 'from' param ... if has_from_coord is False: geocode_param = 'geo_type=from' # step 3a: does the 'from' param need geocoding help? do we have a param to geocode? frm = html_utils.get_first_param(request, 'from') if frm and len(frm) > 0: # step 3b: we have something to geocode, so call the geocoder hoping to hit on a single result g = call_geocoder(request, frm, 'from') if g and g['count'] == 1: # step 3c: got our single result, so now add that to our query string... has_from_coord = True query_string = "fromCoord={0},{1}&{2}".format(g['geocoder_results'][0]['lat'], g['geocoder_results'][0]['lon'], query_string) query_string = query_string.replace("&fromCoord=&", "&").replace("&fromCoord=None&", "&") # strip bogus stuff off... geocode_param = None # step 4: check that we need to geocode the 'to' param if has_to_coord is False and has_from_coord is True: geocode_param = 'geo_type=to' # step 5a: does the 'to' param need geocoding help? do we have a param to geocode? to = html_utils.get_first_param(request, 'to') if to and len(to) > 0: # step 5b: we have something to geocode, so call the geocoder hoping to hit on a single result g = call_geocoder(request, to, 'to') if g and g['count'] == 1: # step 5c: got our single result, so now add that to our query string... has_to_coord = True query_string = "toCoord={0},{1}&{2}".format(g['geocoder_results'][0]['lat'], g['geocoder_results'][0]['lon'], query_string) query_string = query_string.replace("&toCoord=&", "&").replace("&toCoord=None&", "&") # strip bogus stuff off... geocode_param = None return query_string, geocode_param
def do_stops_near(request): ''' will either return the nearest list of stops, or geocode redirects TODO TODO TODO Needs work.... ''' has_geocode = html_utils.get_first_param_as_boolean(request, 'has_geocode') has_coord = html_utils.get_first_param_is_a_coord(request, 'placeCoord') if has_geocode or has_coord: call_near_ws() else: place = html_utils.get_first_param(request, 'place') geo = call_geocoder(request, place) if geo['count'] == 1: single_geo = geo['geocoder_results'][0] if single_geo['type'] == 'stop': query_string = "{0}&stop_id={1}".format(request.query_string, single_geo['stop_id']) ret_val = make_subrequest(request, '/stop.html', query_string) else: call_near_ws(single_geo) else: ret_val = make_subrequest(request, '/stop_select_geocode.html') return ret_val
def planner(request): ''' will either call the trip planner, or if we're missing params, redirect to the ambiguous geocode page basically, call the geocode checker, and then either call the ambiguous geocoder page, or plan the trip planner ''' try: ret_val = {} gc = geocode_utils.do_from_to_geocode_check(request) if gc['geocode_param']: ret_val = make_subrequest(request, '/planner_geocode.html', gc['query_string'], gc['geocode_param']) else: mapit = html_utils.get_first_param(request, 'mapit') if mapit: params = TripParamParser(request) params.set_from(gc['from']) params.set_to(gc['to']) # when Arr(ive) flag is set to latest, we do an arrive by at 1:30am the next day if params.is_latest(): params.date_offset(day_offset=1) map_params = params.map_url_params() ret_val = forward_request(request, 'http://ride.trimet.org?submit&' + map_params) else: #import pdb; pdb.set_trace() ret_val = request.model.get_plan(gc['query_string'], **request.params) ret_val['cache'] = gc['cache'] if ret_val and 'error' in ret_val: msg = object_utils.get_error_message(ret_val) ret_val = make_subrequest(request, '/exception.html', 'error_message={0}'.format(msg)) except Exception, e: log.warning('{0} exception:{1}'.format(request.path, e)) ret_val = make_subrequest(request, '/exception.html')
def planner_geocode(request): """ for the ambiguous geocode page """ try: geo_place = None geo_type = html_utils.get_first_param(request, 'geo_type', 'place') if 'from' in geo_type: geo_place = html_utils.get_first_param(request, 'from') elif 'to' in geo_type: geo_place = html_utils.get_first_param(request, 'to') ret_val = geocode_utils.call_geocoder(request, geo_place, geo_type) except Exception as e: log.warning('{0} exception:{1}'.format(request.path, e)) ret_val = make_subrequest(request, '/exception.html') return ret_val
def qrcode(request): ''' streams a qrcode image for the param 'content' (defaults to http://trimet.org) ''' response = Response(content_type='image/png') content = html_utils.get_first_param(request, 'content', 'http://trimet.org') img_io = qr_to_stream(content) response.app_iter = img_io return response
def planner_geocode(request): ''' for the ambiguous geocode page ''' try: geo_place = None geo_type = html_utils.get_first_param(request, 'geo_type', 'place') if 'from' in geo_type: geo_place = html_utils.get_first_param(request, 'from') elif 'to' in geo_type: geo_place = html_utils.get_first_param(request, 'to') ret_val = geocode_utils.call_geocoder(request, geo_place, geo_type) adverts = request.model.get_adverts(request, **request.params) ret_val['adverts'] = adverts except Exception, e: log.warning('{0} exception:{1}'.format(request.path, e)) ret_val = make_subrequest(request, '/exception.html')
def qrcode(request): """ streams a qrcode image for the param 'content' (defaults to http://opentransittools.org) """ response = Response(content_type='image/png') content = html_utils.get_first_param(request, 'content', 'http://opentransittools.org') img_io = qr_to_stream(content) response.app_iter = img_io return response
def has_coord(request, type='place'): ''' determine if the url has either a typeCoord url parameter, or a type::45.5,-122.5 param ''' ret_val = False coord = html_utils.get_first_param_is_a_coord(request, type + 'Coord') if coord: ret_val = True else: place = html_utils.get_first_param(request, type) if geo_utils.has_coord(place): ret_val = True return ret_val
def stop_select_list(request): try: route_stops = request.model.get_route_stops(request.query_string, **request.params) if route_stops['route'] and route_stops['has_errors'] is not True: ret_val = {} route = html_utils.get_first_param(request, 'route') ret_val['route_stops'] = route_stops else: ret_val = make_subrequest(request, '/stop_select_form.html') except Exception, e: log.warning('{0} exception:{1}'.format(request.path, e)) ret_val = make_subrequest(request, '/exception.html', 'app_name=Stop Select List')
def call_near_ws(geo=None, place=None): if place is None: place = Place.make_from_request(request) place.update_values_via_dict(geo) params = place.to_url_params() ret_val['place'] = place.__dict__ ret_val['params'] = params num = 5 if html_utils.get_first_param(request, 'show_more', None): num = 30 params = "num={0}&".format(num) + params ret_val['nearest'] = request.model.get_stops_near(params, **request.params) ret_val['cache'] = []
def stop_schedule(request): html_tabs = stop_sched = alerts = None stop_id = html_utils.get_first_param(request, 'stop_id') route = html_utils.get_first_param(request, 'route') try: url = 'stop_schedule.html?stop_id={0}&route={1}'.format(stop_id, route) html_tabs = schedule_tabs.get_tabs(request, url) stop_sched = request.model.get_stop_schedule(request.query_string, **request.params) alerts = transit_utils.get_stoptime_alerts(stop_sched) except Exception as e: log.warning('{0} exception:{1}'.format(request.path, e)) if html_tabs and stop_sched and stop_sched['has_errors'] is not True: ret_val = {} ret_val['html_tabs'] = html_tabs ret_val['stop_sched'] = stop_sched ret_val['alerts'] = alerts else: ret_val = make_subrequest(request, '/exception.html', 'app_name=Stop Schedule page') return ret_val
def planner(request): """ will either call the trip planner, or if we're missing params, redirect to the ambiguous geocode page basically, call the geocode checker, and then either call the ambiguous geocoder page, or plan the trip planner Map Redirect URLs: http://trimet.org/ride/planner.html?from=pdx&to=zoo&mapit=a http://trimet.org/ride/planner.html?from=888+SE+Lambert&to=ZOO%3A%3A45.5097%2C-122.71629&mapit=a http://trimet.org/ride/planner.html?from=CL%3A%3A45.4684%2C-122.657&to=ZOO%3A%3A45.5097%2C-122.71629&mode=RAIL%2CTRAM%2CSUBWAY%2CFUNICULAR%2CGONDOLA%2CBICYCLE&hour=12&minute=30&m=pm&month=1&day=15&walk=3219&optimize=SAFE&arr=A&mapit=a https://modbeta.trimet.org/ride/#/?fromPlace=PDX%2C%20Portland%3A%3A45.589178%2C-122.593464&toPlace=Oregon%20Zoo%2C%20Portland%3A%3A45.510185%2C-122.715861&date=2019-01-15&time=13%3A38&arriveBy=true&mode=TRAM%2CRAIL%2CGONDOLA%2CBICYCLE&showIntermediateStops=true&maxWalkDistance=4828&maxBikeDistance=4828&optimize=SAFE&bikeSpeed=3.58&ignoreRealtimeUpdates=true&companies= """ # import pdb; pdb.set_trace() try: ret_val = {} gc = geocode_utils.do_from_to_geocode_check(request) if gc['geocode_param']: ret_val = make_subrequest(request, '/planner_geocode.html', gc['query_string'], gc['geocode_param']) else: mapit = html_utils.get_first_param(request, 'mapit') if mapit: params = TripParamParser(request) params.set_from(gc['from']) params.set_to(gc['to']) # when Arr(ive) flag is set to latest, we do an arrive by at 1:30am the next day if params.is_latest(): params.date_offset(day_offset=1) # import pdb; pdb.set_trace() if "ride.trimet.org" in request.model.map_url: ride_params = params.map_url_params() map_url = "{}?submit&{}".format(request.model.map_url, ride_params) else: map_params = params.mod_url_params() map_url = "{}?{}".format(request.model.map_url, map_params) ret_val = forward_request(request, map_url) else: ret_val = request.model.get_plan(gc['query_string'], **request.params) ret_val['cache'] = gc['cache'] if ret_val and 'error' in ret_val: msg = object_utils.get_error_message(ret_val) ret_val = make_subrequest(request, '/exception.html', 'error_message={0}'.format(msg)) except Exception as e: log.warning('{0} exception:{1}'.format(request.path, e)) ret_val = make_subrequest(request, '/exception.html') return ret_val
def call_near_ws(geo=None, place=None): if place is None: place = Place.make_from_request(request) place.update_values_via_dict(geo) params = place.to_url_params() ret_val['place'] = place.__dict__ ret_val['params'] = params num = 5 if html_utils.get_first_param(request, 'show_more', None): num = 30 params = "num={0}&".format(num) + params ret_val['nearest'] = request.model.get_stops_near( params, **request.params) ret_val['cache'] = []
def stop_select_list(request): try: route_stops = request.model.get_route_stops(request.query_string, **request.params) if route_stops['route'] and route_stops['has_errors'] is not True: ret_val = {} route = html_utils.get_first_param(request, 'route') ret_val['route_stops'] = route_stops else: ret_val = make_subrequest(request, '/stop_select_form.html') except Exception as e: log.warning('{0} exception:{1}'.format(request.path, e)) ret_val = make_subrequest(request, '/exception.html', 'app_name=Stop Select List') return ret_val
def get_tabs(request, url): ''' ''' date = html_utils.get_first_param_as_date(request) month = html_utils.get_first_param_as_int(request, 'month') day = html_utils.get_first_param_as_int(request, 'day') date = date_utils.set_date(date, month, day) more = html_utils.get_first_param(request, 'more') ret_val = {} ret_val['more_form'] = date_utils.get_day_info(date) ret_val['pretty_date'] = date_utils.pretty_date(date) ret_val['tabs'] = get_svc_date_tabs(date, url, more is not None, get_translator(request)) return ret_val
def stop_select_form(request): routes = None try: routes = request.model.get_routes(request.query_string, **request.params) except Exception as e: log.warning('{0} exception:{1}'.format(request.path, e)) if routes and routes['has_errors'] is not True: ret_val = {} ret_val['place'] = html_utils.get_first_param(request, 'place') ret_val['routes'] = routes else: ret_val = make_subrequest(request, '/exception.html', 'app_name=Stop Select page') return ret_val
def stop(request): stop = None has_coord = html_utils.get_first_param_is_a_coord(request, 'placeCoord') try: stop = request.model.get_stop(request.query_string, **request.params) except Exception as e: log.warning('{0} exception:{1}'.format(request.path, e)) if stop and stop['has_errors'] is not True: ret_val = {} ret_val['stop'] = stop elif has_coord: coord = html_utils.get_first_param(request, 'placeCoord') ret_val = make_subrequest(request, '/stops_near.html', 'placeCoord={0}'.format(coord)) else: ret_val = make_subrequest(request, '/exception.html', 'app_name=Stop Details page') return ret_val
def get_tabs(request, url): ''' make the set of tabs on the schedule page ''' #import pdb; pdb.set_trace() is_prev_day = use_previous_day(request) if is_prev_day: date = date_utils.get_day_before() else: date = html_utils.get_first_param_as_date(request) month = html_utils.get_first_param_as_int(request, 'month') day = html_utils.get_first_param_as_int(request, 'day') date = date_utils.set_date(date, month, day) more = html_utils.get_first_param(request, 'more') tab_id = html_utils.get_first_param_as_int(request, 'tab_id', 0) ret_val = {} ret_val['more_form'] = date_utils.get_day_info(date) ret_val['pretty_date'] = date_utils.pretty_date(date) ret_val['tabs'] = make_date_tabs(date, url, is_prev_day, tab_id, more is not None, get_translator(request)) return ret_val
def get_tabs(request, url): """ make the set of tabs on the schedule page """ #import pdb; pdb.set_trace() is_prev_day = use_previous_day(request) if is_prev_day: date = date_utils.get_day_before() else: date = html_utils.get_first_param_as_date(request) month = html_utils.get_first_param_as_int(request, 'month') day = html_utils.get_first_param_as_int(request, 'day') year = date_utils.normalize_year(month) date = date_utils.set_date(date, month, day, year) more = html_utils.get_first_param(request, 'more') tab_id = html_utils.get_first_param_as_int(request, 'tab_id', 0) ret_val = {} ret_val['more_form'] = date_utils.get_day_info(date) ret_val['pretty_date'] = date_utils.pretty_date(date) ret_val['tabs'] = make_date_tabs(date, url, is_prev_day, tab_id, more is not None, get_translator(request)) return ret_val
def make_from_request(cls, request, param_name='place'): ret_val = Place() try: name = html_utils.get_first_param(request, 'name') if name is None: _ = get_translator(request) name = _(u'Uncertain location') lat = html_utils.get_first_param(request, 'lat') lon = html_utils.get_first_param(request, 'lon') city = html_utils.get_first_param(request, 'city') ret_val.set_values(name, lat, lon, city) place = html_utils.get_first_param(request, param_name) ret_val.set_values_via_place_str(place) place_coord = html_utils.get_first_param(request, param_name + 'Coord') ret_val.set_values_via_coord_str(place_coord) except: pass return ret_val
def make_from_request(cls, request, param_name='place'): ret_val = Place() try: name = html_utils.get_first_param(request, 'name') if name is None: _ = get_translator(request) name = _(u'Uncertain location') lat = html_utils.get_first_param(request, 'lat') lon = html_utils.get_first_param(request, 'lon') city = html_utils.get_first_param(request, 'city') ret_val.set_values(name, lat, lon, city) place = html_utils.get_first_param(request, param_name) ret_val.set_values_via_place_str(place) place_coord = html_utils.get_first_param(request, param_name + 'Coord') ret_val.set_values_via_coord_str(place_coord) except Exception as e: pass return ret_val
def do_from_to_geocode_check(request): """ checks whether we have proper coordinates for the from & to params if we're missing a coordinate, we'll geocode and see if there's a direct hit if no direct hit, we return the geocode_paaram that tells the ambiguous redirect page what to do... @return: a modified query string, and any extra params needed for the geocoder """ ret_val = {'query_string': None, 'geocode_param': None, 'from': None, 'to': None, 'cache': []} # step 1: check for from & to coord information in the url has_from_coord = geo_utils.is_param_a_coord(request, 'from') has_to_coord = geo_utils.is_param_a_coord(request, 'to') qs = request.query_string # step 2: check we need to geocode the 'from' param ... if has_from_coord is False: ret_val['geocode_param'] = 'geo_type=from' # step 3a: does the 'from' param need geocoding help? do we have a param to geocode? frm = html_utils.get_first_param(request, 'from') if frm and len(frm) > 0: # step 3b: we have something to geocode, so call the geocoder hoping to hit on a single result g = call_geocoder(request, frm, 'from') if g and g['count'] == 1: # step 3c: got our single result, so now add that to our query string... has_from_coord = True doc = g['geocoder_results'][0] fp = geo_utils.solr_to_named_param(doc, frm) qs = "from={0}&{1}".format(fp, qs) qs = qs.replace("&fromCoord=&", "&").replace("&fromCoord=None&", "&") # strip bogus stuff off... # step 3d: clear flag and set newly geocoded 'from' parameter ret_val['geocode_param'] = None ret_val['from'] = fp ret_val['cache'].append(make_autocomplete_cache(frm, doc)) # step 4: check that we need to geocode the 'to' param if has_to_coord is False and has_from_coord is True: ret_val['geocode_param'] = 'geo_type=to' # step 4a: does the 'to' param need geocoding help? do we have a param to geocode? to = html_utils.get_first_param(request, 'to') if to and len(to) > 0: # step 4b: we have something to geocode, so call the geocoder hoping to hit on a single result g = call_geocoder(request, to, 'to') if g and g['count'] == 1: # step 4c: got our single result, so now add that to our query string... has_to_coord = True doc = g['geocoder_results'][0] tp = geo_utils.solr_to_named_param(doc, to) qs = "to={0}&{1}".format(tp, qs) qs = qs.replace("&toCoord=&", "&").replace("&toCoord=None&", "&") # strip bogus stuff off... # step 4d: clear flag and set newly geocoded 'to' parameter ret_val['geocode_param'] = None ret_val['to'] = tp ret_val['cache'].append(make_autocomplete_cache(to, doc)) # step 5: assign query string to return ret_val['query_string'] = qs return ret_val
def stop_select_geocode(request): place = html_utils.get_first_param(request, 'place') ret_val = geocode_utils.call_geocoder(request, place) return ret_val
@view_config(route_name='stop_desktop', renderer='desktop/stop.html') @view_config(route_name='stop_ws', renderer='ws/stop.html') def stop(request): stop = None has_coord = html_utils.get_first_param_is_a_coord(request, 'placeCoord') try: stop = request.model.get_stop(request.query_string, **request.params) except Exception, e: log.warning('{0} exception:{1}'.format(request.path, e)) if stop and stop['has_errors'] is not True: ret_val = {} ret_val['stop'] = stop elif has_coord: coord = html_utils.get_first_param(request, 'placeCoord') ret_val = make_subrequest(request, '/stops_near.html', 'placeCoord={0}'.format(coord)) else: ret_val = make_subrequest(request, '/exception.html', 'app_name=Stop Details page') return ret_val @view_config(route_name='stop_schedule_mobile', renderer='mobile/stop_schedule.html') @view_config(route_name='stop_schedule_desktop', renderer='desktop/stop_schedule.html') @view_config(route_name='stop_schedule_ws', renderer='ws/stop_schedule.html') def stop_schedule(request): html_tabs = stop_sched = alerts = None stop_id = html_utils.get_first_param(request, 'stop_id') route = html_utils.get_first_param(request, 'route') try: url = 'stop_schedule.html?stop_id={0}&route={1}'.format(stop_id, route)
def do_from_to_geocode_check(request): ''' checks whether we have proper coordinates for the from & to params if we're missing a coordinate, we'll geocode and see if there's a direct hit if no direct hit, we return the geocode_paaram that tells the ambiguous redirect page what to do... @return: a modified query string, and any extra params needed for the geocoder ''' ret_val = {'query_string':None, 'geocode_param':None, 'from':None, 'to':None, 'cache':[]} # step 1: check for from & to coord information in the url has_from_coord = geo_utils.is_param_a_coord(request, 'from') has_to_coord = geo_utils.is_param_a_coord(request, 'to') qs = request.query_string # step 2: check we need to geocode the 'from' param ... if has_from_coord is False: ret_val['geocode_param'] = 'geo_type=from' # step 3a: does the 'from' param need geocoding help? do we have a param to geocode? frm = html_utils.get_first_param(request, 'from') if frm and len(frm) > 0: # step 3b: we have something to geocode, so call the geocoder hoping to hit on a single result g = call_geocoder(request, frm, 'from') if g and g['count'] == 1: # step 3c: got our single result, so now add that to our query string... has_from_coord = True doc = g['geocoder_results'][0] fp = geo_utils.solr_to_named_param(doc, frm) qs = "from={0}&{1}".format(fp, qs) qs = qs.replace("&fromCoord=&", "&").replace("&fromCoord=None&", "&") # strip bogus stuff off... # step 3d: clear flag and set newly geocoded 'from' parameter ret_val['geocode_param'] = None ret_val['from'] = fp ret_val['cache'].append(make_autocomplete_cache(frm, doc)) # step 4: check that we need to geocode the 'to' param if has_to_coord is False and has_from_coord is True: ret_val['geocode_param'] = 'geo_type=to' # step 4a: does the 'to' param need geocoding help? do we have a param to geocode? to = html_utils.get_first_param(request, 'to') if to and len(to) > 0: # step 4b: we have something to geocode, so call the geocoder hoping to hit on a single result g = call_geocoder(request, to, 'to') if g and g['count'] == 1: # step 4c: got our single result, so now add that to our query string... has_to_coord = True doc = g['geocoder_results'][0] tp = geo_utils.solr_to_named_param(doc, to) qs = "to={0}&{1}".format(tp, qs) qs = qs.replace("&toCoord=&", "&").replace("&toCoord=None&", "&") # strip bogus stuff off... # step 4d: clear flag and set newly geocoded 'to' parameter ret_val['geocode_param'] = None ret_val['to'] = tp ret_val['cache'].append(make_autocomplete_cache(to, doc)) # step 5: assign query string to return ret_val['query_string'] = qs return ret_val
def make_place_from_stop_request(request, stop): place = html_utils.get_first_param(request, 'place') name = name_from_named_place(place, place) ret_val = geo_utils.make_place(name, lat, lon)
def query_by_request(self, request, mode="rail", lang="en"): m = html_utils.get_first_param(request, 'mode', mode) l = html_utils.get_first_param(request, '_LOCALE_', lang) log.debug("mode={0}, lang={1}".format(m, l)) return self.query(m, l)