Verified Commit 0fce7c66 authored by Peter Stanko's avatar Peter Stanko
Browse files

Added new command - promote, delete course and add user to course

parent bf4588ba
Pipeline #17365 passed with stage
in 8 minutes and 31 seconds
......@@ -81,6 +81,13 @@ def cli_users_set_password(name, password):
manager.update_users_password(name, password)
@users_cli.command('promote', help='Promote user to the admin')
def cli_users_promote(name):"[CMD] Promote user to admin: {name}")
@users_cli.command('add-secret', help='Adds secret for the user')
@click.option('-s', '--secret', help='Users password', prompt=True, hide_input=True)
......@@ -88,6 +95,7 @@ def cli_users_add_secret(name, secret):"[CMD] Add secret for the user: {name} ({secret})")
manager.add_user_secret(name, secret)
@users_cli.command('delete', help="Deletes user")
def cli_users_delete_user(name):
......@@ -121,7 +129,7 @@ def cli_users_list(course, role, group, file):
@courses_cli.command('create', help="Creates course with default roles")
@click.argument('name', )
def cli_course_creates(name):"[CMD] Create Course: {name}")
......@@ -133,6 +141,23 @@ def cli_courses_list():
@courses_cli.command('rm', help="Delete course")
def cli_courses_rm(name):"[CMD] Remove Course: {name}")
@courses_cli.command('add-user', help="Add user")
@click.argument('group', required=False)
def cli_courses_add_user(user, course, role, group):"[CMD] Add user {user} to course course {course} as {role} in {group}")
manager.add_user_to_course(user, course, role=role, group=group)
'create-role', help="Creates role by it's type in the course")
......@@ -11,7 +11,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, Group, Role, Secret, Submission, SubmissionState, User
from portal.database.models import Course, Role, Secret, Submission, SubmissionState, User
from import RestService
from import time
......@@ -54,6 +54,15 @@ class DataManagement(object):
user =
def delete_course(self, name: str):
"""Deletes course
name(str): name of the course
course =
def update_users_password(self, name: str, password: str) -> User:
"""Updates user's password
......@@ -186,23 +195,26 @@ class DataManagement(object):
def import_users(self, file, course=None, role=None, group=None):
course =
if role:
role =, role)
if group:
group =, group)
users = self._parse_csv_users(file)
created_users = []
for user in users:
found_user =['username'], throws=False)
if found_user:
if not found_user:
log.debug(f"[IMPORT] Adding user {user['username']}: {user}")
found_user =**user, admin=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)
self._add_users_to_course(created_users, course, role, group)
def _get_role_group_instance(self, course, group, role):
course =
if role:
role =, role)
if group:
group =, group)
return group, role
def _parse_csv_users(self, file=None) -> list:
fields = ['username', 'uco', 'email', 'name']
......@@ -217,9 +229,22 @@ class DataManagement(object):
reader = csv.DictReader(f, fieldnames=fields)
return __read_users(reader)
def _add_users_to_course(self, created_users, role: Role, group: Group):
def _add_users_to_course(self, created_users, course, role, group):
for user in created_users:
if role:
if group:
self.add_user_to_course(user, course, role, group)
def promote_user(self, name):
user =
user.is_admin = True
def add_user_to_course(self, user, course, role, group=None):
user =
group, role = self._get_role_group_instance(course, group, role)
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