Commit f333257a authored by Martin Mareš's avatar Martin Mareš
Browse files

CLI command for adding a student to a course

Also refactored CLI a little and made search for courses local
in to current semester.
parent 43a60ed7
......@@ -1521,6 +1521,34 @@ def cli_find_user(name):
return users[0]
def cli_find_semester(semester):
if semester is None:
db_query("SELECT * FROM owl_semesters ORDER BY rank DESC LIMIT 1")
semester_row = db.fetchone()
if not semester_row:
cli_die("No semester found")
else:
db_query("SELECT * FROM owl_semesters WHERE ident=%s", (semester,))
semester_row = db.fetchone()
if not semester_row:
cli_die("No such semester")
return semester_row
def cli_find_course(ident):
semester_row = cli_find_semester(None)
db_query("""
SELECT c.*
FROM owl_courses c
WHERE ident=%s AND semid=%s
""", (ident, semester_row.semid))
course = db.fetchone()
if course:
return course
else:
cli_die(f'No course with identifier {course_ident} found')
@app.cli.command("create-course")
@click.argument("ident")
@click.argument("name")
......@@ -1531,21 +1559,11 @@ def cli_create_course(ident, name, teacher, token=None, semester=None):
"""Create a new course. TEACHER is the teacher's full name or email."""
teacher_user = cli_find_user(teacher)
semester_row = cli_find_semester(semester)
if not token:
token = secrets.token_hex(6)
if semester is None:
db_query("SELECT * FROM owl_semesters ORDER BY rank DESC LIMIT 1")
semester_row = db.fetchone()
if not semester_row:
cli_die("No semester found")
else:
db_query("SELECT * FROM owl_semesters WHERE ident=%s", (semester,))
semester_row = db.fetchone()
if not semester_row:
cli_die("No such semester")
db_query("""
INSERT INTO owl_courses(ident, name, enroll_token, semid)
VALUES (%s, %s, %s, %s)
......@@ -1572,15 +1590,7 @@ def cli_add_teacher(course_ident, teacher):
"""Add a teacher to an existing course. TEACHER is the teacher's full name or e-mail."""
teacher_user = cli_find_user(teacher)
db_query("""
SELECT *
FROM owl_courses
WHERE ident=%s
""", (course_ident,))
course = db.fetchone()
if not course:
cli_die(f'No course with identifier {course_ident} found')
course = cli_find_course(course_ident)
db_query("""
INSERT INTO owl_enroll(uid, cid, is_teacher)
......@@ -1591,6 +1601,24 @@ def cli_add_teacher(course_ident, teacher):
db_connection.commit()
@app.cli.command("add-student")
@click.argument("course_ident")
@click.argument("student")
def cli_add_teacher(course_ident, student):
"""Add a student to an existing course. STUDENT is the teacher's full name or e-mail."""
student_user = cli_find_user(student)
course = cli_find_course(course_ident)
db_query("""
INSERT INTO owl_enroll(uid, cid, is_teacher)
VALUES (%s, %s, false)
""",
(student_user.uid, course.cid))
db_connection.commit()
### Sending notifications ###
......
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