Ejemplo n.º 1
0
def activate():
    """
    Activates a student account and set's there password
    """
    try:
        body = request.get_json()

        activate_token = body["activate_token"]
        password = body["password"]

        if len(password) < 3 or len(password) > 50:
            return bad_request()

        if not models.token_exists(activate_token):

            return bad_request()

        student_hash = models.create_hash(password)
        models.save_hash(student_hash, activate_token)

    except KeyError:
        return bad_request()
    except Exception as e:
        print(e)
        return server_error()

    return created()
Ejemplo n.º 2
0
def create_quiz(course_id):
    """
    Creates a quiz
    """
    try:
        body = request.get_json()

        name = body["name"]
        start_date = body["start_date"]
        end_date = body["end_date"]
        description = body["description"]
        language = body["language"]

        if not models.check_dates(
                start_date, end_date) and not models.check_languages(language):
            return bad_request()

        quiz_id = models.insert_quiz(course_id, name, start_date, end_date,
                                     description, language)

    except KeyError:
        return bad_request()
    except Exception:
        return server_error()
    return created({"quiz_id": quiz_id})
Ejemplo n.º 3
0
def questions(quiz_id):
    """
    Adds one question to a quiz
    """

    try:
        json = request.get_json()

        description = json["description"]

        question_id = models.add_question(quiz_id, description)
    except KeyError:
        return bad_request()
    except Exception as e:
        return server_error()

    return created({"question_id": question_id})
Ejemplo n.º 4
0
def create():
    """
    Creates a class
    """
    try:
        body = request.get_json()

        teacher_id = request.teacher_id
        name = body["name"]

        if not name:
            return bad_request()

        class_id = models.insert_class(teacher_id, name)

    except KeyError:
        return bad_request()
    except Exception:
        return server_error()
    return created({"class_id": class_id})
Ejemplo n.º 5
0
def create_test_case(quiz_id, question_id):
    """
    Creates a test case
    """

    try:
        json = request.get_json()

        test_input = json["test_input"]
        test_expected = json["test_expected"]

        test_id = models.create_test_case(question_id, test_input,
                                          test_expected)
    except (KeyError, ValueError):
        return bad_request()
    except Exception as e:
        print(e)
        return server_error()

    return created({"test_id": test_id})
Ejemplo n.º 6
0
def signup():
    """
    Signs a teacher up and returns their id
    """
    try:
        body = request.get_json()

        name = body["name"]
        email = body["email"]
        password = body["password"]

        # Checks if email is taken in the teacher or student table
        if models.taken_email(email):
            return bad_request()

        teacher_hash = models.create_hash(password)
        teacher_id = models.insert_teacher(name, email, teacher_hash)
    except KeyError:
        return bad_request()
    except Exception:
        return server_error()

    return created({"teacher_id": teacher_id})
Ejemplo n.º 7
0
def add_students(class_id):
    """
    Adds students to the database
    """
    try:
        body = request.get_json()

        teacher_id = request.teacher_id
        students = body["students"]
        emails = []

        for email in students:
            emails.append(email["email"])

        models.insert_students(students, teacher_id)
        models.delete_unique()
        models.insert_into_class(class_id, tuple(emails))

    except KeyError:
        return bad_request()
    except Exception:
        return server_error()
    return created()
Ejemplo n.º 8
0
def create_free_quizzes():
    """
    Creates a free quiz
    """

    try:
        json = request.get_json()

        quiz_name = json["quiz_name"]
        quiz_language_id = json["quiz_language_id"]
        quiz_short_desc = json["quiz_short_desc"]

        models.create_free_quiz(quiz_name, quiz_language_id, quiz_short_desc)
    except KeyError as e:
        print(e)
        return bad_request()
    # Error checking will be done in create_free_quiz route and will return ValueError if so
    except ValueError as e:
        print(e)
        return bad_request()
    except Exception:
        return server_error()

    return created()