Advertisement
Guest User

Untitled

a guest
Dec 31st, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import os
  2. import datetime
  3. from django.core.management.base import BaseCommand
  4. from django.contrib.auth.models import User, Group
  5. from django.conf import settings
  6. from fabric.operations import local
  7.  
  8. import_file = os.path.join(settings.IMPORTER_DATA_DIR, 'somefile.csv')
  9. schema_file = os.path.join(settings.BASE_DIR, 'apps', 'base', 'management', 'commands', 'importer_courses.sql')
  10. import_db = settings.DATABASES['default']['NAME']
  11.  
  12.  
  13. class Command(BaseCommand):
  14.     help = "Drops old course import temp table, recreate, populate from CSV data, create corresponding course records."
  15.  
  16.     def handle(self, *args, **options):
  17.  
  18.         local("psql -d {db} -c 'DROP TABLE IF EXISTS importer_courses'".format(db=import_db))
  19.         local("psql -d {db} -f {schema_file}".format(db=import_db, schema_file=schema_file))
  20.         local('psql -d {db} -c "set client_encoding to \'LATIN1\'; COPY importer_courses (action,course_sec_id,course,section,sec_short_title,sec_desc,sec_start_date,sec_end_date, dept, sec_csxl, sec_term, short_title_formatted) FROM \'{import_file}\' WITH DELIMITER \',\' CSV HEADER"'.format(db=import_db, import_file=import_file))
  21.  
  22. # ...
  23. # You now have a table containing the contents of the CSV. You can use Django's inspectdb to create a standard model from it, and query that model to do interesting things with it.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement