summaryrefslogtreecommitdiff
path: root/polls
diff options
context:
space:
mode:
Diffstat (limited to 'polls')
-rw-r--r--polls/forms.py51
-rw-r--r--polls/models.py10
-rw-r--r--polls/views.py45
3 files changed, 101 insertions, 5 deletions
diff --git a/polls/forms.py b/polls/forms.py
new file mode 100644
index 0000000..bd6b9b3
--- /dev/null
+++ b/polls/forms.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2009 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# See the file COPYING for details.
+
+'''
+Forms management
+'''
+
+from django import forms
+from django.utils.translation import gettext_lazy as _
+
+from papillon.polls.models import Poll, Category
+
+class CreateForm(forms.Form):
+ author_name = forms.CharField(label=_("Author name"), max_length=100,
+ help_text=_("Name, firstname or nickname of the author"))
+ name = forms.CharField(label=_("Poll name"), max_length=200,
+ help_text=_("Global name to present the poll"))
+ description = forms.CharField(label=_("Poll description"), max_length=200,
+ help_text=_("Precise description of the poll"),
+ widget=forms.widgets.Textarea())
+ public = forms.BooleanField(label=_("Display the poll on main page"),
+ required=False, help_text=_("Check this option to make the poll \
+public"))
+ poll_type = forms.ChoiceField(label=_("Type of the poll"), choices=Poll.TYPE,
+ help_text=_("""Type of the poll:
+
+ - "Yes/No poll" is the appropriate type for a simple multi-choice poll
+ - "Yes/No/Maybe poll" allows voters to stay undecided
+ - "One choice poll" gives only one option to choose from
+"""))
+
+class CreateWithCatForm(CreateForm):
+ category = forms.ChoiceField(label="", help_text="Category of the poll",
+ choices=[(cat.id, cat.name) for cat in Category.objects.all()])
+
diff --git a/polls/models.py b/polls/models.py
index 531c1e3..8f41cec 100644
--- a/polls/models.py
+++ b/polls/models.py
@@ -51,8 +51,8 @@ class Poll(models.Model):
modification_date = models.DateTimeField(auto_now=True)
public = models.BooleanField(default=False)
open = models.BooleanField(default=True)
- TYPE = (('P', _('Poll')),
- ('B', _('Balanced poll')),
+ TYPE = (('P', _('Yes/No poll')),
+ ('B', _('Yes/No/Maybe poll')),
('O', _('One choice poll')),)
# ('M', _('Meeting')),)
type = models.CharField(max_length=1, choices=TYPE)
@@ -130,11 +130,13 @@ class Choice(models.Model):
class Meta:
ordering = ['order']
- def getSum(self):
+ def getSum(self, balanced_poll=None):
'''Get the sum of votes for this choice'''
sum = 0
for vote in Vote.objects.filter(choice=self, value__isnull=False):
sum += vote.value
+ if balanced_poll:
+ return sum/2
return sum
def changeOrder(self, idx=1):
@@ -167,4 +169,4 @@ class Vote(models.Model):
VOTE = ((1, (_('Yes'), _('Yes'))),
(0, (_('No'), _('Maybe')), ),
(-1, (_('No'), _('No'))),)
- value = models.IntegerField(choices=VOTE, blank=True, null=True) \ No newline at end of file
+ value = models.IntegerField(choices=VOTE, blank=True, null=True)
diff --git a/polls/views.py b/polls/views.py
index b7103a8..8e04b12 100644
--- a/polls/views.py
+++ b/polls/views.py
@@ -33,6 +33,7 @@ from django.http import HttpResponseRedirect
from papillon.settings import LANGUAGES
from papillon.polls.models import Poll, PollUser, Choice, Voter, Vote, \
Category, Comment
+from papillon.polls.forms import CreateForm, CreateWithCatForm
def getBaseResponse(request):
"""Manage basic fields for the template
@@ -73,6 +74,48 @@ def category(request, category_id):
response_dct['polls'] = Poll.objects.filter(public=True, category=category)
return render_to_response('category.html', response_dct)
+def create(request):
+ '''Creation of a poll.
+ '''
+ def genRandomURL():
+ "Generation of a random url"
+ url = ''
+ while not url or Poll.objects.filter(base_url=url).count() or\
+ Poll.objects.filter(admin_url=url).count():
+ url = ''
+ chars = string.letters + string.digits
+ for i in xrange(6):
+ url += random_choice(chars)
+ url += str(int(time.time()))
+ return url
+
+ response_dct, redirect = getBaseResponse(request)
+ base_form = CreateForm
+ if Category.objects.all().count():
+ base_form = CreateWithCatForm
+
+ if request.method == 'POST':
+ form = base_form(request.POST)
+ if form.is_valid():
+ author = PollUser(name=form.cleaned_data['author_name'])
+ author.save()
+ base_url = genRandomURL()
+ admin_url = genRandomURL()
+ category = None
+ if 'category' in form.cleaned_data:
+ category = form.cleaned_data['category']
+ poll = Poll(name=form.cleaned_data['name'],
+description=form.cleaned_data['description'], author=author, base_url=base_url,
+admin_url=admin_url, type=form.cleaned_data['poll_type'], category=category,
+public=form.cleaned_data['public'])
+ poll.save()
+ return HttpResponseRedirect('http://%sedit/%s/' % (
+ response_dct['root_url'], admin_url))
+ else:
+ form = base_form()
+ response_dct['form'] = form
+ return render_to_response('create.html', response_dct)
+
def createOrEdit(request, admin_url):
'''Creation or edition of a poll.
admin_url is given to identify a particular poll
@@ -492,7 +535,7 @@ def poll(request, poll_url):
vote.save()
idx = choices.index(choice)
voter.votes.insert(idx, vote)
- sums = [choice.getSum() for choice in choices]
+ sums = [choice.getSum(poll.type == 'B') for choice in choices]
vote_max = max(sums)
c_idx = 0
while c_idx < len(choices):