Simori.com

Where Art meets Science and falls in love.

Archive for the ‘Featured’ 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.

Sneak Preview of Adobe Suite CS4

it’s been more than a year.  Adobe Creative Suite 3 was released in March 2007. And we are going to have a sneak preview of Adobe CS4.

The previews of Photoshop for Photo editing, Dreamweaver for Web design, Fireworks for image editing, and Soundbooth for audio editing has drastic changes when it is compared with Adobe CS3 Suite.

Adobe Systems is offering 2 day  trials of 3 beta applications from its next Creative Suite package.

You can get it from  Dreamweaver for Web design, Fireworks for image editing, and Soundbooth for audio editing.

  • 0 Comments
  • Filed under: Featured
  • A “Green” Floating City

    Called Lilypads, these are basically “zero-emission floating homes that uses solar, wind, tidal and biomass power to generate energy for its inhabitants.” From Gizmodo

    Mac Tablet Rumors (Pics)

    Here are a couple of great would-be Mac Tablets designs.

    According to MacRumors, there was a few rumors prior to the WWDC08 (1, 2, 3) regarding the development of a Mac tablet from Apple. Many even speculated that Apple will announce the their first tablet computer; however, that still remained unspoken despite that there are numerous patents, job-listing and collaborations between Intel and Apple with these few months.