Skip to content

Instantly share code, notes, and snippets.

@robrocker7
Created February 7, 2012 20:53
Show Gist options
  • Select an option

  • Save robrocker7/1761892 to your computer and use it in GitHub Desktop.

Select an option

Save robrocker7/1761892 to your computer and use it in GitHub Desktop.
def find_city(request):
ctx = {}
if 'q' in request.GET:
try:
# check to see if q is a int to assume a zipcode
zipcode = int(request.GET['q'])
# now check to see if the zip finds an ZipCode object
try:
zip_obj = ZipCode.objects.get(zip=zipcode)
city_obj = CityLocation.objects.get(
city_name=zip_obj.city, state=zip_obj.state)
return HttpResponseRedirect(city_obj.get_absolute_url())
except ZipCode.DoesNotExist:
ctx['error'] = 'zip_not_found'
except:
# assume its a string and try to find a city state match
terms = request.GET['q'].split(',')
terms = [term.strip() for term in terms]
# check to see if terms[0] is a state
try:
state = State.objects.get(name=terms[0])
except State.DoesNotExist:
try:
state = State.objects.get(abbreviation__iexact=terms[0])
except State.DoesNotExist:
state = None
# if state is not "None" and terms is more than 1 try to
# find a city otherwise redirect to the state page
if state is not None and len(terms) > 1:
try:
city_obj = CityLocation.objects.get(
state=state.abbreviation, city_name__iexact=terms[1])
return HttpResponseRedirect(city_obj.get_absolute_url())
except CityLocation.DoesNotExist:
ctx['error'] = 'city_not_found'
elif state is not None:
return HttpResponseRedirect(
state.get_absolute_url() + '?q=%s' % request.GET['q'])
# check to see if terms[0] is a city
cities = CityLocation.objects.filter(city_name__iexact=terms[0])
if len(cities) > 1 and len(terms) > 1:
try:
state = State.objects.get(name=terms[1])
except State.DoesNotExist:
try:
state = State.objects.get(abbreviation__iexact=terms[1])
except State.DoesNotExist:
state = None
if state is not None:
cities = cities.get(state=state.abbreviation)
return HttpResponseRedirect(cities.get_absolute_url())
# if cities == 1 then redirect
if len(cities) == 1:
return HttpResponseRedirect(cities[0].get_absolute_url())
ctx['matches'] = cities
forms = {}
forms['basic'] = PAContactForm()
ctx['forms'] = forms
states = State.objects.order_by('name')
ctx['states'] = states
return render_to_response('crime-stats/choose-state.html',
ctx,
context_instance=RequestContext(request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment