Verified Commit efe9a5e3 authored by Peter Stanko's avatar Peter Stanko
Browse files

Import users using csv

parent c9babed9
......@@ -105,6 +105,17 @@ def cli_users_list():
@users_cli.command('import-csv', help='Import users from the csv')
@click.argument('group', required=False)
@click.option('-f', '--file', help="From which file they should be imported, default is stdin",
def cli_users_list(course, role, group, file):'[CMD] Importing users from the csv users')
manager.import_users(file, course=course, role=role, group=group)
# Courses related commands
import csv
import datetime
import sys
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
......@@ -8,7 +10,7 @@ from import init_dev_data
from import init_prod_data
from import init_test_data
from portal import logger
from portal.database.models import Course, Role, Secret, Submission, SubmissionState, User
from portal.database.models import Course, Group, Role, Secret, Submission, SubmissionState, User
from import RestService
from import time
......@@ -183,4 +185,31 @@ class DataManagement(object):
user =
user.secrets.append(Secret(name='user-cli-secret', value=secret))
log.debug(f'[DATA] Created secret for user: {user.log_name}')
\ No newline at end of file
def import_users(self, file, course=None, role=None, group=None):
if role:
role =, role)
if group:
group =, group)
users = self._parse_csv_users(file)
created_users = []
for user in users:
created =**user, admin=False)
self._add_users_to_course(created_users, role, group)
def _parse_csv_users(self, file=None):
fields = ['username', 'uco', 'email', 'name']
if file is None:
return csv.DictReader(sys.stdin, fieldnames=fields)
with open(file) as f:
return csv.DictReader(f, fieldnames=fields)
def _add_users_to_course(self, created_users, role: Role, group: Group):
for user in created_users:
if role:
if group:
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