Update import command to allow S3 resources as source
This commit is contained in:
parent
4965eb5fa1
commit
e7916215b0
|
|
@ -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,15 +11,10 @@ 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 handle(self, *args, **options):
|
def import_users(self, file_handler):
|
||||||
self.stdout.write('Importing from {}!'.format(options['csv_file']))
|
reader = csv.DictReader(file_handler)
|
||||||
dir_path = settings.BASE_DIR
|
|
||||||
rel_path = options['csv_file']
|
|
||||||
abs_path = os.path.join(dir_path, rel_path)
|
|
||||||
try:
|
|
||||||
with open(abs_path) as f:
|
|
||||||
reader = csv.DictReader(f)
|
|
||||||
for row in reader:
|
for row in reader:
|
||||||
email = row['Email'].lower().strip()
|
email = row['Email'].lower().strip()
|
||||||
if email == '':
|
if email == '':
|
||||||
|
|
@ -48,5 +44,23 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
self.stdout.write("")
|
self.stdout.write("")
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
self.stdout.write('Importing from {}!'.format(options['csv_file']))
|
||||||
|
is_s3 = options['s3']
|
||||||
|
csv_file = options['csv_file']
|
||||||
|
try:
|
||||||
|
if not is_s3:
|
||||||
|
dir_path = settings.BASE_DIR
|
||||||
|
rel_path = csv_file
|
||||||
|
abs_path = os.path.join(dir_path, rel_path)
|
||||||
|
|
||||||
|
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:
|
except Exception as e:
|
||||||
self.stdout.write(e)
|
self.stdout.write(e)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue