From c5cdae575220d38098508832968051f3e86c2329 Mon Sep 17 00:00:00 2001 From: Fred Wenzel Date: Tue, 16 Mar 2010 09:09:26 +0100 Subject: [PATCH] remora_url helper to create URLs independently from Zamboni logic (bug 552507) --- apps/addons/templates/addons/details.html | 11 ++++++----- apps/cake/helpers.py | 23 ++++++++++++++++++++-- apps/cake/tests.py | 24 ++++++++++++++++++++--- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/apps/addons/templates/addons/details.html b/apps/addons/templates/addons/details.html index 24f0a66a1c7..ff9045acdc8 100644 --- a/apps/addons/templates/addons/details.html +++ b/apps/addons/templates/addons/details.html @@ -195,7 +195,7 @@

{{ _('What do you think?') }}

{% if not user.is_authenticated() %}

- {% trans login='/users/login' %}{# TODO reverse url #} + {% trans login=remora_url('/users/login') %}{# TODO reverse url #} Please log in to submit a review {% endtrans %}

@@ -203,7 +203,7 @@

{{ _('What do you think?') }}

{# TODO uncakeify #}
+ action="{{ remora_url('/reviews/add/{0}')|f(addon.id) }}"> {{ cake_csrf_token() }} {% with disabled = ('' if user.is_authenticated() else ' disabled="disabled"') %} @@ -437,6 +437,7 @@

{{ _('Need help with this add-on?') }}

_('E-mail your question') }} {% endif %} {% if addon.support_url %} + {# TODO linkification #}
  • {{ _('Visit the support site') }}
  • {% endif %} @@ -461,7 +462,7 @@

    {{ _('Tags', 'tags_header_tags_title') }}

    {% if user.is_authenticated() %} {{ _('Add a tag') }}
    - {# TODO reverse URL #} {{ cake_csrf_token() }} @@ -530,9 +531,9 @@

    {{ _('Related Collections') }}

    {% if user.is_authenticated() %} {# TODO reverse URLs #} - + data-coll-url="{{ remora_url('/collections') }}">
    {{ _("What's this?", 'addons_display_a_license_what') }}

    diff --git a/apps/cake/helpers.py b/apps/cake/helpers.py index 3f81867b1af..0c8a1e7e9aa 100644 --- a/apps/cake/helpers.py +++ b/apps/cake/helpers.py @@ -2,10 +2,10 @@ from time import time from django.conf import settings - -import jinja2 +from django.utils import translation from jingo import register, env +import jinja2 from .models import Session as CakeSession @@ -34,3 +34,22 @@ def cake_csrf_token(context): except CakeSession.DoesNotExist: return + + +@register.function +@jinja2.contextfunction +def remora_url(context, url, lang=None, app=None, prefix=''): + """ + Builds a remora-style URL, independent from Zamboni's prefixer logic. + If app and/or lang are None, the current Zamboni values will be used. + To omit them from the URL, set them to ''. + """ + if lang is None: + lang = translation.to_locale(context['LANG']).replace('_', '-') + if app is None: + app = context['APP'].short + + url_parts = [prefix, lang, app, url] + url_parts = [p.strip('/') for p in url_parts if p] + + return '/'+'/'.join(url_parts) diff --git a/apps/cake/tests.py b/apps/cake/tests.py index a8ff84601a2..60dfb15498f 100644 --- a/apps/cake/tests.py +++ b/apps/cake/tests.py @@ -1,13 +1,16 @@ from django.contrib.auth.models import AnonymousUser from mock import Mock +from nose.tools import eq_ from test_utils import TestCase from pyquery import PyQuery as pq -from cake.models import Session +import amo from users.models import UserProfile -from cake.backends import SessionBackend -from cake.helpers import cake_csrf_token +from .backends import SessionBackend +from .models import Session +from .helpers import cake_csrf_token, remora_url + class CakeTestCase(TestCase): @@ -115,3 +118,18 @@ def test_csrf_token_nosession(self): token = cake_csrf_token(ctx) assert not token + + def test_remora_url(self): + """Build remora URLs.""" + ctx = { + 'LANG': 'en-us', + 'APP': amo.FIREFOX + } + url = remora_url(ctx, '/addon/1234') + eq_(url, '/en-US/firefox/addon/1234') + + url = remora_url(ctx, '/addon/1234', 'pt-BR', 'thunderbird') + eq_(url, '/pt-BR/thunderbird/addon/1234') + + url = remora_url(ctx, '/devhub/something', app='', prefix='remora') + eq_(url, '/remora/en-US/devhub/something')