diff options
author | etienne <etienne@07715635-78ed-41b0-aaf1-0afda6c37f35> | 2009-06-08 00:15:00 +0000 |
---|---|---|
committer | etienne <etienne@07715635-78ed-41b0-aaf1-0afda6c37f35> | 2009-06-08 00:15:00 +0000 |
commit | 54550d2091d2254fd20fbeefdc82dc56a69611fe (patch) | |
tree | 69b2158739967fe8894188be09dfaf35db2c18fb | |
parent | 3e95d82ac2ccd24dbe331d4047ae243144dfac25 (diff) | |
download | Papillon-54550d2091d2254fd20fbeefdc82dc56a69611fe.tar.bz2 Papillon-54550d2091d2254fd20fbeefdc82dc56a69611fe.zip |
Using newforms infor the create form
-rw-r--r-- | polls/forms.py | 51 | ||||
-rw-r--r-- | polls/models.py | 10 | ||||
-rw-r--r-- | polls/views.py | 45 | ||||
-rw-r--r-- | settings.py | 1 | ||||
-rw-r--r-- | static/styles.css | 4 | ||||
-rw-r--r-- | templates/create.html | 57 | ||||
-rw-r--r-- | templates/main.html | 4 | ||||
-rw-r--r-- | urls.py | 1 |
8 files changed, 164 insertions, 9 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): diff --git a/settings.py b/settings.py index 0118dc3..081d7ca 100644 --- a/settings.py +++ b/settings.py @@ -88,6 +88,7 @@ INSTALLED_APPS = ( 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', + 'django.contrib.markup', 'papillon.polls', ) diff --git a/static/styles.css b/static/styles.css index 141aa46..605fef3 100644 --- a/static/styles.css +++ b/static/styles.css @@ -143,7 +143,7 @@ color:#808080; text-decoration:none; } -.alert, .error{ +.alert, .error, .errorlist{ color:blue; } @@ -309,4 +309,4 @@ text-align:center; .comments .submit{ width:auto; -}
\ No newline at end of file +} diff --git a/templates/create.html b/templates/create.html new file mode 100644 index 0000000..63ae021 --- /dev/null +++ b/templates/create.html @@ -0,0 +1,57 @@ +{% extends "base.html" %} +{% load i18n %} +{% load markup %} + +{% block content %} + <h2>{% trans "New poll" %}</h2> +<form action="http://{{root_url}}create" method="post"> +<table class='new_poll'> + <tr><td colspan='3'>{{form.author_name.errors}}</td></tr> + <tr> + <td><label for='author_name'>{{form.author_name.label}}</label></td> + <td>{{form.author_name}}</td> + <td class='form_description'>{{form.author_name.help_text}}</td> + </tr> + + <tr><td colspan='3'>{{form.name.errors}}</td></tr> + <tr> + <td><label for='name'>{{form.name.label}}</label></td> + <td>{{form.name}}</td> + <td class='form_description'>{{form.name.help_text}}</td> + </tr> + + <tr><td colspan='3'>{{form.description.errors}}</td></tr> + <tr> + <td><label for='description'>{{form.description.label}}</label></td> + <td>{{form.description}}</td> + <td class='form_description'>{{form.description.help_text}}</td> + </tr> +{% if form.category %} + <tr><td colspan='3'>{{form.category.errors}}</td></tr> + <tr> + <td><label for='category'>{{form.category.label}}</label></td> + <td>{{form.category}}</td> + <td class='form_description'>{{form.category.help_text}}</td> + </tr> +{% endif %} + <tr><td colspan='3'>{{form.public.errors}}</td></tr> + <tr> + <td><label for='public'>{{form.public.label}}</label></td> + <td>{{form.public}}</td> + <td class='form_description'>{{form.public.help_text}}</td> + </tr> + + <tr><td colspan='3'>{{form.poll_type.errors}}</td></tr> + <tr> + <td><label for='poll_type'>{{form.poll_type.label}}</label></td> + <td>{{form.poll_type}}</td> + <td class='form_description'>{{form.poll_type.help_text|restructuredtext}}</td> + </tr> + <tr> + <td></td> + <td><input type='submit' value='{% trans "Create" %}' class='submit'/></td> + </tr> +</table> +</form> + +{% endblock %} diff --git a/templates/main.html b/templates/main.html index 16d7a1e..8a09830 100644 --- a/templates/main.html +++ b/templates/main.html @@ -3,8 +3,8 @@ {% block content %} {% if error %}<p class='error'>{{error}}</p>{%endif%} -<h2><a href='edit/0'>{%trans "Create a poll"%}</a></h2> -<p>{% trans "Create a new sondage for take a decision, find a date for a meeting, etc." %} <a href='edit/0'>{% trans "It's here!" %}</a></p> +<h2><a href='create'>{%trans "Create a poll"%}</a></h2> +<p>{% trans "Create a new sondage for take a decision, find a date for a meeting, etc." %} <a href='create'>{% trans "It's here!" %}</a></p> {% if polls %}<h2>{%trans "Public polls"%}</h2>{%endif%} {% for poll in polls %} @@ -30,6 +30,7 @@ feeds = { urlpatterns = patterns('', (r'^papillon/admin/(.*)', admin.site.root), (r'^papillon/$', 'papillon.polls.views.index'), + (r'^papillon/create$', 'papillon.polls.views.create'), (r'^papillon/edit/(?P<admin_url>\w+)/$', 'papillon.polls.views.createOrEdit'), (r'^papillon/category/(?P<category_id>\w+)/$', |