From 217e823fdce29420d6cae93a943bc97d8b0f2f23 Mon Sep 17 00:00:00 2001 From: Sanj Date: Sun, 19 Feb 2012 17:31:17 +0530 Subject: [PATCH] beginnings of api - route --- chaloBEST/mumbai/apiviews.py | 10 +++++++++ chaloBEST/mumbai/models.py | 39 +++++++++++++++++++++++++++++++++++- chaloBEST/mumbai/views.py | 1 + chaloBEST/settings.py | 2 ++ chaloBEST/urls.py | 5 ++++- 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 chaloBEST/mumbai/apiviews.py diff --git a/chaloBEST/mumbai/apiviews.py b/chaloBEST/mumbai/apiviews.py new file mode 100644 index 0000000..25a00d8 --- /dev/null +++ b/chaloBEST/mumbai/apiviews.py @@ -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 + }) diff --git a/chaloBEST/mumbai/models.py b/chaloBEST/mumbai/models.py index aaeee15..72e2e41 100644 --- a/chaloBEST/mumbai/models.py +++ b/chaloBEST/mumbai/models.py @@ -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) diff --git a/chaloBEST/mumbai/views.py b/chaloBEST/mumbai/views.py index 4a1c164..0079b50 100644 --- a/chaloBEST/mumbai/views.py +++ b/chaloBEST/mumbai/views.py @@ -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", {}) diff --git a/chaloBEST/settings.py b/chaloBEST/settings.py index 1cf223a..53a0719 100644 --- a/chaloBEST/settings.py +++ b/chaloBEST/settings.py @@ -11,6 +11,8 @@ ADMINS = ( # ('Your Name', 'your_email@domain.com'), ) +SITENAME = "ChaloBEST" + LOCAL_DEVELOPMENT = True JSON_DEBUG = True diff --git a/chaloBEST/urls.py b/chaloBEST/urls.py index 2ecc4de..f437764 100644 --- a/chaloBEST/urls.py +++ b/chaloBEST/urls.py @@ -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[a-zA-Z0-9\s\-]*?)/$', 'mumbai.views.route'), (r'^areas/$', 'mumbai.views.areas'), (r'^area/(?P.*?)/$', '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')),