Simori.com

Where Art meets Science and falls in love.

Archive for the ‘Interesting’ Category

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.

More is part-2…

 

I am disabling comments for this posts as I already wrote this post in my personal blog. Head over to my blog to share some feedback with me.

Adobe Acrobat 9: Now Available

For those who love Adobe Reader. Good news for them.

Adobe Systems Incorporated (Nasdaq:ADBE) today announced the immediate availability of Adobe® Acrobat® 9 software, a significant upgrade that transforms the process of creating and sharing electronic documents. ”

it cost around 150$ in the market.

  • 0 Comments
  • Filed under: Interesting
  • G-phone

    Android to appear on HTC Handset device end of this year and Windows Mobile 7 to follow later beginning of 2009.

    HTC Corp, produces powerful handsets that continually push the boundaries of innovation to provide true mobile freedom.

    Chen announced at HTC Touch Diamond launch in South Africa, that HTC is planning to unveil a device with Google Android Operating System by Q4 of this year, and a Windows Mobile 7 by Q1 of next year, at least that’s what’s on the planned release schedule from HTC.

  • 1 Comment
  • Filed under: Interesting
  • 0 Comments
  • Filed under: Interesting