From b4b83dbbf034ba0cde53f2e811d4c7eb6463da7c Mon Sep 17 00:00:00 2001 From: Charles Holbrow Date: Tue, 29 Jan 2013 20:28:01 -0500 Subject: [PATCH] adding import script for usde schools (req. schemamigration) --- .../apps/courses/management/__init__.py | 1 + .../courses/management/commands/__init__.py | 1 + .../management/commands/import_usde_csv.py | 76 +++++++++++++++++++ karmaworld/apps/courses/models.py | 2 + 4 files changed, 80 insertions(+) create mode 100644 karmaworld/apps/courses/management/__init__.py create mode 100644 karmaworld/apps/courses/management/commands/__init__.py create mode 100644 karmaworld/apps/courses/management/commands/import_usde_csv.py diff --git a/karmaworld/apps/courses/management/__init__.py b/karmaworld/apps/courses/management/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/karmaworld/apps/courses/management/__init__.py @@ -0,0 +1 @@ + diff --git a/karmaworld/apps/courses/management/commands/__init__.py b/karmaworld/apps/courses/management/commands/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/karmaworld/apps/courses/management/commands/__init__.py @@ -0,0 +1 @@ + diff --git a/karmaworld/apps/courses/management/commands/import_usde_csv.py b/karmaworld/apps/courses/management/commands/import_usde_csv.py new file mode 100644 index 0000000..fbbe47c --- /dev/null +++ b/karmaworld/apps/courses/management/commands/import_usde_csv.py @@ -0,0 +1,76 @@ +import csv +from itertools import izip + +from django.core.management.base import BaseCommand +from karmaworld.apps.courses.models import School + + +class Command(BaseCommand): + args = '' + help = ("""Import USDE csv file. add schools to the UsdeSchool model. + Assumes the following header: + Institution_ID,Institution_Name,Institution_Address,Institution_City,Institution_State,Institution_Zip,Institution_Phone,Institution_OPEID,Institution_IPEDS_UnitID,Institution_Web_Address,Campus_ID,Campus_Name,Campus_Address,Campus_City,Campus_State,Campus_Zip,Campus_IPEDS_UnitID,Accreditation_Type,Agency_Name,Agency_Status,Program_Name,Accreditation_Status,Accreditation_Date_Type,Periods,Last Action""" + ) + + def parse_school_csv(self, filename): + """parse a csv file, and return a list of dictionaries + """ + headers = False + schools = [] + + with open(filename) as f: + + reader = csv.reader(f) + headers = reader.next() + for row in reader: + schools.append(row) + + headers = [s.lower() for s in headers] + + return [ dict(izip(headers,school)) for school in schools ] + + def handle(self, *args, **kwargs): + + if len(args) < 1: + self.stdout.write('Provide a filename\n') + return + + filename = args[0] + + school_dicts = self.parse_school_csv(filename) + + self.stdout.write('Importing from list of %d schools\n' % len(school_dicts)) + + count = 0 + + for d in school_dicts: + + if 'institution_id' not in d or not d['institution_id']: + print d + raise Exception('Error: School does not have an institution_id!') + + try: + school = School.objects.get(usde_id=d['institution_id']) + + except School.DoesNotExist: + school = School() + print d['institution_id'] + print d['institution_name'] + count += 1 + + + school.name = d['institution_name'] + school.location = d['institution_city'] + ', ' + d['institution_state'] + school.url = d['institution_web_address'] + school.usde_id = d['institution_id'] + school.save() + + self.stdout.write('Imported %d NEW unique schools\n' % count) + + + + + + + + diff --git a/karmaworld/apps/courses/models.py b/karmaworld/apps/courses/models.py index 000394a..ee24748 100644 --- a/karmaworld/apps/courses/models.py +++ b/karmaworld/apps/courses/models.py @@ -21,6 +21,8 @@ class School(models.Model): url = models.URLField(max_length=511, blank=True) # Facebook keeps a unique identifier for all schools facebook_id = models.BigIntegerField(blank=True, null=True) + # United States Department of Education institution_id + usde_id = models.BigIntegerField(blank=True, null=True) file_count = models.IntegerField(default=0) class Meta: -- 2.25.1