def by_city(self, city): # city may be a pk, a url, a crc, or a City instance. if isinstance(city, int): city = City.objects.get(pk=city) elif isinstance(city, str) and city.count(', ') == 2: city = City.get_by_url(city) elif isinstance(city, str) and city.count('/') == 2: city = City.get_by_crc(city) return self.published_only().filter(city=city)
def get_redirect_url(self, *args, **kwargs): q = self.request.GET.get('q', None) try: city = City.get_by_crc(q) except City.DoesNotExist: raise Http404 return reverse('category-list-html', args=[city.tr_url])
def city_by_latlng(request): """ Receive GET lat/lng and return localized info on nearest city. The client sends values from the HTML5 geolocation API: longitude and latitude. Find the city closest to the location and return its data in the language set in the LANGUAGE setting. """ try: lat = float(request.GET.get('latitude', None)) lng = float(request.GET.get('longitude', None)) except TypeError: return HttpResponseBadRequest() try: city = City.by_latlng(lat, lng) except City.DoesNotExist: raise Http404 try: an = AltName.objects.get(geoname_id=city.pk, type=3, is_main=True, language=settings.LANGUAGE_CODE) except AltName.DoesNotExist: raise Http404 return HttpResponse(json.dumps({ "id": city.id, "lat": city.lat, "lng": city.lng, "region": city.region.pk, "country": city.country.pk, "population": city.population, "slug": an.slug, "name": an.name, "crc": an.crc, "url": an.url, }), content_type="application/json")
def citymx(req, city_short, city_name, tpl='dtrseo/es_citymx.html'): # The old URLs to some Mexican cities, e.g. "/citymxmer/merida.html" abbr_cities = { 'aca': 3533462, # 'Acapulco', 'cam': 3531732, # 'Campeche', 'can': 3531673, # 'Cancún', 'chi': 4014338, # 'Chihuahua', 'jua': 4013708, # 'Ciudad Juárez', 'cue': 3529947, # 'Cuernavaca', 'gua': 4005539, # 'Guadalajara', 'her': 4004898, # 'Hermosillo', 'leo': 3998655, # 'León', 'maz': 3996322, # 'Mazatlán', 'mer': 3523349, # 'Mérida', 'mex': 3996069, # 'Mexicali', 'mdf': 3530597, # 'Mexico D.F.', 'mty': 3995465, # 'Monterrey', 'nog': 4004886, # 'Nogales', 'lar': 3522551, # 'Nuevo Laredo', 'oax': 3522507, # 'Oaxaca', 'pac': 3522210, # 'Pachuca', 'pue': 3521081, # 'Puebla', 'qro': 3991164, # 'Querétaro', 'slp': 3985606, # 'San Luís Potosí', 'tam': 3516355, # 'Tampico', 'tap': 3516266, # 'Tapachula', 'tij': 3981609, # 'Tijuana', 'tol': 3515302, # 'Toluca', 'tor': 3981254, # 'Torreón', 'tux': 3515001, # 'Tuxtla Gutiérrez', 'ver': 3514783, # 'Veracruz', 'vil': 3514670, # 'Villahermosa', 'xal': 3526617, # 'Xalapa', 'zac': 3979844, # 'Zacatecas', # 'aaa': 'Otra ciudad mexicana', # 'bbb': 'Fuera de México', } # Find the city id by place name. if city_short not in abbr_cities.keys(): return HttpResponseNotFound() city = City.objects.get(pk=abbr_cities[city_short]) cities = City.get_cities_around_city(city) ctx = { 'userprofiles': UserProfile.objects.filter( gender__in=[1, 2, 4, 5], city__in=cities, user__is_active=True, pic__isnull=False).order_by('-user__last_login').prefetch_related( 'user', 'city')[:48], 'city': get_object_or_404(AltName, geoname_id=abbr_cities[city_short], is_main=1, type=3, language=(get_language() or settings.LANGUAGE_CODE)[:2]), } return render(req, tpl, ctx)
def import_city(self): uptodate = self.download_once('city') if uptodate and not self.force: return data = self.get_data('city') self.build_country_index() self.build_region_index() cnt = 0 print('Importing city data ...') for items in self.parse(data): cnt += 1 type = items[7] if type not in conf['CITY_TYPES']: continue city = City() city.id = int(items[0]) # geoname_id city.name = items[1] # Real name city.lat = float(items[4]) # latitude in decimal degrees (wgs84) city.lng = float(items[5]) # longitude in decimal degrees (wgs84) city.population = items[14] # Find country try: city.country = self.country_index[items[8]] except: print('Skip city "{0}", no related country found!' .format(city.id)) continue # Find region try: rc = '{0}.{1}'.format(items[8].upper(), items[10]) city.region = self.region_index[rc] except: print('Skip city "{0}", no related region found!' .format(city.id)) continue city.save() print('{0} cities imported.'.format(cnt))
def city_by_latlng(request): """The client sends values from the HTML5 geolocation API: accuracy, longitude, latitude. Find the city closest to the location and return its data. """ try: lat = float(request.GET.get('latitude', None)) lng = float(request.GET.get('longitude', None)) except TypeError: return HttpResponseBadRequest() city = City.by_latlng(lat, lng) data = {"id": city.pk, "lat": city.lat, "lng": city.lng, "population": city.population, "country": city.country.pk, "crc": city.get_crc(), } return HttpResponse(json.dumps(data), content_type="application/json")
def get_queryset(self): self.city = City.get_by_url(self.kwargs['city']) _cities = City.get_cities_around_city(self.city, dist=25) _category = self.kwargs['category'] self.category = get_object_or_404(Category, slug=_category) return Post.objects.by_category(self.category).filter(city__in=_cities)
def get_context_data(self, **kwargs): cats = settings.ANUNCIOS['CATEGORIES'] context = super().get_context_data(**kwargs) context['city'] = City.get_by_url(self.kwargs['city']) context['grouping_list'] = [x for x in cats if x['parent'] is None] return context
def _get_city_by_crc(crc): """Return the database object found by the string from the <input> field.""" return City.get_by_crc(crc)
def import_city(self): uptodate = self.download_once('city') if uptodate and not self.force: return data = self.get_data('city') self.build_country_index() self.build_region_index() cnt = 0 print('Importing city data ...') for items in self.parse(data): cnt += 1 type = items[7] if type not in conf['CITY_TYPES']: continue city = City() city.id = int(items[0]) # geoname_id city.name = items[1] # Real name city.lat = float(items[4]) # latitude in decimal degrees (wgs84) city.lng = float(items[5]) # longitude in decimal degrees (wgs84) city.population = items[14] # Find country try: city.country = self.country_index[items[8]] except: print('Skip city "{0}", no related country found!'.format( city.id)) continue # Find region try: rc = '{0}.{1}'.format(items[8].upper(), items[10]) city.region = self.region_index[rc] except: print('Skip city "{0}", no related region found!'.format( city.id)) continue city.save() print('{0} cities imported.'.format(cnt))