adding import script for usde schools (req. schemamigration)
authorCharles Holbrow <charlesholbrow@gmail.com>
Wed, 30 Jan 2013 01:28:01 +0000 (20:28 -0500)
committerCharles Holbrow <charlesholbrow@gmail.com>
Wed, 30 Jan 2013 01:28:01 +0000 (20:28 -0500)
karmaworld/apps/courses/management/__init__.py [new file with mode: 0644]
karmaworld/apps/courses/management/commands/__init__.py [new file with mode: 0644]
karmaworld/apps/courses/management/commands/import_usde_csv.py [new file with mode: 0644]
karmaworld/apps/courses/models.py

diff --git a/karmaworld/apps/courses/management/__init__.py b/karmaworld/apps/courses/management/__init__.py
new file mode 100644 (file)
index 0000000..8b13789
--- /dev/null
@@ -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 (file)
index 0000000..8b13789
--- /dev/null
@@ -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 (file)
index 0000000..fbbe47c
--- /dev/null
@@ -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 = '<filename>'
+    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)
+
+
+
+
+
+
+
+
index 000394ae2b3e5d47fec86e9c8e7e9f11d156d163..ee24748b690df58e4b301495cafad2ff6f72b24d 100644 (file)
@@ -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: