Commit 8f4897f2 authored by Christophe Siraut's avatar Christophe Siraut

Poster votes yes by default

parent ecd81ad0
......@@ -47,10 +47,13 @@ class Poll(models.Model):
user = models.ForeignKey(User, null=True)
result = jsonfield.JSONField()
def save(self, *args, **kwargs):
self.compile()
super(Poll, self).save(*args, **kwargs)
def compile(self):
self.result = {'voters': self.compile_voters(),
'choices': self.compile_choices()}
self.save()
def compile_voters(self):
'''Return a list of votes by voter'''
......@@ -110,6 +113,12 @@ class Bulletin(models.Model):
def __unicode__(self):
return self.voter
if not Bulletin.objects.filter(poll=poll.id, voter=voter):
bulletin = Bulletin(poll=poll, voter=voter)
bulletin.save()
else:
bulletin = Bulletin.objects.get(poll=poll.id, voter=voter)
return bulletin
class Vote(models.Model):
choice = models.ForeignKey(Choice)
......
......@@ -66,7 +66,20 @@ def editchoices(request, poll_id):
if formset.is_valid():
formset.save()
if not '_addanother' in request.POST:
return HttpResponseRedirect(poll.link)
voter = get_voter_name(request)
if not voter:
voter = 'Anonymous'
if not Bulletin.objects.filter(poll=poll.id, voter=voter):
bulletin = Bulletin(poll=poll, voter=voter)
bulletin.save()
votes = [Vote(bulletin=bulletin, choice=choice, voice=True)
for choice in Choice.objects.filter(poll=poll.id)]
for v in votes:
v.save()
poll.save()
return HttpResponseRedirect(reverse('meetingpoll', poll.id))
else:
messages.error(request,
_("There are some errors in the form you posted."))
......@@ -99,6 +112,15 @@ def make_buletin_form(poll, **kwargs):
return [[VoteForm(prefix=choice)] for choice in Choice.objects.filter(poll=poll.id)]
def get_voter_name(request, form=None):
if request.user.is_authenticated():
return str(request.user)
else:
if form:
return form.cleaned_data['voter']
if request.session.get('name'):
return request.session.get('name')
def vote(request, poll_id):
poll = get_object_or_404(Poll.objects.all(), id=poll_id)
has_voted, is_updated = False, False
......@@ -106,21 +128,21 @@ def vote(request, poll_id):
if request.method == 'POST':
form = BulletinForm(request.POST, initial={'poll': poll.id})
vforms = [[VoteForm(request.POST, prefix=choice, instance=choice) ] for choice in Choice.objects.filter(poll=poll.id)]
vforms = [[VoteForm(request.POST, prefix=choice, instance=choice)]
for choice in Choice.objects.filter(poll=poll.id)]
if form.is_valid():
if request.POST.get('trap', False):
return render(request,
return render(
request,
'meetingpoll/poll_detail.html',
{'object': poll, 'form': form, 'vforms':vforms, 'has_voted': has_voted})
{'object': poll,
'form': form,
'vforms': vforms,
'has_voted': has_voted})
if request.user.is_authenticated():
voter = str(request.user)
else:
voter = form.cleaned_data['voter']
if request.session.get('name'):
voter = request.session.get('name')
voter = get_voter_name(request, form)
for forms in vforms:
for vorm in forms:
......@@ -138,13 +160,20 @@ def vote(request, poll_id):
'meetingpoll/poll_detail.html',
{'object': poll, 'form': form, 'vforms':vforms, 'has_voted': has_voted})
if voter != 'your name':
if not voter or voter == 'your name':
messages.error(request, _('Did you forget to provide your name?'))
form = BulletinForm(instance=poll, initial={'voter': voter})
return render(request,
'meetingpoll/poll_detail.html',
{'object': poll, 'form': form, 'vforms':vforms, 'has_voted': has_voted})
if not Bulletin.objects.filter(poll=poll.id, voter=voter):
bulletin = Bulletin(poll=poll, voter=voter)
bulletin.save()
else:
bulletin = Bulletin.objects.get(poll=poll.id, voter=voter)
for forms in vforms:
for vorm in forms:
if vorm.is_valid():
......@@ -184,7 +213,7 @@ def vote(request, poll_id):
old.comment = vorm.cleaned_data['comment']
old.save()
is_updated = True
poll.compile()
poll.save()
if has_voted:
messages.success(request, _("Your vote has been counted, thank you."))
......@@ -198,8 +227,6 @@ def vote(request, poll_id):
pass
else:
messages.error(request, _('Did you forget to provide your name?'))
else:
messages.error(request, _('Did you forget to provide your name?'))
voter = _('your name')
else: # request.method = 'GET'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment