def update_quiz(request, pk, template_name='teachers/update/update_quiz.html'): quiz= get_object_or_404(Quiz, pk=pk) quizzes = repo.QuizRepository(Quiz) owner_id = quizzes.get_owner_id(pk) is_active = quizzes.is_active(pk) if request.user.id == owner_id and not is_active: questions = repo.QuestionRepository(Questions) sum_question_points = questions.sum_all_quiz_questions_points(pk) cur_quiz_points = quizzes.get_quiz_points(pk) form = QuizForm(request.POST or None, instance=quiz) if form.is_valid(): quiz = form.save(commit=False) form_points_data = form.cleaned_data["max_points"] quiz.max_points = round(form_points_data, 1) if_points_changed = round(form_points_data - cur_quiz_points, 1) if if_points_changed != 0: course_id = quizzes.get_course_id(pk) all_q_points = quizzes.get_all_quiz_points(course_id) c_points = quizzes.get_course_points(pk) free_points = c_points - all_q_points + cur_quiz_points free_points = round(free_points, 1) if form_points_data > free_points: quiz.max_points = cur_quiz_points messages.error(request, 'Available course points: %f. '%free_points, extra_tags='alert') if sum_question_points > form_points_data: quiz.max_points = cur_quiz_points messages.error(request, 'Question points in this quiz are: %f. '%sum_question_points, extra_tags='alert') quiz.save() return HttpResponse(render_to_string('teachers/update/item_edit_form_success.html')) return render(request, template_name, {'form':form, 'id': pk, 'sum_questions_points': sum_question_points}) raise Http404
def test_valid_form(self): data = { "title": "Example", "category": str(self.category.pk), } form = QuizForm(data=data) self.assertTrue(form.is_valid())
def test_invalid_when_any_word_of_description_is_longer_than_45_characters(self): data = { "title": "Example", "category": str(self.category.pk), "description": "one_very_long_word_and_that_should_raise_a_validation_error", } form = QuizForm(data) self.assertFalse(form.is_valid())
def test_is_invalid_when_quiz_with_the_same_title_already_exists(self): Quiz.objects.create(title="Example", category=self.category, author=self.user) data = { "title": "Example", "category": str(self.category.pk), } form = QuizForm(data=data) self.assertFalse(form.is_valid())
def test_saves_quiz_with_given_author(self): data = { "title": "Example", "category": str(self.category.pk), } form = QuizForm(data=data) quiz = form.save(author=self.user, commit=True) self.assertIs(quiz.author, self.user) self.assertTrue(Quiz.objects.filter(title=quiz.title).exists())
def post(self, request, *args, **kwargs): QuestionsFormSet = self.get_questions_formset_class() quiz_form = QuizForm( self.request.POST, self.request.FILES, instance=self.object ) questions_formset = QuestionsFormSet( request.POST, request.FILES, instance=self.object ) if quiz_form.is_valid() and questions_formset.is_valid(): return self.forms_valid(quiz_form, questions_formset) else: return self.forms_invalid(quiz_form, questions_formset)
def get(self, request, *args, **kwargs): QuestionsFormSet = self.get_questions_formset_class() context = { **self.get_context_data(), "quiz_form": QuizForm(instance=self.object), "questions_formset": QuestionsFormSet(instance=self.object), } return self.render_to_response(context)
def quiz_add(request, pk): course = get_object_or_404(Course, pk=pk, owner_id=request.user) courses = repo.CourseRepository(Course) c_points = courses.get_points(pk) quizzes = repo.QuizRepository(Quiz) all_q_points = quizzes.get_all_quiz_points(pk) free_points = c_points - all_q_points free_points = round(free_points, 1) if request.method == 'POST': form = QuizForm(request.POST) if form.is_valid(): quiz = form.save(commit=False) quiz.course = course quiz.owner = request.user max_points = form.cleaned_data["max_points"] difference = round(max_points - free_points, 1) if(difference <= 0): quiz.max_points = round(max_points, 1) quiz.save() messages.success(request, 'You may now add question/options to the quiz.') return redirect('/teachers/course/%d/quizzes'%pk) else: messages.error(request, 'You put more points than you have left. Available points: %f'%free_points, extra_tags='alert') else: form = QuizForm() return render(request, 'teachers/add/quiz_add.html', {'course': course, 'form': form, 'free_points':free_points })
def quiz_creation_view(request, code): user = request.user if request.method == 'POST': form = QuizForm(request.POST) if form.is_valid(): quiz = form.save(commit=False) quiz.creator = user quiz.group = Group.objects.get(pk=code) quiz.save() # manage_questions(request, quiz) return redirect( reverse_lazy('add-questions', kwargs={'qid': quiz.pk})) # questions = question_set(request.POST, instance=quiz) else: form = QuizForm() return render(request, 'quizzes/create_quiz.html', {'form': form})