Skip to content

Commit

Permalink
Improve get_translated_url, use request.resolver_match instead.
Browse files Browse the repository at this point in the history
This value is already set in `WSGIHandler.get_response()`,
so there is no need to perform a `resolve()` manually.
It also avoids issues wityh 404 pages much easier.
  • Loading branch information
vdboor committed Dec 5, 2014
1 parent 428aee1 commit cd36852
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changes in development (git version)
* Added ``parler.contrib.rest_framework`` package for django-rest-framework_ integration.
* Detect when translatable fields are assigned too early.
* Fix missing 404 check in delete-translation view.
* Improve ``{% get_translated_url %}``, using ``request.resolver_match`` value.


Changes in version 1.2.1 (2014-10-31)
Expand Down
13 changes: 6 additions & 7 deletions parler/templatetags/parler_tags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inspect
from django.core.urlresolvers import resolve, reverse, Resolver404
from django.core.urlresolvers import reverse
from django.template import Node, Library, TemplateSyntaxError
from django.utils.translation import get_language
from django.utils import six
Expand Down Expand Up @@ -135,17 +135,16 @@ def get_translated_url(context, lang_code, object=None):

# Just reverse the current URL again in a new language, and see where we end up.
# This doesn't handle translated slugs, but will resolve to the proper view name.
path = context['request'].path
try:
resolvermatch = resolve(path)
except Resolver404:
request = context['request']
resolver_match = request.resolver_match # Set by BaseHandler.get_response(), reading resolve(request.path_info)
if resolver_match is None:
# Can't resolve the page itself, the page is apparently a 404.
# This can also happen for the homepage in an i18n_patterns situation.
return ''

with smart_override(lang_code):
clean_kwargs = _cleanup_urlpattern_kwargs(resolvermatch.kwargs)
return reverse(resolvermatch.view_name, args=resolvermatch.args, kwargs=clean_kwargs)
clean_kwargs = _cleanup_urlpattern_kwargs(resolver_match.kwargs)
return reverse(resolver_match.view_name, args=resolver_match.args, kwargs=clean_kwargs, current_app=resolver_match.app_name)


@register.filter
Expand Down
9 changes: 6 additions & 3 deletions parler/tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse, resolve
from django.test import RequestFactory
from django.utils import translation
from parler.templatetags.parler_tags import get_translated_url
Expand Down Expand Up @@ -57,7 +57,8 @@ def test_get_translated_url(self):
'object': self.article
}

# Simulate {% get_translated_url CODE object %} syntax
# Simulate {% get_translated_url CODE object %} syntax.
# The object.get_absolute_url() will be used to get a translated URL.
self.assertEqual(get_translated_url(context, lang_code=self.other_lang2), '/{0}/article/lang2/'.format(self.other_lang2))
self.assertEqual(get_translated_url(context, lang_code=self.conf_fallback), '/{0}/article/default/'.format(self.conf_fallback))

Expand All @@ -72,8 +73,10 @@ def test_get_translated_url_view_kwargs(self):
context = {
'request': RequestFactory().get(url),
}
context['request'].resolver_match = resolve(url) # Simulate WSGIHandler.get_response()

# Simulate {% get_translated_url CODE object %} syntax
# Simulate {% get_translated_url CODE %} syntax
# The request.resolver_match will be used to get a translated URL.
self.assertEqual(get_translated_url(context, lang_code=self.other_lang2), '/{0}/tests/kwargs-view/'.format(self.other_lang2))
self.assertEqual(get_translated_url(context, lang_code=self.conf_fallback), '/{0}/tests/kwargs-view/'.format(self.conf_fallback))

Expand Down

0 comments on commit cd36852

Please sign in to comment.