Пример #1
0
def inject_answers():
    content = request.get_json()

    # get quiz
    try:
        quiz = Quiz.get(Quiz.id == content['id'])
    except:
        return Response(status=404)

    # get user
    try:
        user = User.get(User.id == content['userid'])
    except:
        return Response(status=404)

    # not the owner and quiz is published
    for question in content['questions']:
        # get the questions
        try:
            qt = QuestionText.get(QuestionText.id == question['id'])
        except:
            return Response(status=414)

        for answer in question['answers']:
            # if the answers does not have an id then make a full answer
            user_answers = [
                x for x in QuestionAnswer.select().where(
                    QuestionAnswer.user == user, QuestionAnswer.questiontextid
                    == qt)
            ]

            if 'id' not in answer.keys() and len(user_answers) == 0:
                try:
                    qa = QuestionAnswer(text=answer['text'],
                                        user=user,
                                        questiontextid=qt)
                    qa.save()
                    return_answer = qa.to_dict()
                except:
                    return Response(status=418)
            elif 'id' not in answer.keys():
                try:
                    qa = user_answers[0]
                    qa.text = answer['text']
                    qa.save()
                    return_answer = qa.to_dict()
                except:
                    return Response(status=444)

    return json.dumps(get_full_quizdb(quiz.id))
Пример #2
0
def create_questionanswer():

    content = format_json(request.get_json())

    # this is the answe that is getting modified
    answer = QuestionAnswer()

    # this finds the user sending the request and the information in the request
    user = User.get(User.id == content['userid'])
    qtext = QuestionText.get(QuestionText.id == content['questiontextid'])
    quiz = Quiz.get(Quiz.id == qtext.quizid)

    if quiz.userid != user.id:
        quiz_creator = User.get(User.id == quiz.userid)

        # this check needs to be here so
        # the user may not be able to set their answer to true.
        # this will set their answer to right or not.
        # providing instant feedback

        for x in QuestionAnswer.select().where(
            (QuestionAnswer.user_id == quiz_creator)
                & (QuestionAnswer.isRight == True)):

            if x.to_dict()['text'].lower() == content['answertext']:
                answer.isRight = True
                break

    else:
        # the question creator and the answer are the same thust they may change what they want.
        if 'isright' in content.keys():

            if content['isright'] == True:
                answer.isRight = True

            elif content['isright'] == False:
                answer.isRight = False

    answer.text = content['answertext']
    answer.user = user
    answer.questiontextid = qtext
    answer.save()

    return json.dumps(answer.to_dict())