A plethora of fixes to make this more usable on non-mobile devices; should gracefully degrade a bit better now... if it can't get any GPS coordinates, the user can enter an address and bypass that requirement, and we'll do a reverse lookup and get some GPS coordinates based on said address. Users can also now view the checkin history of other people - someone wanna implement friendships/privacy? We have an open source 4Square here. ;D

This commit is contained in:
Ryan McGrath 2010-10-29 05:10:41 -04:00
parent c17458cc8a
commit d503e3fe08
11 changed files with 223 additions and 116 deletions

View file

@ -1,17 +1,26 @@
import urllib2
from urllib2 import HTTPError
try:
# Python 2.6 and up
import json as simplejson
except ImportError:
# This case gets rarer by the day, but if we need to, we can pull it from Django provided it's there.
from django.utils import simplejson
from datetime import datetime, timedelta
from django.contrib.gis.geos import *
from django.contrib.gis.measure import D
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.utils import simplejson
from django.contrib.auth.models import User
from django.template import RequestContext, Context
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from drinkkit.redditors.models import LocationCategory, Location, Tip, Checkin
@ -153,28 +162,57 @@ def add_location(request):
fixed at some point. ;P
"""
if request.POST:
if request.POST['location_name'] and request.POST['lat'] and request.POST['long']:
new_location = Location()
new_location.name = request.POST['location_name']
new_location.geometry = 'POINT(%s %s)' %(request.POST['lat'], request.POST['long'])
# Somewhat ugly, but meh.
if request.POST['location_name'] and (request.POST['street_address'] or (request.POST['lat'] and request.POST['long'])):
new_location = Location()
new_location.name = request.POST['location_name']
if request.POST['lat'] and request.POST['long']:
new_location.geometry = 'POINT(%s %s)' %(request.POST['lat'], request.POST['long'])
else:
# If we don't have a lat/long pair, we know they got here by providing a street address, so
# let's do some geocoding via Google APIs and find the lat/long ourselves.
#
# Note: We assume Washington, DC here because of the region this application is suited to. If
# you wanted to expand on this, you'd probably wanna get the region/IP-(lat/long) (which isn't as accurate)
# and use it to sandbox your results. This is a pretty hacky 5AM thing. ;P
fixed_address = request.POST['street_address'].replace(" ", "+")
api_url = "http://maps.googleapis.com/maps/api/geocode/json?&address=%s,Washington,DC&sensor=false" % fixed_address
try:
# Download and parse the JSON response into native Python objects.
geocode_request = urllib2.Request(api_url)
geocoded_data = simplejson.load(urllib2.urlopen(geocode_request))
# Store latitude and longitude, for readability
for result in geocoded_data["results"]:
latitude = result["geometry"]["location"]["lat"]
longitude = result["geometry"]["location"]["lng"]
# Save our determined geometry coordinates
new_location.geometry = 'POINT(%s %s)' %(latitude, longitude)
except HTTPError:
# You're boned, Google's either blocking you or you done goofed. Ordinarily, you'd
# wanna handle errors properly here, but in my case I want a hard failure. YMMV.
pass
# If they've supplied a street address, sweet, use it.
if request.POST['street_address']:
new_location.street_address = request.POST['street_address']
# If they've supplied a street address, sweet, use it.
if request.POST['street_address']:
new_location.street_address = request.POST['street_address']
# If they set a location type/category, let's record it... (5AM code)
if request.POST['location_type']:
try:
category = LocationCategory.objects.get(id = request.POST['location_type'])
new_location.category = category
except LocationCategory.DoesNotExist:
pass
# If they set a location, let's record it... (5AM code)
if request.POST['location_type']:
try:
category = LocationCategory.objects.get(id = request.POST['location_type'])
new_location.category = category
except LocationCategory.DoesNotExist:
pass
new_location.save()
return HttpResponseRedirect('/locations/%s/' % str(new_location.id))
new_location.save()
return HttpResponseRedirect('/locations/%s/' % str(new_location.id))
else:
if not request.POST['lat'] or not request.POST['lat']:
errmsg = "We weren't able to get coordinates for where you are right now. Does your phone or device have GPS?"
errmsg = "We weren't able to get coordinates for where you are right now. Does your phone or device have GPS? If not, specify an address and we'll use that instead!"
if not request.POST['location_name']:
errmsg = "You didn't even bother to enter a name for this location. Wtf?"
return render_to_response('locations/add.html',
@ -235,8 +273,17 @@ def add_tip(request, location_id):
else:
return HttpResponse(status=404)
def find_redditors(request):
def view_redditor(request, redditor_name):
"""
Handles locating all Redditors in a given area who recently checked in.
View a Redditor "profile" - right now, all we do is show their checkin
history (where they've been). Stalking is fun, right?
...right?
"""
pass
try:
redditor = User.objects.get(username = redditor_name)
return render_to_response('redditors/view_profile.html',
context_instance = RequestContext(request, {'redditor': redditor})
)
except User.DoesNotExist:
return HttpResponse(status=404)