Commits

Carl Meyer committed 396016d

Added localeurl_future template tag library with locale_url tag that uses new Django 1.3 url tag syntax and won't dump deprecation warnings. Fixes #17

  • Participants
  • Parent commits 2fbc5a6

Comments (0)

Files changed (5)

File docs/history.rst

 ---------
 
 Tip: (unreleased)
+  * Add ``localeurl_future`` template tag library to provide ``locale_url`` tag
+    that wraps the new Django ``url`` tag to allow using the new syntax and
+    avoid deprecation warnings under Django 1.3. Fixes #17. Thanks Fabian
+    Büchler for the report.
   * Account for reverse() receiving kwargs=None. Fixes #18. Thanks Alexander
     Clausen for report and tests, Joel Ryan for draft patch.
   * Prevent change_locale view from stripping query params from ``next``.
 
 Release 1.4: (2010-03-19)
   * Moved localeurl settings from localeurl/__init__.py to
-    localeurl/settings.py.  
+    localeurl/settings.py.
   * Added ``LocaleurlSitemap`` for easier creation of multilingual
     sitemaps.
   * Added ``LOCALEURL_USE_ACCEPT_LANGUAGE`` setting to check HTTP

File docs/usage.rst

 ``locale_url`` tag. This tag behaves exactly like the standard ``url`` tag,
 except you specify a language.
 
+.. note::
+
+   In Django 1.3 and later, using the ``locale_url`` tag from
+   ``localeurl_tags`` will result in a deprecation warning about changed url
+   tag syntax. To avoid this warning, ``{% load locale_url from
+   localeurl_future %}`` in your template after you ``{% load localeurl_tags
+   %}``. This also requires that you adopt the `new url tag syntax`_.
+
+.. _new url tag syntax: http://docs.djangoproject.com/en/1.3/releases/1.3/#changes-to-url-and-ssi
+
 Example
 -------
 
 
   <a href="{% locale_url "de" articles.views.display id=article.id %}">Show article in German</a>
 
+If you are using Django 1.3+ and you loaded ``locale_url`` from the
+``localeurl_future`` library, you'd need quotes around the view name::
+
+  <a href="{% locale_url "de" "articles.views.display" id=article.id %}">Show article in German</a>
 
 The ``chlocale`` filter
 ~~~~~~~~~~~~~~~~~~~~~~~

File localeurl/templatetags/localeurl_future.py

+from django import template
+from django.templatetags.future import url
+from django.utils.functional import wraps
+
+from localeurl_tags import locale_url
+
+
+register = template.Library()
+
+
+def locale_url_wrapper(parser, token):
+    return locale_url(parser, token, django_url_tag=url)
+
+
+locale_url_wrapper = wraps(locale_url)(locale_url_wrapper)
+
+
+
+register.tag('locale_url', locale_url_wrapper)

File localeurl/templatetags/localeurl_tags.py

 from django.template import Node, Token, TemplateSyntaxError
 from django.template import resolve_variable, defaulttags
 from django.template.defaultfilters import stringfilter
+from django.utils.functional import wraps
+
 from localeurl import utils
 
 register = template.Library()
 
 
+
 def chlocale(url, locale):
     """
     Changes the URL's locale prefix if the path is not locale-independent.
     _, path = utils.strip_path(path)
     return utils.locale_url(path, locale)
 
+
 chlocale = stringfilter(chlocale)
 register.filter('chlocale', chlocale)
 
 
+
 def rmlocale(url):
     """Removes the locale prefix from the URL."""
     script_prefix, path = utils.strip_script_prefix(url)
     _, path = utils.strip_path(path)
     return ''.join([script_prefix, path])
 
+
 rmlocale = stringfilter(rmlocale)
 register.filter('rmlocale', rmlocale)
 
 
-def locale_url(parser, token):
+
+def locale_url(parser, token, django_url_tag):
     """
     Renders the url for the view with another locale prefix. The syntax is
     like the 'url' tag, only with a locale before the view.
         raise TemplateSyntaxError("'%s' takes at least two arguments:"
                 " the locale and a view" % bits[0])
     urltoken = Token(token.token_type, bits[0] + ' ' + ' '.join(bits[2:]))
-    urlnode = defaulttags.url(parser, urltoken)
+    urlnode = django_url_tag(parser, urltoken)
     return LocaleURLNode(bits[1], urlnode)
 
+
 class LocaleURLNode(Node):
     def __init__(self, locale, urlnode):
         self.locale = locale
         else:
             return chlocale(path, locale)
 
-register.tag('locale_url', locale_url)
+
+
+def locale_url_wrapper(parser, token):
+    return locale_url(parser, token, django_url_tag=defaulttags.url)
+
+
+locale_url_wrapper = wraps(locale_url)(locale_url_wrapper)
+
+
+
+register.tag('locale_url', locale_url_wrapper)

File localeurl/tests/__init__.py

 """
 import re
 
+from django import template
 from django.core import urlresolvers
 from django.test import TestCase
-from django import template
+from django.utils import translation
 
 from localeurl import middleware, settings as localeurl_settings, utils
 from localeurl.models import reverse
     def setUp(self):
         self.settings_manager = test_utils.TestSettingsManager()
         settings_fixture(self.settings_manager)
+        translation.activate("en")
         reload(localeurl_settings)
         reload(urlresolvers)
 
         self.assertEqual('/fr/dummy/', self.render_template(
                 '{{"/nl-nl/dummy/"|chlocale:"fr"}}'))
 
+try:
+    from localeurl.templatetags import localeurl_future
+except ImportError:
+    localeurl_future = None
+
+
+# @@@ use proper test skipping once Django 1.2 is minimum version
+if localeurl_future is not None:
+    class FutureTagsTestCase(LocaleurlTestCase):
+        def render_template(self, text):
+            t = test_utils.TestTemplate(text, libraries=[localeurl_future.register])
+            c = template.Context()
+            return t.render(c)
+
+
+        def test_locale_url_tag(self):
+            self.assertRaises(ValueError, self.render_template,
+                    '{% locale_url "nl" "dummy0" %}')
+
+            self.assertEqual('/en/dummy/', self.render_template(
+                    '{% locale_url "en-us" "dummy0" %}'))
+
+            self.assertEqual('/fr/dummy/4', self.render_template(
+                '{% locale_url "fr" "dummy1" test=4 %}'))
+
+            self.assertEqual('/en/dummy/4', self.render_template(
+                '{% locale_url "en" "dummy1" test=4 as testvar %}{{ testvar }}'))
+
+
+        def test_locale_url_tag_no_default_prefix(self):
+            self.settings_manager.set(PREFIX_DEFAULT_LOCALE=False)
+            reload(localeurl_settings)
+
+            self.assertEqual('/dummy/', self.render_template(
+                    '{% locale_url "en-us" "dummy0" %}'))
+
+            self.assertEqual('/fr/dummy/', self.render_template(
+                '{% locale_url "fr" "dummy0" %}'))
+
 
 
 class DummyModel(object):