def handle(self, *args, **options): # load all the data from fixtures path = os.path.join(os.path.dirname(campus.__file__), 'fixtures') # buildings f = open(os.path.join(path, 'buildings.json'), 'r') txt = f.read() f.close() buildings = json.loads(txt) for b in buildings: b['fields']['id'] = b['pk'] new = Building.objects.create(**b['fields']) print new.id, new.name # regional campuses f = open(os.path.join(path, 'campuses.json'), 'r') txt = f.read() f.close() objects = json.loads(txt) for o in objects: o['fields']['id'] = o['pk'] new = RegionalCampus.objects.create(**o['fields']) print new.id, new.name # locations f = open(os.path.join(path, 'locations.json'), 'r') txt = f.read() f.close() objects = json.loads(txt) for o in objects: o['fields']['id'] = o['pk'] new = Location.objects.create(**o['fields']) print new.id, new.name # parking lots f = open(os.path.join(path, 'parkinglots.json'), 'r') txt = f.read() f.close() objects = json.loads(txt) for o in objects: o['fields']['id'] = "parkinglot-%s" % o['pk'] new = ParkingLot.objects.create(**o['fields']) print new.id, new.name # emergency phones f = open(os.path.join(path, 'phones.json'), 'r') txt = f.read() f.close() objects = json.loads(txt) for o in objects: o['fields']['id'] = "phone-%s" % o['pk'] new = EmergencyPhone.objects.create(**o['fields']) print new.id, new.name # bike racks f = open(os.path.join(path, 'bikeracks.json'), 'r') txt = f.read() f.close() objects = json.loads(txt) for o in objects: o['fields']['id'] = "bikerack-%s" % o['pk'] new = BikeRack.objects.create(**o['fields']) print new.id, new.name create_groupable_locations() # Groups f = open(os.path.join(path, 'groups.json'), 'r') txt = f.read() f.close() objects = json.loads(txt) for o in objects[:]: o['fields']['id'] = o['pk'] locations = o['fields'].pop('locations') new = Group.objects.create(**o['fields']) print new.id for l in locations: print "adding %s" % l, gl = GroupedLocation.objects.get_by_natural_key(l[0], l[1]) print gl new.locations.add(gl)
def handle(self, *args, **options): print "Crunching datas:" ''' Reset Campus "manage.py reset" has been woefull deprecated, so no: call_command('reset', 'campus', verbosity=0, interactive=False) Ran into issue with Windows The SQL generated from sqlclear always failed (an issue with the names of foriegn key constraints). If the SQL is ran twice, the tables with dependencies will be removed first and independent tables removed second Future compatibility issues: sqlclear and reset both generate sql based on models in the project. Sometimes we have to jump back/forth between map versions and the models/tablenames between the two are not yet knows. The sql is now generated directly from in information schema ''' for i in range(2): sql = self.reset_sql() error = self.run_query(sql) if error: print "Failed to update the db :(" print type(error) print error return #syncdb call_command('syncdb', verbosity=0, interactive=False) #south migrate if options.get('test') is not True: call_command('migrate', verbosity=0, interactive=False) # load all the data from fixtures path = os.path.join(os.path.dirname(campus.__file__), 'fixtures') fixtures = os.listdir(path) fixtures.sort() for f in fixtures: m = re.match(r"(?P<fixture>\w+)\.json", f) if m: fixture = m.group('fixture') if fixture == "groups": continue # skip groups, must run last print " Updating %s ..." % fixture call_command('loaddata', fixture, verbosity=0, interactive=False) # Groups # for the m2m relation, create all GroupedLocation instances # had to wait until all locations and contenttypes initiated print " Updating groups ..." with open(os.path.join(path, 'groups.json'), 'r') as f: txt = f.read() groups = json.loads(txt) for g in groups[:]: locations = g['fields'].pop('locations') qs = QuerySet(MapObj) mob = qs.get(id=g['pk']) mob = mob.__dict__ mob.pop('_state') Group.objects.create(**mob) ''' when / if groups get additional attributes, will have to extend importer here for k,v in g['fields']: setattr(new, k, v) ''' g['fields']['locations'] = locations sys.stdout.write(" Updating content types ") create_groupable_locations(verbosity=1) sys.stdout.flush() print # Create/Update DiningLocations from search service print ' Updating dining ...' DiningLocation.refresh() sys.stdout.write(" Updating m2m locations ") for g in groups[:]: group = Group.objects.get(id = g['pk']) locations = g['fields']['locations'] count = 0 for l in locations: gl = GroupedLocation.objects.get_by_natural_key(l[0], l[1]) group.locations.add(gl) # too many dots, otherwise if count % 3 == 0: sys.stdout.write(".") sys.stdout.flush() count = count +1 print print "All done. The map nom'd all the data and is happy."