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')
@click.argument('name')
def cli_users_promote(name):
log.info(f"[CMD] Promote user to admin: {name}")
manager.promote_user(name)
@users_cli.command('add-secret', help='Adds secret for the user')
@click.argument('name')
@click.option('-s', '--secret', help='Users password', prompt=True, hide_input=True)
......@@ -88,6 +95,7 @@ def cli_users_add_secret(name, secret):
log.info(f"[CMD] Add secret for the user: {name} ({secret})")
manager.add_user_secret(name, secret)
@users_cli.command('delete', help="Deletes user")
@click.argument('name')
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', )
@click.argument('name')
def cli_course_creates(name):
log.info(f"[CMD] Create Course: {name}")
manager.create_course(name)
......@@ -133,6 +141,23 @@ def cli_courses_list():
manager.list_courses()
@courses_cli.command('rm', help="Delete course")
@click.argument('name')
def cli_courses_rm(name):
log.info(f"[CMD] Remove Course: {name}")
manager.delete_course(name)
@courses_cli.command('add-user', help="Add user")
@click.argument('user')
@click.argument('course')
@click.argument('role')
@click.argument('group', required=False)
def cli_courses_add_user(user, course, role, group):
log.info(f"[CMD] Add user {user} to course course {course} as {role} in {group}")
manager.add_user_to_course(user, course, role=role, group=group)
@courses_cli.command(
'create-role', help="Creates role by it's type in the course")
@click.argument('course')
......
......@@ -11,7 +11,7 @@ from management.data.data_dev import init_dev_data
from management.data.data_prod import init_prod_data
from management.data.data_test 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 portal.service.rest import RestService
from portal.tools import time
......@@ -54,6 +54,15 @@ class DataManagement(object):
user = self.rest.find.user(name)
self.rest.users(user).delete()
def delete_course(self, name: str):
"""Deletes course
Args:
name(str): name of the course
"""
with self.app.app_context():
course = self.rest.find.course(name)
self.rest.courses(course).delete()
def update_users_password(self, name: str, password: str) -> User:
"""Updates user's password
Args:
......@@ -186,23 +195,26 @@ class DataManagement(object):
def import_users(self, file, course=None, role=None, group=None):
with self.app.app_context():
course = self.rest.find.course(course)
if role:
role = self.rest.find.role(course, role)
if group:
group = self.rest.find.group(course, group)
users = self._parse_csv_users(file)
created_users = []
for user in users:
found_user = self.rest.find.user(user['username'], throws=False)
if found_user:
if not found_user:
log.debug(f"[IMPORT] Adding user {user['username']}: {user}")
found_user = self.rest.users.create(**user, admin=False)
else:
log.debug(f"[IMPORT] skipping already existing user {user['username']}: {user}")
created_users.append(found_user)
continue
log.debug(f"[IMPORT] Adding user {user['username']}: {user}")
created = self.rest.users.create(**user, admin=False)
created_users.append(created)
self._add_users_to_course(created_users, role, group)
created_users.append(found_user)
self._add_users_to_course(created_users, course, role, group)
def _get_role_group_instance(self, course, group, role):
course = self.rest.find.course(course)
if role:
role = self.rest.find.role(course, role)
if group:
group = self.rest.find.group(course, 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:
self.rest.roles(role).add_client(user)
if group:
self.rest.groups(group).add_user(user)
self.add_user_to_course(user, course, role, group)
def promote_user(self, name):
with self.app.app_context():
user = self.rest.find.user(name)
user.is_admin = True
self.rest.users.write_entity(user)
def add_user_to_course(self, user, course, role, group=None):
user = self.rest.find.user(user)
group, role = self._get_role_group_instance(course, group, role)
if role:
self.rest.roles(role).add_client(user)
if group:
self.rest.groups(group).add_user(user)
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