Verified Commit 40a4fd97 authored by Peter Stanko's avatar Peter Stanko
Browse files

Ability to import users from csv

parent efe9a5e3
......@@ -6,6 +6,7 @@ from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from import shared
from import init_cpp_data
from import init_dev_data
from import init_prod_data
from import init_test_data
......@@ -45,6 +46,8 @@ class DataManagement(object):
if env == 'test':
if env == 'cpp':
init_cpp_data(, db=self.db)
......@@ -196,16 +199,26 @@ class DataManagement(object):
users = self._parse_csv_users(file)
created_users = []
for user in users:
if['username'], throws=False):
log.debug(f"[IMPORT] skipping already existing user {user['username']}: {user}")
log.debug(f"[IMPORT] Adding user {user['username']}: {user}")
created =**user, admin=False)
self._add_users_to_course(created_users, role, group)
def _parse_csv_users(self, file=None):
def _parse_csv_users(self, file=None) -> list:
fields = ['username', 'uco', 'email', 'name']
def __read_users(reader):
return [user for user in reader]
if file is None:
return csv.DictReader(sys.stdin, fieldnames=fields)
reader = csv.DictReader(sys.stdin, fieldnames=fields)
return __read_users(reader)
with open(file) as f:
return csv.DictReader(f, fieldnames=fields)
reader = csv.DictReader(f, fieldnames=fields)
return __read_users(reader)
def _add_users_to_course(self, created_users, role: Role, group: Group):
for user in created_users:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment