def edit_question(request, quiz_id, question_id): form = NewQuestionForm(request.POST, request.FILES) if not form.is_valid(): messages.error( request, "The submitted form is incorrect: {}".format(form.errors)) return reload(request) try: question = TextualQuestion.objects.get(id=question_id) except ObjectDoesNotExist: messages.error(request, "This question is unavailable.") return reload(request) if question.quiz_id != int(quiz_id): messages.error(request, "You can only edit your own quizzes!") return reload(request) if request.user != question.quiz.created_by: messages.error(request, "You can only edit your own quizzes!") return reload(request) if not request.method == "POST": return TemplateResponse(request, "quizzes/edit_quiz.html", { "quiz": question.quiz, "question": question }) question.description = form.cleaned_data["description"] question.solution = form.cleaned_data["solution"] if form.cleaned_data["description_image"]: question.description_image = form.cleaned_data["description_image"] if form.cleaned_data["solution_image"]: question.solution_image = form.cleaned_data["solution_image"] question.save() messages.success(request, "The question was successfully edited!") Interaction.objects.create(interaction_type="Question edited") return reload(request)
def new_question(request, quiz_id): if not request.method == "POST": messages.debug(request, "Question creation is only allowed via POST.") return reload(request) form = NewQuestionForm(request.POST, request.FILES) if not form.is_valid(): messages.error( request, "The submitted form is incorrect: {}".format(form.errors)) return reload(request) associated_quiz = Quiz.objects.get(id=quiz_id) if request.user != associated_quiz.created_by: messages.error(request, "You can only edit your own quizzes!") return reload(request) if not associated_quiz: messages.error(request, "No associated quiz found.") return reload(request) TextualQuestion.objects.create( description=form.cleaned_data["description"], solution=form.cleaned_data["solution"], quiz=associated_quiz, description_image=form.cleaned_data["description_image"], solution_image=form.cleaned_data["solution_image"]) messages.success(request, "A new textual question was added!") Interaction.objects.create(interaction_type="New question added") return edit_quiz(request, quiz_id)
def search(request): if not request.method == "POST": messages.debug(request, "Search is only allowed via POST.") return reload(request) form = SearchForm(request.POST) if not form.is_valid(): return reload(request) query = form.cleaned_data["search"] if not query: return index(request) title_quizzes = Quiz.objects.filter(title__icontains=query) description_quizzes = Quiz.objects.filter(description__icontains=query) quizzes = set((title_quizzes | description_quizzes).distinct()) hashtags = HashTag.objects.filter(title__icontains=query) for hashtag in hashtags: quizzes.update(hashtag.quiz_set) title_compilations = Compilation.objects.filter(title__icontains=query) description_compilations = Compilation.objects.filter( description__icontains=query) compilations = set(title_compilations | description_compilations) Interaction.objects.create(interaction_type="Questionnaire searched") return TemplateResponse( request, "index.html", { "quizzes": quizzes, "compilations": compilations, "search": form.cleaned_data["search"], })
def delete_quiz(request, id): quiz = get_object_or_404(Quiz, id=id) if request.user != quiz.created_by: messages.error(request, "You can only delete your own quizzes!") return reload(request) if not quiz: messages.debug(request, "There is no quiz with the id %s" % id) return reload(request) quiz.delete() messages.success(request, "Quiz was successfully deleted!") Interaction.objects.create(interaction_type="Quiz deleted") return reload(request)
def new_quiz(request): if not request.method == "POST": messages.debug(request, "Quiz creation is only allowed via POST.") return reload(request) form = NewQuizForm(request.POST, request.FILES) if not form.is_valid(): messages.error(request, "The submitted form is incorrect.") return reload(request) if Quiz.objects.filter(title=form.cleaned_data["title"]).exists(): messages.warning( request, "A Quiz with the title {} already exists.".format( form.cleaned_data["title"])) return reload(request) quiz = Quiz.objects.create(title=form.cleaned_data["title"], description=form.cleaned_data["description"], image=form.cleaned_data["image"], created_by=request.user) messages.success(request, "A new quiz was created!") Interaction.objects.create(interaction_type="Quiz created") return edit_quiz(request, quiz.id)
def questionnaire_all(request, quiz_id): associated_quiz = Quiz.objects.get(id=quiz_id) if not associated_quiz: messages.error(request, "No associated quiz found.") return reload(request) questions = associated_quiz.question_set.all() html = render(request, "questionnaire_all.html", { "quiz": associated_quiz, "questions": questions }) return html
def questionnaire(request, quiz_id): associated_quiz = Quiz.objects.get(id=quiz_id) if not associated_quiz: messages.error(request, "No associated quiz found.") return reload(request) questions = associated_quiz.question_set.all() question_paginator = RandomizablePaginator(questions, 1) question_page = request.GET.get("question") questions = question_paginator.get_page(question_page) Interaction.objects.create( interaction_type="Questionnaire of quiz \"{}\" visited".format( associated_quiz.title)) return TemplateResponse(request, "questionnaire.html", { "quiz": associated_quiz, "questions": questions })
def edit_quiz(request, id): quiz = get_object_or_404(Quiz, id=id) if request.user != quiz.created_by: messages.error(request, "You can only edit your own quizzes!") return reload(request) return TemplateResponse(request, "quizzes/edit_quiz.html", {"quiz": quiz})