Jump to next question after a vote
This commit is contained in:
parent
9faa0b3354
commit
216bd7fab9
1 changed files with 16 additions and 7 deletions
|
@ -47,8 +47,8 @@ class BackgroundUpdateView(RedirectView):
|
||||||
class ElectionCreateView(SuccessMessageMixin, CreateView):
|
class ElectionCreateView(SuccessMessageMixin, CreateView):
|
||||||
model = Election
|
model = Election
|
||||||
form_class = ElectionForm
|
form_class = ElectionForm
|
||||||
template_name = "elections/election_create.html"
|
|
||||||
success_message = _("Élection créée avec succès !")
|
success_message = _("Élection créée avec succès !")
|
||||||
|
template_name = "elections/election_create.html"
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return reverse("election.admin", args=[self.object.pk])
|
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
|
model = Question
|
||||||
template_name = "elections/vote.html"
|
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):
|
def get(self, request, *args, **kwargs):
|
||||||
self.object = self.get_object()
|
self.object = self.get_object()
|
||||||
vote_form = OptionFormSet(instance=self.object)
|
vote_form = OptionFormSet(instance=self.object)
|
||||||
|
@ -304,9 +316,6 @@ class VoteView(OpenElectionOnly, SuccessMessageMixin, DetailView):
|
||||||
for v in vote_form:
|
for v in vote_form:
|
||||||
v.record_vote(self.request.user)
|
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(
|
return HttpResponseRedirect(self.get_success_url())
|
||||||
reverse("election.view", args=[self.object.election.pk])
|
|
||||||
+ f"#q_{self.object.pk}"
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in a new issue