mumbaitrains + apiviews minor optimization

This commit is contained in:
Sanj 2012-05-08 21:41:50 +05:30
parent 413896befa
commit 7842288a63
7 changed files with 106 additions and 2 deletions

View File

@ -67,9 +67,9 @@ def areas(request):
def stops(request):
q = request.GET.get("q", "")
if q != '':
qset = Stop.objects.find_approximate(q, TRIGRAM_THRESHOLD).select_related()
qset = Stop.objects.find_approximate(q, TRIGRAM_THRESHOLD).select_related('road', 'area')
else:
qset = Stop.objects.all().select_related()
qset = Stop.objects.all().select_related('road', 'area')
srid = int(request.GET.get("srid", 4326))
return render_to_json_response({
'type': 'FeatureCollection',

View File

View File

@ -0,0 +1,32 @@
from django.contrib.gis.db import models
LINE_CHOICES = (
('Western', 'Western'),
('Central', 'Central'),
('Harbour', 'Harbour'),
)
class Train(models.Model):
number = models.CharField(max_length=128)
line = models.CharField(max_length=128, choices=LINE_CHOICES, db_index=True)
stations = models.ManyToManyField("Station", through='TrainStation')
def __unicode__(self):
return self.number
class Station(models.Model):
name = models.CharField(max_length=128)
point = models.PointField(null=True, blank=True)
# line = models.CharField(choices=LINE_CHOICES, db_index=True)
def __unicode__(self):
return self.name
class TrainStation(models.Model):
train = models.ForeignKey(Train)
station = models.ForeignKey(Station)
serial = models.IntegerField()
time = models.TimeField()
# Create your models here.

View File

@ -0,0 +1,52 @@
from pyquery import PyQuery as pq
from models import *
import datetime
BASE_URL = 'http://mumbailifeline.com/'
'''
eg.:
>>>parseURL('http://mumbailifeline.com/timetable.php?sel_route=central&sfrom=Mumbai_CST&sto=Masjid&time1=4:00%20am&time2=11:59%20PM', Central')
'''
def parseURL(url, line):
d = pq(url=url)
table = d('#gradient-style')
trs = table.find('tr')
for i in range(1,len(trs)):
thisTr = trs[i]
td = thisTr.getchildren()[0]
a = td.find('a')
trainNo = a.text.strip()
print "Saving %s ... " % trainNo
trainURL = BASE_URL + a.get('href').strip()
saveTrain(trainURL, trainNo, line)
def saveTrain(url, no, line):
train, created = Train.objects.get_or_create(number=no, line=line)
if not created:
print "Train no %s already exists in db, skipping" % no
return
train.save()
d = pq(url=url)
table = d.find('table')[3]
for tr in table.iterfind('tr'):
children = tr.getchildren()
serial = 0
if len(tr.findall('td')) > 0:
td0 = children[0]
a = td0.find('a')
stationName = a.text.strip()
station, created = Station.objects.get_or_create(name=stationName)
timeString = children[1].find('strong').text
hour = int(timeString.split(":")[0].strip()) - 1
mins = int(timeString.split(":")[1][0:2])
ampm = timeString[-2:]
if ampm == 'pm':
hour = hour + 12
stationTime = datetime.time(hour,mins)
st = TrainStation(train=train, station=station, time=stationTime, serial=serial)
st.save()
serial += 1
print "Saved %s" % no

View File

@ -0,0 +1,16 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

View File

@ -0,0 +1 @@
# Create your views here.

View File

@ -98,6 +98,7 @@ MIDDLEWARE_CLASSES = (
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware'
)
ROOT_URLCONF = 'chaloBEST.urls'
@ -120,7 +121,9 @@ INSTALLED_APPS = (
# Uncomment the next line to enable admin documentation:
'django.contrib.gis',
'django_extensions',
'debug_toolbar',
'mumbai',
'mumbaitrains',
# 'django.contrib.admindocs',
)