Exemple #1
0
    def city_autocomplete(self,city_name):
        import urllib
        params = urllib.urlencode({'q':city_name.encode('utf8'),'limit':10,'name':'city_name'})
        url = 'http://www.waze.co.il/WAS/autocomplete?%s'%params
        f = urllib.urlopen(url)
        fstr = f.read()
        results = fstr.strip().split("\n")
        #just for client debugging
        #if 'REFERER' not in request.headers:return fstr
        from greencouriers.model import City,meta
        for res_name in results:
            if not res_name.strip(): continue
            ctres = meta.Session.query(City).filter_by(name=res_name.strip()).all()
            if not ctres:
                ct = City()
                ct.name = res_name.strip()
                ct.population=-1
                import pylons
                ct.country = config['global_conf']['default_country'].lower()

                meta.Session.add(ct) ; 
        meta.Session.commit()
        import simplejson
        #response.headers['content-type']='application/json'
        return simplejson.dumps({'results':[{'id':res,'value':res} for res in results]
                                 ,'for':city_name})
Exemple #2
0
def search_city(city_name,country,insdata=None):
    if not insdata and country!='il':
        return search_city_google(city_name,country)
    if not insdata:
        import urllib
        params = urllib.urlencode({'city_name':city_name}) #,'street_name':u'מכבי'.encode('utf8')})
        murl = 'http://www.waze.co.il/WAS/search?%s'%params
        f = urllib.urlopen(murl)
        import simplejson
        resp = simplejson.load(f)
    else:
        resp = insdata

    if resp['successful']:
        from greencouriers.model import City,meta
        commit=False
        qres=meta.Session.query(City).filter_by(name=city_name,country=country).all()
        if qres:
            ct = qres[0]
        else: 
            ct = City()
            ct.country=country
            ct.name = city_name
            meta.Session.add(ct)
            commit=True
        if ct and not ct.lon:
            ct.lon = resp['result']['center']['lon']
            ct.lat = resp['result']['center']['lat']
            ct.extent_top=resp['result']['extent']['top']
            ct.extent_bottom=resp['result']['extent']['bottom']
            ct.extent_right=resp['result']['extent']['right']
            ct.extent_left=resp['result']['extent']['left']
            ct.population=-1
            commit=True
        if commit:meta.Session.commit()
    return resp