From 1562fbabe6d468a8f3207867c83adc7befb6c9c0 Mon Sep 17 00:00:00 2001 From: Charles Holbrow Date: Wed, 30 Jan 2013 17:11:46 -0500 Subject: [PATCH] adding script in import db from json --- .../courses/management/commands/dump_json.py | 3 +- .../management/commands/import_json.py | 91 +++++++++++++++++++ .../management/commands/import_usde_csv.py | 4 +- 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 karmaworld/apps/courses/management/commands/import_json.py diff --git a/karmaworld/apps/courses/management/commands/dump_json.py b/karmaworld/apps/courses/management/commands/dump_json.py index 85bb11c..c46f679 100644 --- a/karmaworld/apps/courses/management/commands/dump_json.py +++ b/karmaworld/apps/courses/management/commands/dump_json.py @@ -62,6 +62,7 @@ def course_to_dict(course): 'name': course.name, 'slug': course.slug, 'school_id': course.school.id, + 'id': course.id } if course.desc: @@ -105,5 +106,5 @@ class Command(BaseCommand): courses = [course_to_dict(course) for course in Course.objects.all()] fn = 'courses_' + self.date + '.json' with open(fn, 'w') as f: - json.dump(notes, f) + json.dump(courses, f) diff --git a/karmaworld/apps/courses/management/commands/import_json.py b/karmaworld/apps/courses/management/commands/import_json.py new file mode 100644 index 0000000..81e4a43 --- /dev/null +++ b/karmaworld/apps/courses/management/commands/import_json.py @@ -0,0 +1,91 @@ +import json +import os.path +import datetime + +from django.core.management.base import BaseCommand +from django.core.files import File as DjangoFile + +from karmaworld.apps.notes.models import * +# a little messy to import courses in a notes command? +from karmaworld.apps.courses.models import * + +class Command(BaseCommand): + + def handle(self, *args, **kwargs): + + printl = self.stdout.write + + if 'all' not in args: + printl('specify "all" - assumes schools.json, notes.json and courses.json') + return + + if 'clean' in args: + for n in Note.objects.all(): n.delete() + for c in Course.objects.all(): c.delete() + for s in School.objects.all(): s.delete() + + # read json files + with open('schools.json', 'r') as f: + school_dicts = json.load(f) + + with open('notes.json', 'r') as f: + note_dicts = json.load(f) + + with open('courses.json', 'r') as f: + course_dicts = json.load(f) + + printl('Schools found: %d\n' % len(school_dicts)) + printl('Notes found: %d\n' % len(note_dicts)) + printl('Courses found: %d\n' % len(course_dicts)) + + #Schools + printl('Importing Schools\n') + for school in school_dicts: + s = School(**school) + s.save() + + # Courses + printl('Importing Courses\n') + for course in course_dicts: + #printl('Course: ' + course['name'] + '\n') + course['updated_at'] = datetime.datetime.utcnow() + course['created_at'] = datetime.datetime.utcnow() + + c = Course(**course) + c.save() + + # Notes + printl('Importing Notes\n') + for note in note_dicts: + + # These keys cannot be pased as keyword arguments + tags = None + if 'tags' in note: + tags = note['tags'] + del note['tags'] + + file_path = None + if 'file_path' in note and note['file_path']: + file_path = os.path.join('files', note['file_path']) # specify folder for files + del note['file_path'] + + # replace the string with this value + note['uploaded_at'] = datetime.datetime.utcnow() + + n = Note(**note) + + # Add the tags, if any + if tags: + for t in tags: n.tags.add(t) + + if file_path: + with open(file_path) as f: + df = DjangoFile(f) + _, file_name = os.path.split(file_path) # careful + n.note_file.save(file_name, df) + + n.save() + + for c in Course.objects.all(): c.update_note_count() + for s in School.objects.all(): s.update_note_count() + diff --git a/karmaworld/apps/courses/management/commands/import_usde_csv.py b/karmaworld/apps/courses/management/commands/import_usde_csv.py index fbbe47c..47a0ea0 100644 --- a/karmaworld/apps/courses/management/commands/import_usde_csv.py +++ b/karmaworld/apps/courses/management/commands/import_usde_csv.py @@ -54,8 +54,8 @@ class Command(BaseCommand): except School.DoesNotExist: school = School() - print d['institution_id'] - print d['institution_name'] + #print d['institution_id'] + #print d['institution_name'] count += 1 -- 2.25.1