Example #1
0
def create_quiz():
    '''
        basic creation of quizes publish is a new field so it is checked for before 
        being changed. 
    '''

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

    # checking database for the existance of the quiz the user is trying to create.
    if len(
            list(Quiz.select().where(
                Quiz.userid == user, Quiz.quizname.contains(
                    content['quizname'])))) > 0:
        return Response(status=216)
    new_quiz = Quiz()
    new_quiz.quizname = content['quizname']
    new_quiz.userid = content['userid']

    if "published" in content.keys():
        new_quiz.published = content["published"]

    try:
        new_quiz.save()

    except IntegrityError:
        return Response(status=409)

    return json.dumps(new_quiz.to_dict())
Example #2
0
def get_quizes_by_user(name):
    try:
        query = Quiz.select().where(Quiz.userid == userid)
        query.execute()
        query = list(query)
    except:
        return Response(status=500)
    return json.dumps([quiz.to_dict() for quiz in query])
Example #3
0
def get_published_quizes():
    try:
        query = Quiz.select().where(Quiz.published == True)
        query.execute()
        query = list(query)
    except:
        return Response(status=500)

    return json.dumps([quiz.to_dict() for quiz in query])
Example #4
0
def get_all_quizes():
    try:
        query = Quiz.select()
        query.execute()
        query = list(query)
    except:
        return Response(status=500)

    return json.dumps([quiz.to_dict() for quiz in query])
Example #5
0
def mass_assignment():

    content = format_json(request.get_json())

    try:
        user = User.get(User.id == content['userid'])
    except:
        return Response(status=404)
    quiz = None
    return_quiz = []
    # if the id is not in the json create the quiz this should not be the case the quiz will typically be gotten
    if 'id' not in content.keys():
        pass
        # create the quiz name in the database
        try:
            query = Quiz.select().where(Quiz.quizname == content['quizname'],
                                        Quiz.userid == user)
            query.execute()
            query = list(query)

            if len(query) == 0:
                quiz = Quiz(quizname=content['quizname'], userid=user)
                quiz.save()
            else:
                quiz = query[0]

        except:
            return Response(status=416)
    else:
        try:
            quiz = Quiz.get(Quiz.id == content['id'])
            if quiz.quizname != content['quizname']:
                quiz.quizname = content['quizname']
                try:
                    quiz.save()
                except:
                    return Response(status=403)
        except:
            return Response(status=404)

    if quiz.published == False and int(user.id) == int(quiz.userid.id):
        # if the quiz is not published and the user is not the quiz maker
        # first items will be deleted
        query = QuestionText.select().where(QuestionText.quizid == quiz.id)
        query.execute()
        all_questions = list(query)
        request_questions = [x['text'] for x in content['questions']]
        remove_this = []

        for quest in all_questions:
            if quest.text not in request_questions:
                remove_this.append(quest)

        for quest in remove_this:
            try:
                QuestionAnswer.delete().where(
                    QuestionAnswer.questiontextid == quest.id).execute()
            except:
                return Response(status=433)
            try:
                QuestionText.delete().where(
                    QuestionText.id == quest.id).execute()
            except:
                return Response(status=423)

        for question in content['questions']:
            qt = None

            if user.id == quiz.userid.id:
                #  if they are  they make change the questions

                # then created
                if 'id' not in question.keys():
                    try:
                        query = QuestionText.select().where(
                            QuestionText.text == question['text'],
                            QuestionText.quizid == quiz)
                        query.execute()
                        query = list(query)

                        if len(query) == 0:
                            qt = QuestionText(text=question['text'],
                                              quizid=quiz)
                            qt.save()
                        else:
                            qt = query[0]
                    except:
                        return Response(status=417)
                else:
                    try:
                        qt = QuestionText.get(
                            QuestionText.id == question['id'])
                        qt.text = question['text']
                        qt.save()
                    except:
                        return Response(status=404)
            else:
                # if the user.id is not quiz.id then the question exists
                try:
                    qt = QuestionText.get(QuestionText.id == question['id'])
                except:
                    return Response(status=404)

            # anyone may make changes to their answers
            for answer in question['answers']:
                qa = None
                query = []

                if user.id != quiz.userid.id:
                    query = QuestionAnswer.select().where(
                        QuestionAnswer.user == user,
                        QuestionAnswer.questiontextid == qt)
                    query.execute()
                    query = list(query)

                if len(query) == 0:
                    if 'id' not in answer.keys():
                        try:
                            qa = QuestionAnswer(text=answer['text'],
                                                user=user,
                                                questiontextid=qt)
                            qa.save()
                        except:
                            return Response(status=418)
                    else:
                        try:
                            qa = QuestionAnswer.get(
                                QuestionAnswer.id == answer['id'])
                            qa.text = answer['text']
                            qa.save()
                        except:
                            return Response(status=404)

    return Response(json.dumps(get_full_quizdb(quiz.id)))