Skip to content

Commit

Permalink
remora_url helper to create URLs independently from Zamboni logic (bu…
Browse files Browse the repository at this point in the history
…g 552507)
  • Loading branch information
Fred Wenzel committed Mar 16, 2010
1 parent cd727e8 commit c5cdae5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
11 changes: 6 additions & 5 deletions apps/addons/templates/addons/details.html
Expand Up @@ -195,15 +195,15 @@ <h3>{{ _('What do you think?') }}</h3>
<div id="review-box" class="highlight">
{% if not user.is_authenticated() %}
<p>
{% trans login='/users/login' %}{# TODO reverse url #}
{% trans login=remora_url('/users/login') %}{# TODO reverse url #}
Please <a href="{{ login }}">log in</a> to submit a review
{% endtrans %}
</p>
{% endif %}

{# TODO uncakeify #}
<form class="addon-feedback" method="post"
action="{{ locale_url('%s/reviews/add/%s' % (APP.short, addon.id)) }}">
action="{{ remora_url('/reviews/add/{0}')|f(addon.id) }}">
{{ cake_csrf_token() }}
{% with disabled = ('' if user.is_authenticated() else
' disabled="disabled"') %}
Expand Down Expand Up @@ -437,6 +437,7 @@ <h3 class="compact-bottom">{{ _('Need help with this add-on?') }}</h3>
_('E-mail your question') }}</a></li>
{% endif %}
{% if addon.support_url %}
{# TODO linkification #}
<li><a href="{{ addon.support_url }}">{{
_('Visit the support site') }}</a></li>
{% endif %}
Expand All @@ -461,7 +462,7 @@ <h3 class="compact-bottom">{{ _('Tags', 'tags_header_tags_title') }}</h3>
{% if user.is_authenticated() %}
<a href="#" id="addatag">{{ _('Add a tag') }}</a>
<div class="addtagform">
<form id="tagForm" action="{{ locale_url(APP.short+'/tags/add/') }}"
<form id="tagForm" action="{{ remora_url('/tags/add/') }}"
method="post">{# TODO reverse URL #}
{{ cake_csrf_token() }}
<input type="text" id="newTag" name="newTag"/>
Expand Down Expand Up @@ -530,9 +531,9 @@ <h3 class="compact-bottom">{{ _('Related Collections') }}</h3>

{% if user.is_authenticated() %}
{# TODO reverse URLs #}
<form action="{{ locale_url(APP.short+'/collections/addtocollection') }}"
<form action="{{ remora_url('/collections/addtocollection') }}"
method="post" id="coll_publish"
data-coll-url="{{ locale_url(APP.short+'/collections') }}">
data-coll-url="{{ remora_url('/collections') }}">
<div>
<a href="/collections/">{{ _("What's this?", 'addons_display_a_license_what') }}</a>
<h3>
Expand Down
23 changes: 21 additions & 2 deletions apps/cake/helpers.py
Expand Up @@ -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

Expand Down Expand Up @@ -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)
24 changes: 21 additions & 3 deletions 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):
Expand Down Expand Up @@ -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')

0 comments on commit c5cdae5

Please sign in to comment.