beginnings of api - route

This commit is contained in:
Sanj 2012-02-19 17:31:17 +05:30
parent eb17261df0
commit 217e823fdc
5 changed files with 55 additions and 2 deletions

View File

@ -0,0 +1,10 @@
from models import *
from ox.django.shortcuts import get_object_or_404_json
def route(request, code):
route = get_object_or_404_json(Route, code=code)
stops = [r.stop.get_dict() for r in RouteDetail.objects.filter(route=route)]
return render_to_json_response({
'route': route.get_dict(),
'stops': stops
})

View File

@ -95,6 +95,34 @@ class Stop(models.Model):
point = models.PointField(null=True)
alt_names = generic.GenericRelation("AlternativeName")
def get_dict(self):
return {
'id': self.id,
'code': self.code,
'slug': self.slug,
'official_name': self.name,
'display_name': self.display_name,
'road': self.road.name,
'area': self.area.name,
'name_mr': self.name_mr
#FIXME: add alt names
}
def get_geojson(self, srid=4326):
if self.point is not None:
geom = json.loads(self.point.transform(srid, True).geojson)
else:
geom = {}
properties = self.get_dict()
return {
'type': 'Feature',
'properties': properties,
'geometry': geom
}
def __unicode__(self):
return self.name
@ -130,6 +158,14 @@ class Route(models.Model):
def __unicode__(self):
return self.alias
def get_dict(self):
return {
'id': self.id,
'code': self.code,
'alias': self.alias,
'slug': self.slug,
'distance': self.distance
}
class RouteDetail(models.Model):
route_code = models.TextField()
@ -141,7 +177,8 @@ class RouteDetail(models.Model):
class Meta:
verbose_name = 'Route Detail'
ordering = ['serial']
def __unicode__(self):
return str(self.route) + " : " + str(self.serial)

View File

@ -2,6 +2,7 @@
from models import *
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from actions import *
def index(request):
return render_to_response("index.html", {})

View File

@ -11,6 +11,8 @@ ADMINS = (
# ('Your Name', 'your_email@domain.com'),
)
SITENAME = "ChaloBEST"
LOCAL_DEVELOPMENT = True
JSON_DEBUG = True

View File

@ -4,6 +4,9 @@ from os.path import join
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
#import ox.django.api.urls
#import mumbai
urlpatterns = patterns('',
# Example:
@ -14,7 +17,7 @@ urlpatterns = patterns('',
(r'^route/(?P<alias>[a-zA-Z0-9\s\-]*?)/$', 'mumbai.views.route'),
(r'^areas/$', 'mumbai.views.areas'),
(r'^area/(?P<name>.*?)/$', 'mumbai.views.area'),
(r'^1.0/', include('mumbai.apiurls')),
# Uncomment the admin/doc line below to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
#(r'^grappelli/', include('grappelli.urls')),