Jump to next question after a vote

This commit is contained in:
Tom Hubrecht 2020-12-20 10:49:39 +01:00
parent 9faa0b3354
commit 216bd7fab9

View file

@ -47,8 +47,8 @@ class BackgroundUpdateView(RedirectView):
class ElectionCreateView(SuccessMessageMixin, CreateView):
model = Election
form_class = ElectionForm
template_name = "elections/election_create.html"
success_message = _("Élection créée avec succès !")
template_name = "elections/election_create.html"
def get_success_url(self):
return reverse("election.admin", args=[self.object.pk])
@ -286,10 +286,22 @@ class ElectionView(DetailView):
)
class VoteView(OpenElectionOnly, SuccessMessageMixin, DetailView):
class VoteView(OpenElectionOnly, DetailView):
model = Question
template_name = "elections/vote.html"
def get_success_url(self):
questions = list(self.object.election.questions.all())
q_index = questions.index(self.object)
if q_index + 1 == len(questions):
# On était à la dernière question
return reverse("election.view", args=[self.object.election.pk])
# On récupère l'id de la prochaine question
q_next = questions[q_index + 1].pk
return reverse("election.vote", args=[q_next])
def get(self, request, *args, **kwargs):
self.object = self.get_object()
vote_form = OptionFormSet(instance=self.object)
@ -304,9 +316,6 @@ class VoteView(OpenElectionOnly, SuccessMessageMixin, DetailView):
for v in vote_form:
v.record_vote(self.request.user)
messages.success(self.request, _("Votre vote a bien été enregistré !"))
messages.success(self.request, _("Votre choix a bien été enregistré !"))
return HttpResponseRedirect(
reverse("election.view", args=[self.object.election.pk])
+ f"#q_{self.object.pk}"
)
return HttpResponseRedirect(self.get_success_url())