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): ...@@ -47,10 +47,13 @@ class Poll(models.Model):
user = models.ForeignKey(User, null=True) user = models.ForeignKey(User, null=True)
result = jsonfield.JSONField() result = jsonfield.JSONField()
def save(self, *args, **kwargs):
self.compile()
super(Poll, self).save(*args, **kwargs)
def compile(self): def compile(self):
self.result = {'voters': self.compile_voters(), self.result = {'voters': self.compile_voters(),
'choices': self.compile_choices()} 'choices': self.compile_choices()}
self.save()
def compile_voters(self): def compile_voters(self):
'''Return a list of votes by voter''' '''Return a list of votes by voter'''
...@@ -110,6 +113,12 @@ class Bulletin(models.Model): ...@@ -110,6 +113,12 @@ class Bulletin(models.Model):
def __unicode__(self): def __unicode__(self):
return self.voter 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): class Vote(models.Model):
choice = models.ForeignKey(Choice) choice = models.ForeignKey(Choice)
......
...@@ -66,7 +66,20 @@ def editchoices(request, poll_id): ...@@ -66,7 +66,20 @@ def editchoices(request, poll_id):
if formset.is_valid(): if formset.is_valid():
formset.save() formset.save()
if not '_addanother' in request.POST: 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: else:
messages.error(request, messages.error(request,
_("There are some errors in the form you posted.")) _("There are some errors in the form you posted."))
...@@ -99,6 +112,15 @@ def make_buletin_form(poll, **kwargs): ...@@ -99,6 +112,15 @@ def make_buletin_form(poll, **kwargs):
return [[VoteForm(prefix=choice)] for choice in Choice.objects.filter(poll=poll.id)] 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): def vote(request, poll_id):
poll = get_object_or_404(Poll.objects.all(), id=poll_id) poll = get_object_or_404(Poll.objects.all(), id=poll_id)
has_voted, is_updated = False, False has_voted, is_updated = False, False
...@@ -106,21 +128,21 @@ def vote(request, poll_id): ...@@ -106,21 +128,21 @@ def vote(request, poll_id):
if request.method == 'POST': if request.method == 'POST':
form = BulletinForm(request.POST, initial={'poll': poll.id}) 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 form.is_valid():
if request.POST.get('trap', False): if request.POST.get('trap', False):
return render(request, return render(
request,
'meetingpoll/poll_detail.html', '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 = get_voter_name(request, form)
voter = str(request.user)
else:
voter = form.cleaned_data['voter']
if request.session.get('name'):
voter = request.session.get('name')
for forms in vforms: for forms in vforms:
for vorm in forms: for vorm in forms:
...@@ -138,66 +160,71 @@ def vote(request, poll_id): ...@@ -138,66 +160,71 @@ def vote(request, poll_id):
'meetingpoll/poll_detail.html', '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 voter != 'your name': if not voter or voter == 'your name':
if not Bulletin.objects.filter(poll=poll.id, voter=voter): messages.error(request, _('Did you forget to provide your name?'))
bulletin = Bulletin(poll=poll, voter=voter) form = BulletinForm(instance=poll, initial={'voter': voter})
bulletin.save() return render(request,
else: 'meetingpoll/poll_detail.html',
bulletin = Bulletin.objects.get(poll=poll.id, voter=voter) {'object': poll, 'form': form, 'vforms':vforms, 'has_voted': has_voted})
for forms in vforms: if not Bulletin.objects.filter(poll=poll.id, voter=voter):
for vorm in forms: bulletin = Bulletin(poll=poll, voter=voter)
if vorm.is_valid(): bulletin.save()
try: else:
choice = Choice.objects.get(choice=vorm.cleaned_data['choice'], poll=poll) bulletin = Bulletin.objects.get(poll=poll.id, voter=voter)
except:
return render(request, vorm.cleaned_data['choice'])
for forms in vforms:
if not Vote.objects.filter(choice=choice, bulletin=bulletin): for vorm in forms:
new = Vote(choice=choice, bulletin=bulletin, comment=vorm.cleaned_data['comment']) if vorm.is_valid():
try:
choice = Choice.objects.get(choice=vorm.cleaned_data['choice'], poll=poll)
except:
return render(request, vorm.cleaned_data['choice'])
if not Vote.objects.filter(choice=choice, bulletin=bulletin):
new = Vote(choice=choice, bulletin=bulletin, comment=vorm.cleaned_data['comment'])
if vorm.cleaned_data['voice']:
new.voice = True
else:
new.voice = False
new.save()
if new.voice:
choice.votecount += 1
choice.save()
if request.user.is_anonymous(): # Anonymous has voted...
key = 'has_voted-' + poll.id
request.session[key] = True # This writes cookie
request.session['name'] = voter # This writes cookie
has_voted = True # Used to show "Forget me"
else:
old = Vote.objects.get(choice=choice, bulletin=bulletin)
if old.voice:
if not vorm.cleaned_data['voice']:
choice.votecount -= 1
choice.save()
else:
if vorm.cleaned_data['voice']: if vorm.cleaned_data['voice']:
new.voice = True
else:
new.voice = False
new.save()
if new.voice:
choice.votecount += 1 choice.votecount += 1
choice.save() choice.save()
if request.user.is_anonymous(): # Anonymous has voted...
key = 'has_voted-' + poll.id old.voice = vorm.cleaned_data['voice']
request.session[key] = True # This writes cookie old.comment = vorm.cleaned_data['comment']
request.session['name'] = voter # This writes cookie old.save()
has_voted = True # Used to show "Forget me" is_updated = True
else: poll.save()
old = Vote.objects.get(choice=choice, bulletin=bulletin)
if has_voted:
if old.voice: messages.success(request, _("Your vote has been counted, thank you."))
if not vorm.cleaned_data['voice']: if is_updated:
choice.votecount -= 1 messages.success(request, _("Your vote has been updated, thank you."))
choice.save() if has_voted and poll.user:
else: try:
if vorm.cleaned_data['voice']: if poll.user.userprofile.email_notifications:
choice.votecount += 1 email_notify(poll, voter)
choice.save() except:
pass
old.voice = vorm.cleaned_data['voice']
old.comment = vorm.cleaned_data['comment']
old.save()
is_updated = True
poll.compile()
if has_voted:
messages.success(request, _("Your vote has been counted, thank you."))
if is_updated:
messages.success(request, _("Your vote has been updated, thank you."))
if has_voted and poll.user:
try:
if poll.user.userprofile.email_notifications:
email_notify(poll, voter)
except:
pass
else:
messages.error(request, _('Did you forget to provide your name?'))
else: else:
messages.error(request, _('Did you forget to provide your name?')) messages.error(request, _('Did you forget to provide your name?'))
voter = _('your name') voter = _('your name')
......
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