Commit 9d531fea authored by Christophe Siraut's avatar Christophe Siraut

Move clear_cookie. Cleanups.

parent 72b18abe
......@@ -33,9 +33,6 @@ def profile(request):
request.user.userprofile.save()
return HttpResponseRedirect(reverse('home'))
form = UserProfileForm(instance=request.user.userprofile)
return render(request, "form.html", {'form': form})
return render(request, "form.html", {'form': form})
def clear_cookie(request, poll_id):
request.session.clear()
return HttpResponseRedirect(reverse('vote', args=(poll_id,)))
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import get_current_site
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.forms.formsets import formset_factory
from django.forms.models import inlineformset_factory, BaseInlineFormSet
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render_to_response, render
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from account.forms import UserProfileForm
from account.views import email_notify
from meetingpoll.forms import PollForm, ChoiceForm, VoteForm, BulletinForm
from meetingpoll.models import Poll, Choice, Vote, Bulletin
def new(request):
if request.method == 'POST':
form = PollForm(request.POST)
......@@ -24,93 +21,80 @@ def new(request):
poll.user = request.user
poll.save()
key = 'is_' + poll.id + '_author'
request.session[key] = True # This writes cookie??
return HttpResponseRedirect(reverse('choices', args=(str(poll.id),)))
request.session[key] = True # This writes cookie
return HttpResponseRedirect(reverse('choices',
args=(str(poll.id),)))
else:
form = PollForm()
if request.user.is_anonymous():
request.session.clear()
messages.warning(request, _('You are not logged so you will not be able to modify your poll after creation or recieve notifications.'))
form = PollForm()
return render_to_response(
'meetingpoll/poll_form.html',
{'form': form},
context_instance=RequestContext(request))
messages.warning(request, _('You are not logged so you will not \
be able to modify your poll after creation or recieve notifications.'))
return render_to_response('meetingpoll/poll_form.html',
{'form': form},
context_instance=RequestContext(request))
def get_ordereditem_formset(form, formset=BaseInlineFormSet, **kwargs):
return inlineformset_factory(Poll, Choice, form, formset, **kwargs)
def editchoices(request, poll_id):
poll = get_object_or_404(Poll.objects.all(), id=poll_id)
language_code = request.LANGUAGE_CODE
if request.user.is_anonymous(): # Anonymous wants to edit his new poll
key = 'is_' + poll_id + '_author'
if not request.session.get(key, False):
return HttpResponseRedirect(reverse('home'))
else:
if request.user != poll.user:
return HttpResponseRedirect(reverse('home'))
key = 'is_' + poll_id + '_author' # Anonymous wants to edit his new poll?
if request.user != poll.user and not request.session.get(key, False):
return HttpResponseRedirect(reverse('home'))
OrderedItemFormset = get_ordereditem_formset(ChoiceForm, extra=0, can_delete=True)
OrderedItemFormset = get_ordereditem_formset(ChoiceForm,
extra=0, can_delete=True)
if request.method == "POST":
"""
Should we set a maximum of choices here?
#instances = request.POST.values()
if len(instances) < 20:
for selection in instances:
"""
#Should we set a maximum of choices here?
instances = OrderedItemFormset(request.POST, instance=poll)
if instances.is_valid():
for instance in instances.cleaned_data:
try:
this_choice = instance['choice']
if not instance.get('DELETE'):
try:
choice = Choice.objects.get(poll=poll, choice=instance['choice'])
choice.details = instance['details']
choice.save()
except ObjectDoesNotExist:
choice = Choice(poll=poll, choice=instance['choice'], details=instance['details'])
choice.save()
# If bulletins for this poll existed before edition
# add an 'false' vote child to them for newly created choice.
# This makes template computation easier
for bulletin in Bulletin.objects.filter(poll=poll):
nvote = Vote(choice=choice, bulletin=bulletin, voice=False)
nvote.save()
else:
try:
choice = Choice.objects.get(poll=poll, choice=this_choice)
"""
Removing a Choice will remove all childeren Vote objects.
When Django deletes an object, it emulates the behavior of
the SQL constraint ON DELETE CASCADE -- in other words, any
objects which had foreign keys pointing at the object to be
deleted will be deleted along with it."""
choice.delete()
except :
pass
except:
# probably an empty datefield?
pass
this_choice = instance['choice']
if not instance.get('DELETE'):
try:
choice = Choice.objects.get(poll=poll, choice=instance['choice'])
choice.details = instance['details']
choice.save()
except ObjectDoesNotExist:
choice = Choice(poll=poll, choice=instance['choice'], details=instance['details'])
choice.save()
# If bulletins for this poll existed before edition
# add an 'false' vote child to them for newly created choice.
# This makes template computation easier
for bulletin in Bulletin.objects.filter(poll=poll):
nvote = Vote(choice=choice, bulletin=bulletin, voice=False)
nvote.save()
else:
try:
choice = Choice.objects.get(poll=poll, choice=this_choice)
choice.delete() #Removing a Choice will remove all childeren Vote objects.
except :
pass
return HttpResponseRedirect(poll.link)
else:
#vforms=OrderedItemFormset(request.POST, instance=poll)
messages.error(request, _("There are some errors in the form you posted."))
vforms = instances
else:
if Choice.objects.filter(poll=poll_id).count() == 0:
# Sall we replace next line with something like OrderedItemFormset(extra=2)
OrderedItemFormset = get_ordereditem_formset(ChoiceForm, extra=1, can_delete=True)
OrderedItemFormset = get_ordereditem_formset(
ChoiceForm, extra=1, can_delete=True)
vforms = OrderedItemFormset(instance=poll)
else:
vforms = OrderedItemFormset(instance=poll)
return render_to_response('meetingpoll/choice_form.html',
{'object': poll, 'vforms': vforms, 'language_code': language_code,},
context_instance=RequestContext(request))
{'object': poll,
'vforms': vforms,
'language_code': language_code},
context_instance=RequestContext(request))
@login_required
......@@ -125,7 +109,7 @@ def delete(request, poll_id):
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 vote(request, poll_id):
......@@ -133,10 +117,10 @@ def vote(request, poll_id):
has_voted, is_updated = False, False
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) ]
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)]
if form.is_valid():
if form.is_valid():
if request.user.is_authenticated():
voter = str(request.user)
else:
......@@ -145,14 +129,6 @@ def vote(request, poll_id):
voter = request.session.get('name')
if voter != 'your name':
"""
try:
Bulletin.objects.get(poll=poll.id,voter=voter)
return render_to_response('sondage/poll_detail.html', {'object': poll, 'form': form, 'vforms':vforms, 'error_message':error_message}, context_instance=RequestContext(request))
except:
pass
"""
if not Bulletin.objects.filter(poll=poll.id, voter=voter):
bulletin = Bulletin(poll=poll, voter=voter)
bulletin.save()
......@@ -175,10 +151,10 @@ def vote(request, poll_id):
if new.voice:
choice.votecount += 1
choice.save()
if request.user.is_anonymous(): # Anonymous has voted...
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
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)
......@@ -265,9 +241,11 @@ def vote(request, poll_id):
if voter:
messages.info(request, _('Not %s? Click Forget me.') % voter)
form = BulletinForm(instance=poll,initial={'voter': voter})
#form = BulletinForm(instance=poll)
return render_to_response('meetingpoll/poll_detail.html', {'object': poll, 'form': form, 'vforms':vforms, 'has_voted': has_voted}, context_instance=RequestContext(request))
form = BulletinForm(instance=poll, initial={'voter': voter})
return render_to_response('meetingpoll/poll_detail.html',
{'object': poll, 'form': form, 'vforms':vforms, 'has_voted': has_voted},
context_instance=RequestContext(request))
def exp_csv(request, poll_id):
import csv
......@@ -290,3 +268,7 @@ def exp_csv(request, poll_id):
r.append(v)
writer.writerow(r)
return response
def clear_cookie(request, poll_id):
request.session.clear()
return HttpResponseRedirect(reverse('vote', args=(poll_id,)))
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