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
import os
import requests
from django.conf import settings
from users.models import User, SchoolClass, Role, UserRole
@ -10,43 +11,56 @@ from users.models import User, SchoolClass, Role, UserRole
class Command(BaseCommand):
def add_arguments(self, parser):
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):
self.stdout.write('Importing from {}!'.format(options['csv_file']))
dir_path = settings.BASE_DIR
rel_path = options['csv_file']
abs_path = os.path.join(dir_path, rel_path)
is_s3 = options['s3']
csv_file = options['csv_file']
try:
with open(abs_path) as f:
reader = csv.DictReader(f)
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()
if not is_s3:
dir_path = settings.BASE_DIR
rel_path = csv_file
abs_path = os.path.join(dir_path, rel_path)
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("")
with open(abs_path) as f:
self.import_users(f)
else:
with requests.Session() as s:
download = s.get(csv_file)
decoded_content = download.content.decode('utf-8')
self.import_users(decoded_content.splitlines())
except Exception as e:
self.stdout.write(e)