예제 #1
0
 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'] = []
예제 #2
0
 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'] = []
예제 #3
0
def map_place(request):
    ret_val = {}
    p = Place.make_from_request(request)
    ret_val['place'] = p.__dict__
    return ret_val
예제 #4
0
def stops_near(request):
    """ this routine is called by the stop lookup form.  we branch to either call the
        nearest stop routine (based on lat,lon coordiantes), or call stop.html directly

        this routine feels overly complex ... part of the problem is that we might see
        the stop id passed in via a string (place param gotten by SOLR / ajax), or the 
        stop might come in the string name from a geocoder, etc...

        Note that a log of logic is broken into 4 sub methods of stops_near ... these routiens
        will use (and possibly set) both request and ret_val variables in the parent scope
    """
    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 check_place_for_stopid(place):
        """ return what looks like a stop id in a string
        """
        stop = None
        if place and "Stop ID" in place:
            s = place.split("Stop ID")
            if s and len(s) >= 2:
                stop = s[1].strip()
                stop = re.sub('[\W+\s]+.*', '', stop)
        return stop

    def geo_has_stopid(geo):
        """ look for a stop id in the geocoder result
        """
        stop = None
        try:
            if 'stop_id' in geo and geo['stop_id']:
                stop = geo['stop_id']
            elif 'name' in geo:
                stop = check_place_for_stopid(geo['name'])
        except:
            pass
        return stop

    def add_string_to_querystr(qs, str):
        ret_val = ''
        sep = ''
        if str:
            ret_val = str
            sep = '&'
        if qs and len(qs) > 0:
            ret_val = "{0}{1}{2}".format(ret_val, sep, qs)
        return ret_val

    def make_qs_with_stop_id(stop_id, rec=None):
        query_string = ''
        if rec and 'lat' in rec and 'lon' in rec:
            query_string = add_string_to_querystr(
                query_string,
                "placeCoord={0},{1}".format(rec['lat'], rec['lon']))
        if stop_id:
            query_string = add_string_to_querystr(
                query_string, "stop_id={0}".format(stop_id))
        if request.query_string:
            query_string = add_string_to_querystr(request.query_string,
                                                  query_string)
        return query_string

    # step 1: query has stop_id param ... call stop.html
    stop_id = html_utils.get_first_param_as_str(request, 'stop_id')
    if stop_id:
        ret_val = make_subrequest(request, '/stop.html', request.query_string)
    else:
        # step 2: place param has name with stop_id in it ... call stop.html
        place = html_utils.get_first_param_as_str(request, 'place')
        stop_id = check_place_for_stopid(place)
        if stop_id:
            qs = make_qs_with_stop_id(stop_id)
            ret_val = make_subrequest(request, '/stop.html', qs)
        else:
            # step 3: params have geocode information, call nearest with that information
            p = Place.make_from_request(request)
            if p.is_valid_coord():
                call_near_ws(place=p)
            else:
                # step 4: geocode the place param and if we get a direct hit, call either stop or nearest
                place = html_utils.get_first_param_as_str(request, 'place')
                geo = geocode_utils.call_geocoder(request, place)
                if geo and geo['count'] == 1:
                    single_geo = geo['geocoder_results'][0]
                    # step 4a: looking for stop id in geo result ... if there we'll call stop.html directly
                    stop_id = geo_has_stopid(single_geo)
                    if stop_id:
                        qs = make_qs_with_stop_id(stop_id, single_geo)
                        ret_val = make_subrequest(request, '/stop.html', qs)
                        # NOTE can't add 'cache' here, since this is a subrequest http call...

                    # step 4b: we're going to call nearest based on the geocode coordinates
                    else:
                        call_near_ws(single_geo)
                        ret_val['cache'].append(
                            geocode_utils.make_autocomplete_cache(
                                place, single_geo))
                else:
                    ret_val = make_subrequest(request,
                                              '/stop_select_geocode.html')
    return ret_val
예제 #5
0
def map_place(request):
    ret_val = {}
    p = Place.make_from_request(request)
    ret_val['place'] = p.__dict__
    return ret_val
예제 #6
0
def stops_near(request):
    ''' this routine is called by the stop lookup form.  we branch to either call the
        nearest stop routine (based on lat,lon coordiantes), or call stop.html directly

        this routine feels overly complex ... part of the problem is that we might see
        the stop id passed in via a string (place param gotten by SOLR / ajax), or the 
        stop might come in the string name from a geocoder, etc...

        Note that a log of logic is broken into 4 sub methods of stops_near ... these routiens
        will use (and possibly set) both request and ret_val variables in the parent scope
    '''
    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 check_place_for_stopid(place):
        ''' return what looks like a stop id in a string
        '''
        stop = None
        if place and "Stop ID" in place:
            s = place.split("Stop ID")
            if s and len(s) >= 2:
                stop = s[1].strip()
                stop = re.sub('[\W+\s]+.*', '', stop)
        return stop

    def geo_has_stopid(geo):
        ''' look for a stop id in the geocoder result
        '''
        stop = None
        try:
            if 'stop_id' in geo and geo['stop_id']:
                stop = geo['stop_id']
            elif 'name' in geo:
                stop = check_place_for_stopid(geo['name'])
        except:
            pass
        return stop

    def add_string_to_querystr(qs, str):
        ret_val = ''
        sep = ''
        if str:
            ret_val = str
            sep = '&'
        if qs and len(qs) > 0:
            ret_val = "{0}{1}{2}".format(ret_val, sep, qs)
        return ret_val

    def make_qs_with_stop_id(stop_id, rec=None):
        query_string = ''
        if rec and 'lat' in rec and 'lon' in rec:
            query_string = add_string_to_querystr(query_string, "placeCoord={0},{1}".format(rec['lat'], rec['lon']))
        if stop_id:
            query_string = add_string_to_querystr(query_string, "stop_id={0}".format(stop_id))
        if request.query_string:
            query_string = add_string_to_querystr(request.query_string, query_string)
        return query_string

    # step 1: query has stop_id param ... call stop.html
    stop_id = html_utils.get_first_param_as_str(request, 'stop_id')
    if stop_id:
        ret_val = make_subrequest(request, '/stop.html', request.query_string)
    else:
        # step 2: place param has name with stop_id in it ... call stop.html
        place = html_utils.get_first_param_as_str(request, 'place')
        stop_id = check_place_for_stopid(place)
        if stop_id:
            qs = make_qs_with_stop_id(stop_id)
            ret_val = make_subrequest(request, '/stop.html', qs)
        else:
            # step 3: params have geocode information, call nearest with that information
            p = Place.make_from_request(request)
            if p.is_valid_coord():
                call_near_ws(place=p)
            else:
                # step 4: geocode the place param and if we get a direct hit, call either stop or nearest
                place = html_utils.get_first_param_as_str(request, 'place')
                geo = geocode_utils.call_geocoder(request, place)
                if geo and geo['count'] == 1:
                    single_geo = geo['geocoder_results'][0]
                    # step 4a: looking for stop id in geo result ... if there we'll call stop.html directly
                    stop_id = geo_has_stopid(single_geo)
                    if stop_id:
                        qs = make_qs_with_stop_id(stop_id, single_geo)
                        ret_val = make_subrequest(request, '/stop.html', qs)
                        # NOTE can't add 'cache' here, since this is a subrequest http call...

                    # step 4b: we're going to call nearest based on the geocode coordinates 
                    else:
                        call_near_ws(single_geo)
                        ret_val['cache'].append(geocode_utils.make_autocomplete_cache(place, single_geo))
                else:
                    ret_val = make_subrequest(request, '/stop_select_geocode.html')

    return ret_val