tweaking json db import script
authorCharles Holbrow <charlesholbrow@gmail.com>
Wed, 23 Jan 2013 21:11:22 +0000 (16:11 -0500)
committerCharles Holbrow <charlesholbrow@gmail.com>
Wed, 23 Jan 2013 21:11:22 +0000 (16:11 -0500)
bin/import_json/run.py

index fd98853c5d7167ece8bf009a3236a8dd2f2f4495..43c2ce7e2e19e805aeaf6d3fb6eb55a864ec03ce 100644 (file)
@@ -61,12 +61,16 @@ for note in note_dicts:
        fix_file_path(note)
 
 
+
 # Import from json 
 print 'updating %i schools' % len(school_dicts)
 for school in school_dicts:
        s = School(**school)
        s.save()
 
+# Only Save this scool if we actually need it. 
+arbitrary_school = School(name='No School', slug='no_school')
+
 print 'updating %i courses' % len(course_dicts)
 for course in course_dicts:
        course['updated_at'] = datetime.datetime.utcnow()
@@ -75,7 +79,8 @@ for course in course_dicts:
        # Somc courses have no school_id using arbitrary one for these
        if not course['school_id']: 
                print 'Using arbitrary school_id for course id:', course['id'], '-', course['name']
-               course['school_id'] = School.objects.all()[0].id
+               course['school_id'] = arbitrary_school.id
+               arbitrary_school.save()
 
        c = Course(**course)
        c.save()
@@ -83,11 +88,16 @@ for course in course_dicts:
 
 # Import the Notes
 print 'updating %i notes' % len(note_dicts)
+
+arbitrary_course = Course(name='No Course', slug='no_course', school=arbitrary_school)
+
 for note in note_dicts:
 
        if not note['course_id']:
                print 'using arbitrary course id for note_id:', note['id'], '-', note['name']
-               note['course_id'] = Course.objects.all()[0].id
+               note['course_id'] = arbitrary_course.id
+               arbitrary_school.save()
+               arbitrary_course.save()
 
        if 'slug' not in note:
                path, fn = split(note['file_path'])