Uses election_detail for vote form

This commit is contained in:
Antoine Bartuccio 2016-12-22 01:28:51 +01:00
parent 938e2ce0a9
commit 1f60fbd484
4 changed files with 26 additions and 15 deletions

View File

@ -430,7 +430,7 @@ Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site.
listeT.save() listeT.save()
pres = Role(election=el, title="Président AE", description="Roi de l'AE") pres = Role(election=el, title="Président AE", description="Roi de l'AE")
pres.save() pres.save()
resp = Role(election=el, title="Co Respo Info", description="Ghetto++") resp = Role(election=el, title="Co Respo Info", max_choice=2, description="Ghetto++")
resp.save() resp.save()
cand = Candidature(role=resp, user=skia, election_list=liste, program="Refesons le site AE") cand = Candidature(role=resp, user=skia, election_list=liste, program="Refesons le site AE")
cand.save() cand.save()

View File

@ -56,7 +56,7 @@ class Role(models.Model):
max_choice = models.IntegerField(_('max choice'), default=1) max_choice = models.IntegerField(_('max choice'), default=1)
def user_has_voted(self, user): def user_has_voted(self, user):
return not self.has_voted.filter(id=user.id).exists() return self.has_voted.filter(id=user.id).exists()
def __str__(self): def __str__(self):
return ("%s : %s") % (self.election.title, self.title) return ("%s : %s") % (self.election.title, self.title)

View File

@ -239,7 +239,15 @@ th {
<section class="election__sumbit-section"> <section class="election__sumbit-section">
<button class="election__sumbit-button">{% trans %}Submit the vote !{% endtrans %}</button> <button class="election__sumbit-button">{% trans %}Submit the vote !{% endtrans %}</button>
</section> </section>
<a href="{{url('election:create_list')}}">{% trans %}Add a new list{% endtrans %}</a>
<a href="{{url('election:create_role')}}">{% trans %}Add a new role{% endtrans %}</a>
<form action="{{url('election:candidate', election_id=object.id)}}" method="post">{{candidate_form}} <form action="{{url('election:candidate', election_id=object.id)}}" method="post">{{candidate_form}}
{% csrf_token %} {% csrf_token %}
<p><input type="submit" value="{% trans %}Candidate{% endtrans %}" /></p>
</form>
<form action="{{url('election:vote', election_id=object.id)}}" method="post">
{{election_form.as_p()}}
<p><input type="submit" value="{% trans %}Vote{% endtrans %}" /></p>
{% csrf_token %}
</form> </form>
{% endblock %} {% endblock %}

View File

@ -59,7 +59,7 @@ class VoteForm(forms.Form):
def __init__(self, election, user, *args, **kwargs): def __init__(self, election, user, *args, **kwargs):
super(VoteForm, self).__init__(*args, **kwargs) super(VoteForm, self).__init__(*args, **kwargs)
for role in election.role.all(): for role in election.role.all():
if role.user_has_voted(user): if not role.user_has_voted(user):
cand = role.candidature cand = role.candidature
if role.max_choice > 1: if role.max_choice > 1:
self.fields[role.title] = VoteCheckbox(cand, role.max_choice, required=False) self.fields[role.title] = VoteCheckbox(cand, role.max_choice, required=False)
@ -97,6 +97,7 @@ class ElectionDetailView(CanViewMixin, DetailView):
""" Add additionnal data to the template """ """ Add additionnal data to the template """
kwargs = super(ElectionDetailView, self).get_context_data(**kwargs) kwargs = super(ElectionDetailView, self).get_context_data(**kwargs)
kwargs['candidate_form'] = CandidateForm(self.get_object().id) kwargs['candidate_form'] = CandidateForm(self.get_object().id)
kwargs['election_form'] = VoteForm(self.get_object(), self.request.user)
return kwargs return kwargs
@ -149,11 +150,10 @@ class VoteFormView(CanCreateMixin, FormView):
Alows users to vote Alows users to vote
""" """
form_class = VoteForm form_class = VoteForm
template_name = 'election/vote_form.jinja' template_name = 'election/election_detail.jinja'
def dispatch(self, request, *arg, **kwargs): def dispatch(self, request, *arg, **kwargs):
self.election_id = kwargs['election_id'] self.election = get_object_or_404(Election, pk=kwargs['election_id'])
self.election = get_object_or_404(Election, pk=self.election_id)
return super(VoteFormView, self).dispatch(request, *arg, **kwargs) return super(VoteFormView, self).dispatch(request, *arg, **kwargs)
def vote(self, data): def vote(self, data):
@ -171,17 +171,24 @@ class VoteFormView(CanCreateMixin, FormView):
""" """
data = form.clean() data = form.clean()
res = super(FormView, self).form_valid(form) res = super(FormView, self).form_valid(form)
if self.request.user.is_root: for grp in self.election.vote_groups.all():
self.vote(data)
return res
for grp in data['role'].election.vote_groups.all():
if self.request.user.is_in_group(grp): if self.request.user.is_in_group(grp):
self.vote(data) self.vote(data)
return res return res
return res return res
def get_success_url(self, **kwargs): def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.election_id}) return reverse_lazy('election:detail', kwargs={'election_id': self.election.id})
def get_context_data(self, **kwargs):
""" Add additionnal data to the template """
kwargs = super(VoteFormView, self).get_context_data(**kwargs)
kwargs['candidate_form'] = CandidateForm(self.election.id)
kwargs['object'] = self.election
kwargs['election'] = self.election
kwargs['election_form'] = self.get_form()
return kwargs
# Create views # Create views
@ -220,8 +227,6 @@ class RoleCreateView(CanCreateMixin, CreateView):
""" """
obj = form.instance obj = form.instance
res = super(CreateView, self).form_valid res = super(CreateView, self).form_valid
if self.request.user.is_root:
return res(form)
if obj.election: if obj.election:
for grp in obj.election.edit_groups.all(): for grp in obj.election.edit_groups.all():
if self.request.user.is_in_group(grp): if self.request.user.is_in_group(grp):
@ -245,8 +250,6 @@ class ElectionListCreateView(CanCreateMixin, CreateView):
obj = form.instance obj = form.instance
res = super(CreateView, self).form_valid res = super(CreateView, self).form_valid
if obj.election: if obj.election:
if self.request.user.is_root:
return res(form)
for grp in obj.election.candidature_groups.all(): for grp in obj.election.candidature_groups.all():
if self.request.user.is_in_group(grp): if self.request.user.is_in_group(grp):
return res(form) return res(form)