def post(self, request): u_id = request.data["unique_id"] try: quiz = Quiz.objects.get(unique_id=u_id) questions = quiz.questions.all() analyses = quiz.analyse_set.all() serializer = QuestionSerializer(data=request.data["questions"], many=True) numberOfQuestions = len(questions) * 3 score = 0 if serializer.is_valid(): answers = serializer.data i = 0 for question in answers: if question["kind"] == "1": alanyse = analyses[i] answer = questions[i].answer if question["answer"] == 0: alanyse.unanswered += 1 alanyse.save() i += 1 continue if question["answer"] == answer: alanyse.answered += 1 alanyse.save() score += 3 i += 1 if question["answer"] != answer: alanyse.wrong += 1 alanyse.save() score -= 1 i += 1 answer = Answer() answer.quiz = quiz answer.text = question["answer"] answer.student = Student.objects.get(username=request.user.username) answer.save() if question["kind"] == "2": answer = Answer() answer.quiz = quiz answer.text = question["answer"] answer.student = Student.objects.get(username=request.user.username) answer.save() result = score / numberOfQuestions * 100 r = Result() r.grade = result r.quiz = quiz r.student = ( Student.objects.get(username=request.user.username).first_name + " " + Student.objects.get(username=request.user.username).last_name ) r.username = Student.objects.get(username=request.user.username).username r.save() return HttpResponse(score / numberOfQuestions) except Exception as e: print(e) return HttpResponse(e)
def post(self, request): serializer = QuestionSerializer(data=request.data, many=True) # if the quiz contains many question if serializer.is_valid(): try: questions = serializer.data teacher = Teacher.objects.get(username=request.user.username) quiz = teacher.quiz_set.all() quiz = quiz[len(quiz) - 1] i = 1 for question in questions: q = Question.objects.get(unique_id=question["unique_id"]) q.text = question["text"] q.answer = question["answer"] q.choices = question["choices"] q.kind = question["kind"] q.save() quiz.questions.add(q) a = Analyse() a.quiz = quiz a.question_number = i a.save() i += 1 except Exception as e: print(e) return HttpResponse("quiz added")
def put(self, request): try: serializer = QuestionSerializer(data=request.data) if serializer.is_valid(): question = Question.objects.get(unique_id=serializer.data["unique_id"]) question.text = serializer.data["text"] question.kind = serializer.data["kind"] question.answer = serializer.data["answer"] question.choices = serializer.data["choices"] question.save() else: print(serializer.errors) except Exception as e: print(e) return HttpResponse("done")
def post(self, request): try: serializer = QuestionSerializer(data=request.data) if serializer.is_valid(): question = serializer.data q = Question() q.text = question["text"] q.answer = question["answer"] q.choices = question["choices"] q.kind = question["kind"] teacher = Teacher.objects.get(username=request.user.username) quiz = teacher.quiz_set.all()[0] q.quiz = quiz q.tag = Tag.objects.get(text=question["tag"]) q.unique_id = get_random_string() q.save() else: print(serializer.errors) except Exception as e: print(e) return HttpResponse("done")