Commit 8f4897f2 authored by chris's avatar chris

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,66 +160,71 @@ 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 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():
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 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():
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']:
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']:
choice.votecount += 1
choice.save()
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?'))
old.voice = vorm.cleaned_data['voice']
old.comment = vorm.cleaned_data['comment']
old.save()
is_updated = True
poll.save()
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?'))
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