def delete_full_quiz(id): if id != None: # create the quiz name in the database try: quiz = Quiz.get(Quiz.id == id) except: return Response(status=416) else: return Response(status=404) # first deleting all the questions for qt in QuestionText.select().where(QuestionText.quizid == quiz.id): try: QuestionAnswer.delete().where( QuestionAnswer.questiontextid == qt.id).execute() except: return Response(status=403) # then deleting the questiontext try: QuestionText.delete().where(QuestionText.quizid == quiz.id).execute() except: return Response(status=403) # finally deleting the quiz delete_id = quiz.id try: Quiz.delete().where(Quiz.id == id).execute() except: return Response(status=403) return Response(status=200)
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)))