def post(self, request, **kwargs): category = request.POST.get('category') location = request.POST.get('location') context = self.get_context_data(**kwargs) context['map'] = True if category: search = Search(**{'text': 'Category: {0}'.format(category)}) search.save() museums = Museum.objects.filter(types__code=category) geojson = self.get_geojson(**{'name': category, 'museums': museums}) context["jsonfile"] = category elif location: search = Search(**{'text': location}) search.save() # Inputs: If just a state/abbrev given, show all items for that state only, no radius # Otherwise, geocode the result, run the vicenty distance if location.lower() in self.states_and_abbrevs: if len(location) != 2: location = STATES_NORMALIZED.get(location.lower()) context["jsonfile"] = location # TEMPORARY: EXCLUDE 11K GENERAL MUSEUMS FOR NOW -- Can always add them back later museums = Museum.objects.filter(state=location).exclude(types__code='GMU') if museums.count() > 0: geojson = self.get_geojson(**{'name': location, 'museums': museums}) # By this point, location is always a two-letter abbreviation address, (latitude, longitude) = self.geolocator.geocode(''.join([state_tuple[1] for state_tuple in US_STATES if state_tuple[0] == location])) else: try: museums = [] address, (latitude, longitude) = self.geolocator.geocode(location) except Exception: context["jsonfile"] = "" else: if latitude and longitude: all_museums = Museum.objects.exclude(types__code='GMU') for museum in all_museums: dist = vincenty( (museum.latitude, museum.longitude), (latitude, longitude) ).miles if dist <= RADIUS: museums.append(museum) context["jsonfile"] = hashlib.sha256(location).hexdigest()[:8] geojson = self.get_geojson(**{'name': context["jsonfile"], 'museums': museums}) context["latitude"] = latitude context["longitude"] = longitude # context["geojson_path"] = PATH_PREFIX context['museums'] = museums return render(request, self.template_name, context)
def post(self, request, **kwargs): category = request.POST.get('category') location = request.POST.get('location') context = self.get_context_data(**kwargs) context['map'] = True if category: search = Search(**{'text': 'Category: {0}'.format(category)}) search.save() museums = Museum.objects.filter(types__code=category) geojson = self.get_geojson(**{'name': category, 'museums': museums}) context["jsonfile"] = category elif location: search = Search(**{'text': location}) search.save() # Inputs: If just a state/abbrev given, show all items for that state only, no radius # Otherwise, geocode the result, run the vicenty distance if location.lower() in self.states_and_abbrevs: if len(location) != 2: location = STATES_NORMALIZED.get(location.lower()) context["jsonfile"] = location # TEMPORARY: EXCLUDE 11K GENERAL MUSEUMS FOR NOW -- Can always add them back later museums = Museum.objects.filter(state=location).exclude(types__code='GMU') if museums.count() > 0: geojson = self.get_geojson(**{'name': location, 'museums': museums}) # By this point, location is always a two-letter abbreviation address, (latitude, longitude) = self.geolocator.geocode(''.join([state_tuple[1] for state_tuple in US_STATES if state_tuple[0] == location])) else: try: museums = [] address, (latitude, longitude) = self.geolocator.geocode(location) except Exception: context["jsonfile"] = "" else: if latitude and longitude: all_museums = Museum.objects.exclude(types__code='GMU') for museum in all_museums: dist = vincenty( (museum.latitude, museum.longitude), (latitude, longitude) ).miles if dist <= RADIUS: museums.append(museum) context["jsonfile"] = hashlib.sha256(location).hexdigest()[:8] geojson = self.get_geojson(**{'name': context["jsonfile"], 'museums': museums}) context["latitude"] = latitude context["longitude"] = longitude # context["geojson_path"] = PATH_PREFIX context['museums'] = museums return render(request, self.template_name, context)
def _validate_state(self): """ Method to validate merchant state """ state = self.state state = STATES_NORMALIZED.get(state.strip().lower()) # if there was a fail - skip this row if not state: self.set_message( ('{}'.format(self.index), 'State is invalid format, ' 'use valide US state name', 'Warning')) state = '' self.data['state'] = state
def get_postal_code(self): return STATES_NORMALIZED.get(self.name.lower())
def state_abbr(value): return STATES_NORMALIZED.get(value.strip().lower(), '')
def state_abbr(value): return STATES_NORMALIZED.get(value.strip().lower(), '')