def _createModel(kind,parent = None): value = id_or_name(parent) key_name = None if kind == "route": agency = None if parent: agency = db.Key.from_path(Agency.kind() , value) return Route(key_name = key_name,agency = agency) elif kind == "agency": return Agency(key_name = key_name) elif kind == "trip": route = None if parent: route = db.Key.from_path(Route.kind() , value) return Trip(route = route , key_name = key_name) elif kind == "stop": return Stop() elif kind =="faretrip": trip = None if parent: trip = db.Key.from_path(Trip.kind(),value) return FareTrip(trip = trip , key_name = key_name) raise ValueError
def clean(self, value): if self.required and not value: raise forms.ValidationError(_(u'This field is required.')) elif not self.required and not value: return [] if isinstance(value,basestring): value = value.split(",") if not isinstance(value, (list, tuple)): raise ValidationError(gettext(u'Enter a list of values.')) final_values = [] for val in value: key = db.Key.from_path(Stop.kind(),id_or_name(val)) final_values.append(key) return final_values
def clean(self, value): final_value = None if self.required and not value: raise forms.ValidationError(_(u'This field is required.')) elif not self.required and not value: return None if isinstance(value,basestring): id = id_or_name(value) final_value = db.Key.from_path(self.___model_class.kind(),id) try: object = getCachedObjectOr404(self.___model_class,key = final_value) except Http404: raise forms.ValidationError(_(u'This record not found.')) return final_value
def run(fare_stop_id, mapping_file,input_file,col): """ @param fare_stop_id @param mapping_file The mapping file generated by find-stations.py @input_file A CSV file of fare data @col The column of fare """ from gogogo.models import FareStop , FarePair , Stop from gogogo.models.utils import id_or_name from google.appengine.ext import db from django.utils import simplejson from gogogo.views.db.forms import next_key_name from django.utils import simplejson key = db.Key.from_path(FareStop.kind(),id_or_name(fare_stop_id)) fare_stop = FareStop.get(key) if fare_stop == None: print "Error! fare_stop_id(%s) not found!" % fare_stop_id return file = codecs.open(mapping_file ,"rt","utf-8") data ="" for line in file: data+=line mapping = simplejson.loads(data) reader = UnicodeReader(open(input_file)) col = int(col) first = True to_save = [] print "Loading from database...." from_stop_property = getattr(FarePair, "from_stop") to_stop_property = getattr(FarePair, "to_stop") fares = [] for row in reader: if first: first = False continue try: #print row[0],row[1] try: fare = float(row[col]) except ValueError: print "Warning. Value Error for %s to %s = %s" % (row[0],row[1],row[col]) continue from_stop_id = mapping[row[0]][0] to_stop_id = mapping[row[1]][0] entry = { "from" : from_stop_id, "to" : to_stop_id, "fare" : fare } fares.append(entry) except KeyError,e: print "Station not found! : %s" % str(e)