Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import datetime
- from django.core.management.base import BaseCommand
- from django.contrib.auth.models import User, Group
- from django.conf import settings
- from fabric.operations import local
- import_file = os.path.join(settings.IMPORTER_DATA_DIR, 'somefile.csv')
- schema_file = os.path.join(settings.BASE_DIR, 'apps', 'base', 'management', 'commands', 'importer_courses.sql')
- import_db = settings.DATABASES['default']['NAME']
- class Command(BaseCommand):
- help = "Drops old course import temp table, recreate, populate from CSV data, create corresponding course records."
- def handle(self, *args, **options):
- local("psql -d {db} -c 'DROP TABLE IF EXISTS importer_courses'".format(db=import_db))
- local("psql -d {db} -f {schema_file}".format(db=import_db, schema_file=schema_file))
- 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))
- # ...
- # 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