From e41e52768085a81b1b5d48f5f0583b6449179fa8 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 25 Oct 2011 00:17:19 +0200 Subject: Correct hard link in templates and views (closes #309) --- papillon/polls/feeds.py | 2 +- papillon/polls/views.py | 41 ++++++++++++++++---------------- papillon/templates/base.html | 4 ++-- papillon/templates/category.html | 2 +- papillon/templates/create.html | 2 +- papillon/templates/edit.html | 14 +++++------ papillon/templates/editChoices.html | 2 +- papillon/templates/editChoicesAdmin.html | 10 ++++---- papillon/templates/editChoicesUser.html | 8 +++---- papillon/templates/main.html | 6 ++--- papillon/templates/vote.html | 14 +++++------ papillon/urls.py | 33 +++++++++++++------------ 12 files changed, 70 insertions(+), 68 deletions(-) diff --git a/papillon/polls/feeds.py b/papillon/polls/feeds.py index 2d52dc7..5c11b9c 100644 --- a/papillon/polls/feeds.py +++ b/papillon/polls/feeds.py @@ -52,4 +52,4 @@ class PollLatestEntries(Feed): def items(self, obj): voters = Voter.objects.filter(poll__id=obj.id).\ order_by('-modification_date')[:10] - return voters \ No newline at end of file + return voters diff --git a/papillon/polls/views.py b/papillon/polls/views.py index 300b313..75823be 100644 --- a/papillon/polls/views.py +++ b/papillon/polls/views.py @@ -26,11 +26,12 @@ import string import time from datetime import datetime -from django.utils.translation import gettext_lazy as _ from django.shortcuts import render_to_response from django.http import HttpResponseRedirect +from django.conf import settings +from django.utils.translation import gettext_lazy as _ +from django.core.urlresolvers import reverse -from papillon.settings import LANGUAGES, BASE_SITE from papillon.polls.models import Poll, PollUser, Choice, Voter, Vote, \ Category, Comment from papillon.polls.forms import CreatePollForm, AdminPollForm, ChoiceForm, \ @@ -40,16 +41,17 @@ def getBaseResponse(request): """Manage basic fields for the template If not null the second argument returned is a redirection. """ - url = BASE_SITE # setting the current language and available languages if 'language' in request.GET: - if request.GET['language'] in [language[0] for language in LANGUAGES]: + if request.GET['language'] in \ + [language[0] for language in settings.LANGUAGES]: request.session['django_language'] = request.GET['language'] return None, HttpResponseRedirect(request.path) languages = [] - for language_code, language_label in LANGUAGES: + for language_code, language_label in settings.LANGUAGES: languages.append((language_code, language_label)) - return {'root_url':url, 'languages':languages}, None + return {'media_url':settings.MEDIA_URL, 'languages':languages, + 'admin_url':settings.ADMIN_MEDIA_PREFIX,}, None def index(request): "Main page" @@ -99,8 +101,8 @@ def create(request): poll.admin_url = genRandomURL() poll.base_url = genRandomURL() poll.save() - return HttpResponseRedirect('%seditChoicesAdmin/%s/' % ( - response_dct['root_url'], poll.admin_url)) + return HttpResponseRedirect(reverse('edit_choices_admin', + args=[poll.admin_url])) else: form = CreatePollForm() response_dct['form'] = form @@ -116,17 +118,15 @@ def edit(request, admin_url): poll = Poll.objects.filter(admin_url=admin_url)[0] except IndexError: # if the poll don't exist redirect to the creation page - url = response_dct['root_url'] - return HttpResponseRedirect('%screate' % ( - response_dct['root_url'])) + return HttpResponseRedirect(reverse('create')) Form = AdminPollForm if request.method == 'POST': form = Form(request.POST, instance=poll) if form.is_valid(): poll = form.save() - return HttpResponseRedirect('%sedit/%s/' % ( - response_dct['root_url'], poll.admin_url)) + return HttpResponseRedirect(reverse('edit', + args=[poll.admin_url])) else: form = Form(instance=poll) response_dct['form'] = form @@ -141,8 +141,7 @@ def editChoicesAdmin(request, admin_url): poll = Poll.objects.filter(admin_url=admin_url)[0] except IndexError: # if the poll don't exist redirect to the main page - url = "/".join(request.path.split('/')[:-2]) - return response_dct, HttpResponseRedirect(url) + return HttpResponseRedirect(reverse('index')) response_dct['poll'] = poll return editChoices(request, response_dct, admin=True) @@ -156,8 +155,7 @@ def editChoicesUser(request, poll_url): poll = None if not poll or not poll.opened_admin: # if the poll don't exist redirect to the main page - url = "/".join(request.path.split('/')[:-2]) - return HttpResponseRedirect(url) + return HttpResponseRedirect(reverse('index')) response_dct['poll'] = poll return editChoices(request, response_dct) @@ -215,7 +213,10 @@ def editChoices(request, response_dct, admin=False): if admin and request.method == 'GET': for key in request.GET: try: - current_url = request.path.split('?')[0] + current_url = reverse('edit_choices_admin', + args=[poll.admin_url]) if admin else \ + reverse('edit_choices_user', + args=[poll.poll_url]) if 'up_choice' in key: choice = Choice.objects.get(id=int(request.GET[key])) if choice.poll != poll: @@ -411,9 +412,7 @@ def poll(request, poll_url): # 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: - url = "/".join(request.path.split('/')[:-3]) - url += "/?bad_poll=1" - return HttpResponseRedirect(url) + return HttpResponseRedirect(reverse('index')) # a vote is submitted if 'author_name' in request.POST and poll.open: diff --git a/papillon/templates/base.html b/papillon/templates/base.html index b673f24..5da20fb 100644 --- a/papillon/templates/base.html +++ b/papillon/templates/base.html @@ -2,7 +2,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> - + {% block title %}Papillon{% endblock %} {% block fullscript %}{% endblock %} @@ -13,7 +13,7 @@ {% for language in languages%}{{language.1}}{% endfor %}
-

Papillon

+

Papillon

{% block content %}{% endblock %} diff --git a/papillon/templates/category.html b/papillon/templates/category.html index 55aa084..f38743a 100644 --- a/papillon/templates/category.html +++ b/papillon/templates/category.html @@ -8,7 +8,7 @@ {% if polls %}

{%trans "Polls"%}

{%endif%} {% for poll in polls %}
-

{{poll.name}}

+

{{poll.name}}

{{poll.description|safe}}

{% endfor %} diff --git a/papillon/templates/create.html b/papillon/templates/create.html index 7c3d2b3..224ae78 100644 --- a/papillon/templates/create.html +++ b/papillon/templates/create.html @@ -9,7 +9,7 @@ {% block content %}

{% trans "New poll" %}

-
+ {% for field in form %} {% if field.is_hidden %} diff --git a/papillon/templates/edit.html b/papillon/templates/edit.html index 7ef4f14..b2b38a5 100644 --- a/papillon/templates/edit.html +++ b/papillon/templates/edit.html @@ -3,20 +3,20 @@ {% load i18n %} {% block fullscript %} - - - + + + {{ form.media }} {% endblock %} {% block content %}

{% trans "Edit poll" %}

- +
-{{root_url}}poll/{{poll.base_url}} +{% url poll poll.base_url%}

{% trans "Copy this address and send it to voters who want to participate to this poll" %} @@ -25,7 +25,7 @@

-{{root_url}}edit/{{poll.admin_url}} +{% url edit poll.admin_url %}

{% trans "Address to modify the current poll" %} @@ -34,7 +34,7 @@

-{{root_url}}editChoicesAdmin/{{poll.admin_url}} +{% url edit_choices_admin poll.admin_url %}

{% trans "Address to modify choices of the current poll." %} diff --git a/papillon/templates/editChoices.html b/papillon/templates/editChoices.html index 1082d30..7e059a8 100644 --- a/papillon/templates/editChoices.html +++ b/papillon/templates/editChoices.html @@ -3,7 +3,7 @@

{% trans "New choice" %}

{%if form_new_choice.errors %}

{{form_new_choice.errors}}

{%endif%} - + {{form_new_choice.poll}} {{form_new_choice.order}} diff --git a/papillon/templates/editChoicesAdmin.html b/papillon/templates/editChoicesAdmin.html index b04b0ec..6510dcc 100644 --- a/papillon/templates/editChoicesAdmin.html +++ b/papillon/templates/editChoicesAdmin.html @@ -3,9 +3,9 @@ {% load i18n %} {% block fullscript %} - - - + + + {{ form_new_choice.media }} {% endblock %} @@ -14,7 +14,7 @@ {% blocktrans %}As long as no options were added to the poll, it will not be available.{% endblocktrans %}

{% else %}

{% trans "Complete/Finalise the poll" %}

-

+

{% endif %} {% include 'editChoices.html' %} {% if choices %} @@ -27,7 +27,7 @@ {% for choice in choices %} - + {{choice.form.poll}}{{choice.form.order}} {%if not poll.dated_choices%}{%endif%} diff --git a/papillon/templates/editChoicesUser.html b/papillon/templates/editChoicesUser.html index 0d2b2c1..47d6a52 100644 --- a/papillon/templates/editChoicesUser.html +++ b/papillon/templates/editChoicesUser.html @@ -3,14 +3,14 @@ {% load i18n %} {% block fullscript %} - - - + + + {{ form_new_choice.media }} {% endblock %} {% block content %} -

{%trans "Return to the poll"%}

+

{%trans "Return to the poll"%}

{% trans "Choices" %}

{% if choices %}
{% trans "Delete?"%}
+ / -
diff --git a/papillon/templates/main.html b/papillon/templates/main.html index 49088f0..f27a97b 100644 --- a/papillon/templates/main.html +++ b/papillon/templates/main.html @@ -3,20 +3,20 @@ {% block content %} {% if error %}

{{error}}

{%endif%} -

{%trans "Create a poll"%}

+

{%trans "Create a poll"%}

{% trans "Create a new sondage for take a decision, find a date for a meeting, etc." %} {% trans "It's here!" %}

{% if polls %}

{%trans "Public polls"%}

{%endif%} {% for poll in polls %}
-

{{poll.name}}

+

{{poll.name}}

{{poll.description|safe}}

{% endfor %} {% if categories %}

{%trans "Categories"%}

{% endif %} {% for category in categories %} -

{{category.name}}

+

{{category.name}}

{% endfor %} {% endblock %} diff --git a/papillon/templates/vote.html b/papillon/templates/vote.html index 94d1898..dc384f5 100644 --- a/papillon/templates/vote.html +++ b/papillon/templates/vote.html @@ -3,9 +3,9 @@ {% load get_range %} {% block fullscript %} - - - + + + {{ form_comment.media }} {% endblock %} @@ -14,7 +14,7 @@ {% if error %}

{{ error }}

{% endif %} {% if not poll.open %}

{% trans "The current poll is closed."%}

{% endif %}

{{ poll.description|safe }}

- +
@@ -119,15 +119,15 @@
{%if poll.opened_admin%} -

{%trans "Add a new choice to this poll?"%}

{%endif%} +

{%trans "Add a new choice to this poll?"%}

{%endif%}
{%if hide_vote%}

{% trans "You have already vote? You are enough wise not to be influenced by other votes? You can display result by clicking" %} {% trans "here" %}.

{%else%} -

{% trans "Remain informed of poll evolution:" %} {%trans "syndication"%}

{%endif%} +

{% trans "Remain informed of poll evolution:" %} {%trans "syndication"%}

{%endif%}
{%if not hide_vote%}

{%trans "Comments"%}

- {%if poll.open%}
+ {%if poll.open%}
diff --git a/papillon/urls.py b/papillon/urls.py index 9d1d8a3..30f95be 100644 --- a/papillon/urls.py +++ b/papillon/urls.py @@ -35,22 +35,25 @@ if not base.endswith('/'): urlpatterns = patterns('', (base + r'admin/doc/', include('django.contrib.admindocs.urls')), - (base + r'admin/jsi18n/$', 'django.views.i18n.javascript_catalog'), + url(base + r'admin/jsi18n/$', 'django.views.i18n.javascript_catalog', + name='admin_i18n'), (base + r'admin/(.*)', admin.site.root), - (base + r'$', 'papillon.polls.views.index'), - (base + r'create$', 'papillon.polls.views.create'), - (base + r'edit/(?P\w+)/$', - 'papillon.polls.views.edit'), - (base + r'editChoicesAdmin/(?P\w+)/$', - 'papillon.polls.views.editChoicesAdmin'), - (base + r'editChoicesUser/(?P\w+)/$', - 'papillon.polls.views.editChoicesUser'), - (base + r'category/(?P\w+)/$', - 'papillon.polls.views.category'), - (base + r'poll/(?P\w+)/$', 'papillon.polls.views.poll'), - (base + r'poll/(?P\w+)/vote$', 'papillon.polls.views.poll'), - (base + r'feeds/(?P.*)$', - 'django.contrib.syndication.views.feed', {'feed_dict': feeds}), + url(base + r'$', 'papillon.polls.views.index', name='index'), + url(base + r'create/$', 'papillon.polls.views.create', name='create'), + url(base + r'edit/(?P\w+)/$', + 'papillon.polls.views.edit', name='edit'), + url(base + r'editChoicesAdmin/(?P\w+)/$', + 'papillon.polls.views.editChoicesAdmin', name='edit_choices_admin'), + url(base + r'editChoicesUser/(?P\w+)/$', + 'papillon.polls.views.editChoicesUser', name='edit_choices_user'), + url(base + r'category/(?P\w+)/$', + 'papillon.polls.views.category', name='category'), + url(base + r'poll/(?P\w+)/$', 'papillon.polls.views.poll', + name='poll'), + url(base + r'poll/(?P\w+)/vote/$', 'papillon.polls.views.poll', + name='vote'), + url(base + r'feeds/(?P.*)$', 'django.contrib.syndication.views.feed', + {'feed_dict': feeds}, name='feed'), (base + r'static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.PROJECT_PATH + '/static'}), (base + r'media/(?P.*)$', 'django.views.static.serve', -- cgit v1.2.3