5 from django.core.management.base import BaseCommand
6 from django.core.files import File as DjangoFile
8 from karmaworld.apps.notes.models import *
9 # a little messy to import courses in a notes command?
10 from karmaworld.apps.courses.models import *
12 class Command(BaseCommand):
14 help = """Import data to the database from .json. Expect the json in the
15 format exported by the dump_json manage command.
16 The 'all' argument is required. When the 'all' argument is used,
17 import_json expects the following files:
21 If an notes object in notes.json include a 'note_file' key, assume this
22 file can be found relative to the following directory:
25 If the optional 'clean' argument is specified, all notes, courses and
26 files will be deleted from the db before importing
28 When importing notes and courses, it is not currently possible to
29 import ForeignKey values for schools or courses that have already been
30 saved in the db. ForeignKey values within json files may only refer to
31 id values included in the current batch of .json files
34 def handle(self, *args, **kwargs):
36 printl = self.stdout.write
39 printl('specify "all" - assumes schools.json, notes.json and courses.json')
43 for n in Note.objects.all(): n.delete()
44 for c in Course.objects.all(): c.delete()
45 for s in School.objects.all(): s.delete()
48 with open('schools.json', 'r') as f:
49 school_dicts = json.load(f)
51 with open('notes.json', 'r') as f:
52 note_dicts = json.load(f)
54 with open('courses.json', 'r') as f:
55 course_dicts = json.load(f)
57 printl('Schools found: %d\n' % len(school_dicts))
58 printl('Notes found: %d\n' % len(note_dicts))
59 printl('Courses found: %d\n' % len(course_dicts))
61 # Store all the new School orm objects in a dictionary
62 schools_by_old_id = {}
63 courses_by_old_id = {}
66 printl('Importing Schools\n')
67 for school in school_dicts:
72 schools_by_old_id[old_id] = s
75 printl('Importing Courses\n')
76 for course in course_dicts:
77 #printl('Course: ' + course['name'] + '\n')
78 course['updated_at'] = datetime.datetime.utcnow()
79 course['created_at'] = datetime.datetime.utcnow()
81 # remove the old ids from the dict
83 old_school_id = course['school_id']
84 del course['id'] # Have this auto generated
85 del course['school_id'] # use the actual school instead
88 c.school = schools_by_old_id[old_school_id]
90 courses_by_old_id[old_id] = c
93 printl('Importing Notes\n')
94 for note in note_dicts:
96 # These keys cannot be pased as keyword arguments
103 if 'note_file' in note and note['note_file']:
104 note_file = os.path.join('files', note['note_file']) # specify folder for files
105 del note['note_file']
107 # replace the string with this value
108 note['uploaded_at'] = datetime.datetime.utcnow()
110 old_course_id = note['course_id']
112 del note['course_id']
116 n.course = courses_by_old_id[old_course_id]
119 # Add the tags, if any
121 for t in tags: n.tags.add(t)
124 printl(note_file + '\n')
125 with open(note_file) as f:
127 _, file_name = os.path.split(note_file) # careful
128 n.note_file.save(file_name, df)
132 for c in Course.objects.all(): c.update_note_count()
133 for s in School.objects.all(): s.update_note_count()