Example #1
0
def create_question():
    """Creates and returns the question that adheres to the content
    of the request. If there is an error, an error response will be
    returned instead.
    """
    data = json.loads(request.data)
    if not ('text' in data):
        raise Exception('Error: Question text is required.')
    if not ('answer' in data):
        raise Exception('Error: Question answer is required.')
    if not ('choices' in data):
        raise Exception('Error: Question answer choices are required.')

    #validate question? yeesh... let's keep it simple for now...
    question = Question(text=str(data['text']), answer=str(data['answer']), choices=str(data['choices']))
    db.session.add(question)
    db.session.commit()
    return json_response(question.to_dict(), 201)