def edit_question(question_id):
    """ (str) -> rendered_template

    Returns the rendered template of admin-questions.html with data from
    user input into the Question object, after the user makes a POST
    request to 'admin/question/<question_id>'
    """
    if request.form['submit'] == 'Save':
        question = GetQuestion(ObjectId(question_id)).get()
        question.question = request.form['form-question']
        question.category = request.form['form-category'].split(", ")
        question.solution = request.form['form-solution'].split("\r\n")
        question.input_description = request.form['form-input']
        question.output_description = request.form['form-output']
        question.test_cases = request.form['form-test'].split("\r\n")
        question.difficulty = int(request.form['form-difficulty'])

        code = '\n'.join(question.solution)
        output = run_code(code, question.test_cases)
        if len(output):
            return 'There was an error with your test cases:<br><br>' + output + '<br><br> Press Back to go back.', 400
        UpdateQuestion(question).post()
    elif request.form['submit'] == 'Delete':
        db.questions.remove(ObjectId(question_id))
    return redirect(url_for('get_question_list'))
def edit_question(question_id):
    """ (str) -> rendered_template

    Returns the rendered template of admin-questions.html with data from
    user input into the Question object, after the user makes a POST
    request to 'admin/question/<question_id>'
    """
    if request.form['submit'] == 'Save':
        question = GetQuestion(ObjectId(question_id)).get()
        question.question = request.form['form-question']
        question.category = request.form['form-category'].split(", ")
        question.solution = request.form['form-solution'].split("\r\n")
        question.input_description = request.form['form-input']
        question.output_description = request.form['form-output']
        question.test_cases = request.form['form-test'].split("\r\n")
        question.difficulty = int(request.form['form-difficulty'])

        code = '\n'.join(question.solution)
        output = run_code(code, question.test_cases)
        if len(output):
            return 'There was an error with your test cases:<br><br>' + output + '<br><br> Press Back to go back.', 400
        UpdateQuestion(question).post()
    elif request.form['submit'] == 'Delete':
        db.questions.remove(ObjectId(question_id))
    return redirect(url_for('get_question_list'))
def upload_code():
    """ () -> rendered_template

    Returns the rendered template of admin-questions.html with data from user input
    into the Question object, after the user makes a POST request to 'admin/questions'
    """
    question_check = MongoConfig.question.find({'question': request.form['form-question']}).count() > 0
    if question_check:
        return render_template('upload.html')
    else:

        solution = request.form['form-solution'].split('\r\n')
        solution = list(filter(None, solution))

        scramble_order = list(range(len(solution)))
        shuffle(scramble_order)

        tests = request.form['form-test'].split('\r\n')
        [x.strip() for x in tests]

        category = request.form['form-category'].split(',')
        [x.strip() for x in category]

        question = Question(
            ObjectId(),
            request.form['form-question'],
            solution,
            scramble_order,
            tests,
            request.form['form-input'],
            request.form['form-output'],
            category,
            request.form['form-difficulty']
        )

        code = '\n'.join(question.solution)

        output = run_code(code, question.test_cases)
        if len(output):
            return 'There was an error with your test cases:\n' + output + '\n Press Back to go back.', 400

        CreateQuestion(question).populate()
        return redirect(url_for('get_questions'))
def upload_code():
    """ () -> rendered_template

    Returns the rendered template of admin-questions.html with data from user input
    into the Question object, after the user makes a POST request to 'admin/questions'
    """
    question_check = MongoConfig.question.find({
        'question':
        request.form['form-question']
    }).count() > 0
    if question_check:
        return render_template('upload.html')
    else:

        solution = request.form['form-solution'].split('\r\n')
        solution = list(filter(None, solution))

        scramble_order = list(range(len(solution)))
        shuffle(scramble_order)

        tests = request.form['form-test'].split('\r\n')
        [x.strip() for x in tests]

        category = request.form['form-category'].split(',')
        [x.strip() for x in category]

        question = Question(ObjectId(), request.form['form-question'],
                            solution, scramble_order, tests,
                            request.form['form-input'],
                            request.form['form-output'], category,
                            request.form['form-difficulty'])

        code = '\n'.join(question.solution)

        output = run_code(code, question.test_cases)
        if len(output):
            return 'There was an error with your test cases:\n' + output + '\n Press Back to go back.', 400

        CreateQuestion(question).populate()
        return redirect(url_for('get_questions'))