diff options
| author | etienne <etienne@07715635-78ed-41b0-aaf1-0afda6c37f35> | 2008-08-20 00:01:23 +0000 |
|---|---|---|
| committer | etienne <etienne@07715635-78ed-41b0-aaf1-0afda6c37f35> | 2008-08-20 00:01:23 +0000 |
| commit | 91ecaf80b3959bc902c713bd8556757b296a2bd0 (patch) | |
| tree | 48ad13ef81c0eaec18804045c73a59278bbe89e0 /polls | |
| parent | b638469a22df4fb92da63a54295347024cb55eb4 (diff) | |
| download | Papillon-91ecaf80b3959bc902c713bd8556757b296a2bd0.tar.bz2 Papillon-91ecaf80b3959bc902c713bd8556757b296a2bd0.zip | |
Internationalization of the application
Diffstat (limited to 'polls')
| -rw-r--r-- | polls/models.py | 23 | ||||
| -rw-r--r-- | polls/views.py | 85 |
2 files changed, 66 insertions, 42 deletions
diff --git a/polls/models.py b/polls/models.py index 08395b8..6e9c9bd 100644 --- a/polls/models.py +++ b/polls/models.py @@ -5,6 +5,7 @@ # See the file COPYING. from django.db import models +from django.utils.translation import gettext_lazy as _ class PollUser(models.Model): name = models.CharField(maxlength=100) @@ -17,19 +18,19 @@ class Poll(models.Model): author = models.ForeignKey(PollUser) base_url = models.CharField(maxlength=100) admin_url = models.CharField(maxlength=100) - STATUS = (('A', 'Available'), - ('D', 'Disabled'),) + STATUS = (('A', _('Available')), + ('D', _('Disabled')),) status = models.CharField(maxlength=1, choices=STATUS) - TYPE = (('M', 'Meeting'), - ('P', 'Poll'), - ('B', 'Balanced poll'), - ('O', 'One choice poll'),) + TYPE = (('M', _('Meeting')), + ('P', _('Poll')), + ('B', _('Balanced poll')), + ('O', _('One choice poll')),) type = models.CharField(maxlength=1, choices=TYPE) - + def getTypeLabel(self): idx = [type[0] for type in self.TYPE].index(self.type) return Poll.TYPE[idx][1] - + class Admin: pass @@ -43,8 +44,8 @@ class Choice(models.Model): class Vote(models.Model): voter = models.ForeignKey(PollUser) choice = models.ForeignKey(Choice) - VOTE = ((-1, 'No'), - (0, 'Maybe'), - (1, 'Yes'),) + VOTE = ((-1, _('No')), + (0, _('Maybe')), + (1, _('Yes')),) value = models.IntegerField(choices=VOTE) diff --git a/polls/views.py b/polls/views.py index 219590c..4e3400b 100644 --- a/polls/views.py +++ b/polls/views.py @@ -4,27 +4,46 @@ # This program can be distributed under the terms of the GNU GPL. # See the file COPYING. +''' +Views management +''' + from random import choice as random_choice import string import time +from django.utils.translation import gettext_lazy as _ from django.shortcuts import render_to_response from django.http import HttpResponseRedirect +from papillon.settings import LANGUAGES from papillon.polls.models import Poll, PollUser, Choice, Vote def getBaseResponse(request): - "Get the root url in order to redirect to the main page" - url = "/".join([request.META['HTTP_HOST'], - request.path.split('/')[1], '']) - return {'root_url':url} + """Manage basic fields for the template + If not null the second argument returned is a redirection. + """ + #Get the root url in order to redirect to the main page + url = "/".join([request.META['HTTP_HOST'], + request.path.split('/')[1], '']) + # setting the current language and available languages + if 'language' in request.GET: + if request.GET['language'] in [language[0] for language in LANGUAGES]: + request.session['django_language'] = request.GET['language'] + return None, HttpResponseRedirect(request.path) + languages = [] + for language_code, language_label in LANGUAGES: + languages.append((language_code, _(language_label))) + return {'root_url':url, 'languages':languages}, None def index(request): "Main page" - response_dct = getBaseResponse(request) + response_dct, redirect = getBaseResponse(request) + if redirect: + return redirect error = '' if 'bad_poll' in request.GET: - response_dct['error'] = "The poll requested don't exist (anymore?)" + response_dct['error'] = _("The poll requested don't exist (anymore?)") return render_to_response('main.html', response_dct) def createOrEdit(request, admin_url): @@ -39,26 +58,26 @@ def createOrEdit(request, admin_url): def genRandomURL(): "Generation of a random url" chars = string.letters + string.digits - url = '' + url = '' for i in xrange(6): url += random_choice(chars) url += str(int(time.time())) return url - + def submitNewPoll(request, response_dct): "A new poll is submited" # verify if all the mandatory_fields are set - mandatory_fields = (('author_name', "Author name"), - ('poll_name', "Poll name"), - ('poll_desc', "Poll description"), - ('poll_type', "Poll type"), + mandatory_fields = (('author_name', _("Author name")), + ('poll_name', _("Poll name")), + ('poll_desc', _("Poll description")), + ('poll_type', _("Poll type")), ) error = "" for key, label in mandatory_fields: if key not in request.POST or not request.POST[key]: # only the first error is reported if not error: - error = "%s is a mandatory field" % label + error = _("%s is a mandatory field") % label else: response_dct[key] = request.POST[key] if error: @@ -76,7 +95,7 @@ admin_url=admin_url, status = 'D', type=request.POST['poll_type']) poll.save() url = response_dct['admin_url'] + '/%s/' % poll.admin_url return response_dct, HttpResponseRedirect(url) - + def getExistingPoll(request, response_dct, admin_url): "Get an existing poll" try: @@ -93,7 +112,7 @@ admin_url=admin_url, status = 'D', type=request.POST['poll_type']) 'poll_status':poll.status, 'type_name':poll.getTypeLabel()} response_dct.update(new_dct) - + # urls base_path = request.META['HTTP_HOST'] + \ "/".join(request.path.split('/')[:-3]) @@ -103,7 +122,7 @@ admin_url=admin_url, status = 'D', type=request.POST['poll_type']) response_dct['admin_url'] += '/%s/' % poll.admin_url response_dct['full_admin_url'] = base_path + "/edit/" \ + admin_url + "/" - + # if a new choice is submitted if 'new_choice' in request.POST and request.POST['new_choice']: try: @@ -112,17 +131,19 @@ admin_url=admin_url, status = 'D', type=request.POST['poll_type']) except IndexError: order = 0 choice = Choice(poll=poll, name=request.POST['new_choice'], - order=order) + order=order) choice.save() - # check if a choice has been choosen for deletion + # check if a choice has been choosen for deletion for key in request.POST: if key.startswith('delete_') and request.POST[key]: choice = Choice.objects.get(id=int(key[len('delete_'):])) Vote.objects.filter(choice=choice).delete() choice.delete() return response_dct, None - - response_dct = getBaseResponse(request) + + response_dct, redirect = getBaseResponse(request) + if redirect: + return redirect response_dct['TYPES'] = Poll.TYPE response_dct['admin_url'] = \ "/".join(request.path.split('/')[:-2]) @@ -136,7 +157,7 @@ admin_url=admin_url, status = 'D', type=request.POST['poll_type']) response_dct['admin_url'] += '/0/' else: # existing poll - response_dct, redirection = getExistingPoll(request, + response_dct, redirection = getExistingPoll(request, response_dct, admin_url) if redirection: return redirection @@ -147,7 +168,7 @@ def poll(request, poll_url): """Display a poll poll_url is given to identify the poll """ - + def modifyVote(request, choices): "Modify user's votes" try: @@ -155,7 +176,7 @@ def poll(request, poll_url): id=int(request.POST['voter']))[0] except (ValueError, IndexError): return - # if no author_name is given deletion of associated votes and + # if no author_name is given deletion of associated votes and # author if not request.POST['author_name']: for choice in choices: @@ -215,7 +236,7 @@ def poll(request, poll_url): author = PollUser(name=request.POST['author_name']) author.save() selected_choices = [] - + # set the selected choices for key in request.POST: if key.startswith('choice_') and request.POST[key]: @@ -236,7 +257,9 @@ def poll(request, poll_url): v = Vote(voter=author, choice=choice, value=0) v.save() - response_dct = getBaseResponse(request) + response_dct, redirect = getBaseResponse(request) + if redirect: + return redirect try: poll = Poll.objects.filter(base_url=poll_url)[0] except IndexError: @@ -244,11 +267,11 @@ def poll(request, poll_url): choices = Choice.objects.filter(poll=poll).order_by('order') # if the poll don't exist or if it has no choices the user is # redirected to the main page - if not choices or not poll: + if not choices or not poll: url = "/".join(request.path.split('/')[:-3]) url += "/?bad_poll=1" return HttpResponseRedirect(url) - + # a vote is submitted if 'author_name' in request.POST: if 'voter' in request.POST: @@ -256,27 +279,27 @@ def poll(request, poll_url): modifyVote(request, choices) else: newVote(request, choices) - + # 'voter' is in request.GET when the edit button is pushed if 'voter' in request.GET: try: response_dct['current_voter_id'] = int(request.GET['voter']) except ValueError: pass - + response_dct.update({'choices':choices, 'poll_type_name':poll.getTypeLabel(), 'poll_name':poll.name, 'poll_desc':poll.description}) response_dct['base_url'] = "/".join(request.path.split('/')[:-2]) \ + '/%s/' % poll.base_url - + # get voters and sum for each choice for this poll votes = [] # all votes for this poll votes = Vote.objects.extra(where=['choice_id IN (%s)' \ % ",".join([str(choice.id) for choice in choices])]) - + voters = [] choices_sum = [0 for choice in choices] choices_ids = [choice.id for choice in choices] |
