From 33e912c2def47b4727645b840d1528047de3fc5c Mon Sep 17 00:00:00 2001 From: Sanj Date: Sat, 25 Feb 2012 00:10:25 +0530 Subject: [PATCH] import schedules, but lots of errors --- indianrails/settings.py | 1 + indianrails/trains/imports.py | 105 ++++++++++++++++++++++++++++++++++ indianrails/trains/models.py | 4 +- requirements.txt | 1 + 4 files changed, 109 insertions(+), 2 deletions(-) diff --git a/indianrails/settings.py b/indianrails/settings.py index 79294ad..998de8f 100644 --- a/indianrails/settings.py +++ b/indianrails/settings.py @@ -101,6 +101,7 @@ INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.humanize', 'django.contrib.gis', + 'django_extensions', 'trains', ) diff --git a/indianrails/trains/imports.py b/indianrails/trains/imports.py index 1218ace..5270f05 100644 --- a/indianrails/trains/imports.py +++ b/indianrails/trains/imports.py @@ -4,6 +4,9 @@ import json from os.path import join import re import datetime +from decimal import Decimal +from django.contrib.gis.geos import Point +import csv2kml def import_stations(): stations = json.load(open(join(DATA_DIR, "stations.json"))) @@ -162,8 +165,110 @@ def getDate(s): date = int(matches[0][1]) return "%02d-%02d" % (monthInt, date,) +def getMinutes(s): + if s is None: + return None + s = s.replace('m', '').strip() + if s == '': + return None + else: + return s + def getBool(s): if s == None or int(s) == 0: return False else: return True + +def import_schedule(): + schedules = json.load(open(join(DATA_DIR, "schedule.json"))) + errors = [] + for s in schedules: + schedule = Schedule() + schedule.data_id = s['id'] + schedule.arrival = getTime(s['arrival']) + schedule.departure = getTime(s['departure']) + schedule.halt = getMinutes(s['halt']) + try: + schedule.stop_number = Decimal(s['stop_number']) + except: + errors.append({ + 'typ': 'Invalid Stop Number', + 'data_id': s['id'], + 'stop_number': s['stop_number'] + }) + schedule.stop_number = Decimal('0') + try: + schedule.station = Station.objects.get(code=s['station_code']) + except: + schedule.station = Station.objects.filter(code=s['station_code'])[0] + errors.append({ + 'typ': 'Invalid or non-unique station code (NOT IMPORTED)', + 'data_id': s['id'], + 'code': s['station_code'] + }) + continue + try: + schedule.train = Train.objects.get(number=s['train_number']) + except: + schedule.train = Train.objects.filter(number=s['train_number'])[0] + errors.append({ + 'typ': 'Invalid or non-unique train code (NOT IMPORTED)', + 'data_id': s['id'], + 'train_number': s['train_number'] + }) + continue + try: + schedule.day = int(s['day']) + except: + errors.append({ + 'typ': 'Day Error', + 'data_id': s['id'], + 'day': s['day'] + }) + schedule.day = 0 + + try: + schedule.distance_travelled = int(s['distance_travelled']) + except: + errors.append({ + 'typ': 'Day Error', + 'data_id': s['id'], + 'distance': s['distance_travelled'] + }) + schedule.distance_travelled = 0 + try: + schedule.save() + except: + errors.append({ + 'typ': 'Something foobarred on Save :(', + 'data_id': s['id'] + }) + errorsFile = open(join(DATA_DIR, "scheduleErrors.json"), "w") + json.dump(errors, errorsFile, indent=2) + errorsFile.close() + +def geolocate_stations(): + errors = [] + for s in Station.objects.filter(point=None)[0:10]: + coords = csv2kml.get_coordinates(s.address) + if coords is not None: + pt = Point(coords[0], coords[1]) + s.point = pt + print s.name + s.save() + else: + errors.append({ + 'typ': 'Failed to geolocate', + 'data_id': s.data_id, + 'address': s.address + }) + errorsFile = open(join(DATA_DIR, "geolocateErrors.json"), "w") + json.dump(errors, errorsFile, indent=2) + errorsFile.close() + +def import_all(): + import_stations() + import_trains() + import_schedules() + diff --git a/indianrails/trains/models.py b/indianrails/trains/models.py index 868d1a5..290add6 100644 --- a/indianrails/trains/models.py +++ b/indianrails/trains/models.py @@ -61,8 +61,8 @@ class Train(models.Model): class Schedule(models.Model): data_id = models.IntegerField() - arrival = models.TimeField() # if blank, get departure time from train models - departure = models.TimeField() + arrival = models.TimeField(null=True) # if blank, get departure time from train models + departure = models.TimeField(null=True) halt = models.IntegerField(null=True) stop_number = models.DecimalField(max_digits=3, decimal_places=2) station = models.ForeignKey(Station) diff --git a/requirements.txt b/requirements.txt index c0f3ccb..eae2e2a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ django==1.3 -e bzr+http://code.0x2620.org/python-ox/#egg=python-ox +-e git://github.com/bit/django-extensions.git#egg=django_extensions