def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) history, c = History.objects.get_or_create(user=self.request.user) context["category_progress_table"] = CategoryProgressTable( history.category_progress) context["has_current_session"] = True if QuizSession.get_current( self.request.user) else False return context
def post(self, request, *args, **kwargs): form = self.get_form(data=request.POST, files=request.FILES) if QuizSession.get_current(user=request.user) is not None: form.add_error("There is a current session already in pregress!") return self.form_invalid(form) elif form.is_valid(): quiz_session = QuizSession.create_from_categories( user=self.request.user, categories=form.cleaned_data["categories"], max_n=int(form.cleaned_data["max_num_questions"]), randomise_order=form.cleaned_data["randomise_order"], include_answered=form.cleaned_data["include_answered"], ) return HttpResponseRedirect(reverse("mcq_bank:run_session")) else: return self.form_invalid(form)
def post(self, request, *args, **kwargs): form = self.get_form(data=request.POST, files=request.FILES) if form.is_valid(): current_session = QuizSession.get_current( user=form.cleaned_data["user"]) if current_session: current_session.mark_quiz_complete() new_session = QuizSession.create_from_questions( user=form.cleaned_data["user"], questions=form.cleaned_data["questions"], max_n=10000, randomise_order=True, include_answered=True, save=True, ) return HttpResponseRedirect(reverse("mcq_bank:run_session")) else: return self.form_invalid(form)
def get_context_data(self, *args, **kwargs): context = super(QuizSessionEndOrContinueView, self).get_context_data(**kwargs) current_session = QuizSession.get_current(user=self.user) progress = current_session.progress if current_session else (0, 0) context["current_session"] = current_session context["questions_answered"] = progress[0] context["questions_total"] = progress[1] context["percent_complete"] = round(100 * progress[0] / progress[1], 2) if progress[1] > 0 else 0 return context
def post(self, request, *args, **kwargs): current_session = QuizSession.get_current(user=self.request.user) if request.POST["choice"] == "start_over": print("Finishing current session and creating new one") try: current_session.mark_quiz_complete() except AttributeError: pass return HttpResponseRedirect( reverse("mcq_bank:quiz_session_create")) elif request.POST["choice"] == "continue": return HttpResponseRedirect(reverse("mcq_bank:run_session")) else: raise RuntimeError( f"Form choice {request.POST['choice']} is invalid!")
def dispatch(self, request, *args, **kwargs): self.user = self.request.user self.session = QuizSession.get_current(request.user) if self.session is None: return redirect("mcq_bank:quiz_session_create") self.history = self.user.history # Set question index to match url if specified if self.request.GET.get("q"): question_index = int(self.request.GET["q"]) if question_index < 0 or question_index > self.session.questions.count() - 1: raise ValueError("Invalid question index specified!") else: self.session.current_question_index = question_index self.session.save() return super(QuizTakeView, self).dispatch(request, *args, **kwargs)
def quiz_session_answer_for_question(quiz_session: QuizSession, question: Question) -> Answer: return quiz_session.user_answer_for_question(question)
def current_session(self): from medusa_website.mcq_bank.models import QuizSession return QuizSession.get_current(user=self.user)
def get_object(self, queryset=None): return QuizSession.get_current(user=self.user)
def get_context_data(self, *args, **kwargs): context = super(QuizSessionCreateView, self).get_context_data(**kwargs) context["has_current_session"] = True if QuizSession.get_current( user=self.request.user) is not None else False return context