Update import command to allow S3 resources as source

This commit is contained in:
Ramon Wenger 2019-11-20 15:05:35 +01:00
parent 4965eb5fa1
commit e7916215b0
1 changed files with 46 additions and 32 deletions

View File

@ -2,6 +2,7 @@ import csv
from django.core.management import BaseCommand from django.core.management import BaseCommand
import os import os
import requests
from django.conf import settings from django.conf import settings
from users.models import User, SchoolClass, Role, UserRole from users.models import User, SchoolClass, Role, UserRole
@ -10,43 +11,56 @@ from users.models import User, SchoolClass, Role, UserRole
class Command(BaseCommand): class Command(BaseCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('csv_file') parser.add_argument('csv_file')
parser.add_argument('--s3', dest='s3', action='store_const', default=False, const=True, help='the file is on a remote server and publicly accessible like e.g. an S3 resource')
def import_users(self, file_handler):
reader = csv.DictReader(file_handler)
for row in reader:
email = row['Email'].lower().strip()
if email == '':
self.stdout.write('No e-mail set, skipping')
continue
school_class_names = [c.strip() for c in row['Klassen'].split(',')]
first_name = row['Vorname'].strip()
last_name = row['Nachname'].strip()
self.stdout.write("Creating user {} {}, {}".format(first_name, last_name, email))
user = User.objects.create_user_with_random_password(first_name, last_name, email)
if row['Rolle'] == 'Lehrer':
self.stdout.write("Assigning teacher role")
teacher = Role.objects.get(key='teacher')
UserRole.objects.get_or_create(user=user, role=teacher)
else:
self.stdout.write("Assigning student role")
student = Role.objects.get(key='teacher')
UserRole.objects.get_or_create(user=user, role=student)
self.stdout.write("Adding to class(es) {}".format(', '.join(school_class_names)))
for school_class_name in school_class_names:
school, _ = SchoolClass.objects.get_or_create(name=school_class_name)
user.school_classes.add(school)
self.stdout.write("")
def handle(self, *args, **options): def handle(self, *args, **options):
self.stdout.write('Importing from {}!'.format(options['csv_file'])) self.stdout.write('Importing from {}!'.format(options['csv_file']))
dir_path = settings.BASE_DIR is_s3 = options['s3']
rel_path = options['csv_file'] csv_file = options['csv_file']
abs_path = os.path.join(dir_path, rel_path)
try: try:
with open(abs_path) as f: if not is_s3:
reader = csv.DictReader(f) dir_path = settings.BASE_DIR
for row in reader: rel_path = csv_file
email = row['Email'].lower().strip() abs_path = os.path.join(dir_path, rel_path)
if email == '':
self.stdout.write('No e-mail set, skipping')
continue
school_class_names = [c.strip() for c in row['Klassen'].split(',')]
first_name = row['Vorname'].strip()
last_name = row['Nachname'].strip()
self.stdout.write("Creating user {} {}, {}".format(first_name, last_name, email)) with open(abs_path) as f:
self.import_users(f)
user = User.objects.create_user_with_random_password(first_name, last_name, email) else:
with requests.Session() as s:
if row['Rolle'] == 'Lehrer': download = s.get(csv_file)
self.stdout.write("Assigning teacher role") decoded_content = download.content.decode('utf-8')
teacher = Role.objects.get(key='teacher') self.import_users(decoded_content.splitlines())
UserRole.objects.get_or_create(user=user, role=teacher)
else:
self.stdout.write("Assigning student role")
student = Role.objects.get(key='teacher')
UserRole.objects.get_or_create(user=user, role=student)
self.stdout.write("Adding to class(es) {}".format(', '.join(school_class_names)))
for school_class_name in school_class_names:
school, _ = SchoolClass.objects.get_or_create(name=school_class_name)
user.school_classes.add(school)
self.stdout.write("")
except Exception as e: except Exception as e:
self.stdout.write(e) self.stdout.write(e)