Building a Brightkite using django-Part-1
Sunday, 17th August, 2008 // 11:05 a.m.As we know Brightkite is a location based social network where users can checkin into a location and make posts. I always had a thought on how we can acheive this functionality using django framework. Last week I started off this project with an eye on contributing it to Pinax project. I talked to Jtauber and I was lucky enough to get a chance before the feature freeze of pinax.
To start this project we need to recollect the basic requirements.
1. Registration for users. This can be acheived by django registration
2. Search for a location and get its proper place name, latitude and longitude data
3. Ability to checkin to that location. In simple words store the userid, place name, latitude, longitude and datetime of the checkin.
Implementation:
I am not going to start a brand new django project and build it upon it. Instead I recommend you to check out pinax project source code. Or you start a new project and add django-registration app to INSTALLED_APPS and set the urls. There are a bunch of tutorials on web on how to do that. Now we can create a new application called "locations".
#!/bin/sh $ python manage.py startapp locations
You now have the basic skeleton of the location application. Lets start creating the model "Location". As discussed in the basic requirements above,
from django.db import models from django.contrib.auth.models import User class Location(models.Model): user = models.ForeignKey(User) time_checkin = models.DateTimeField() place = models.CharField(max_length=100) latitude = models.FloatField() longitude = models.FloatField() class Meta: ordering = ('-time_checkin',) get_latest_by = 'time_checkin'
The model is pretty self explanatory. It has a foriegn key to User, so that each user can have his own checkins. After the model is created our objective is to search for a location and get the geo data for that location. For this we will take help of GeoPy a geocoding toolbox for python. We can query web services like Yahoo, Google, Geocoders to get geo data for a location.
We create an entry in urls.py for this lcoations search.
urlpatterns = patterns('', (r'new/$', 'locations.views.new'), )
To create a form, just create a file forms.py in locations and
class LocationForm(forms.Form): place = forms.CharField()
This means when somebody tries to access the url '/new/' the request is routed to the view 'new'. Lets create a definition for the 'new'. Open view.py
@login_required def new(request): if request.method == 'POST': location_form = LocationForm(request.POST) if location_form.is_valid(): y = geocoders.Yahoo('yahoo_map_api') p = location_form.cleaned_data['place'] place, (lat, lng) = y.geocode(p) location = {'place': place, 'latitude': lat, 'longitude': lng} checkin_form = CheckinForm() return render_to_response("locations/checkin.html", {"location": location, "checkin_form": checkin_form}) else: location_form = LocationForm() return render_to_response("locations/new.html", {"location_form": location_form})
First of all I am protecting this view, so that only logged in users(@login_required) will be able to search locations. Next if the request of the type POST only the loop gets executed. If some tries to access with a GET/anything the else part will be executed there by rendering an empty form again. I'm extracting the data from request.POST and putting in location_form variable.
Next we import geocoders from geopy and then instantiate its Yahoo class. We can use Google or Geocoders as well, but I chose Yahoo. Pass your YAHOO_API_KEY as argument to Yahoo class. I am extracting the form data into a variable 'p' and then performing a geocode using the yahoo object. A request is made to Yahoo Geocoding service and geo data is returned. I created 3 variables place, lat & lng which contains place name, latitude & longitude. I pass all these variables into a dictionary and pass them to a template(checkin.html). In this process I am also creating a blank checkin_form which is also passed to the tempalte. I will discuss those details in the next part. You can check out the checkin.html which is pretty simple. It contains just a form.
Check out the part-2 for more.
Comment Form
6 comments:
001// Justin Lilly// Sunday, 17th August, 2008, 5:03 p.m.
Wowza! Great start so far. I'm particularly interested in the templating portion of this project. How you rendered the maps and whatnot. Also, Pinax is awesome ;)
002// yashh// Sunday, 17th August, 2008, 6:10 p.m.
@justinlilly
Thank you for the comment. You are right the templating part has a little trick which is yet to come in the next part. I will be writing the next part on 20th August. Do check that out. Yes I have rendered the maps.
003// snirp// Wednesday, 17th September, 2008, 11:58 a.m.
Nice stuff! Your views.py needs the following, right?
from geopy import geocoders
004// yashh// Wednesday, 17th September, 2008, 9:28 p.m.
@snirp
Yes you need to import geopy. Here is the complete views.py. I am glad you enjoyed it.
005// Zefepidge// Friday, 12th December, 2008, 3:40 a.m.
Hi! My name is Jessika!
006// soma c o d // Friday, 1st May, 2009, 10:47 a.m.
Well-mannered plat! Sum to favorite