Skip to content

Commit

Permalink
#241 Resolve path in change_lang tag
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Sep 30, 2020
1 parent 860e123 commit da41d02
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
6 changes: 1 addition & 5 deletions chcemvediet/apps/accounts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from django.contrib.auth.models import User
from django.test import TestCase
from django.views.defaults import page_not_found

from poleno.utils.test import ViewTestCaseMixin
from poleno.utils.urls import reverse
Expand All @@ -27,10 +26,7 @@ def test_allowed_http_methods(self):
self.assert_allowed_http_methods(allowed, reverse(u'accounts:profile'))

def test_anonymous_user_is_redirected(self):
patched = functools.partial(page_not_found, template_name=u'404x.html')
with mock.patch(u'django.views.defaults.page_not_found', patched):
self.assert_anonymous_user_is_redirected(reverse(u'accounts:profile'))

self.assert_anonymous_user_is_redirected(reverse(u'accounts:profile'))

def test_authenticated_user_gets_his_profile(self):
self.client.login(username=u'john', password=u'johnpassword')
Expand Down
25 changes: 14 additions & 11 deletions poleno/utils/templatetags/poleno/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import partial

from django.template.defaultfilters import stringfilter
from django.core.urlresolvers import resolve
from django.core.urlresolvers import resolve, Resolver404
from django.conf import settings
from django.contrib.webdesign.lorem_ipsum import paragraphs
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -326,24 +326,27 @@ def capture(content, context, variable):
@register.simple_tag(takes_context=True)
def change_lang(context, lang=None):
u"""
Get active page's url with laguage changed to the specified language.
Get active page's url with language changed to the specified language.
Example:
{% change_lang 'en' %}
Source: https://djangosnippets.org/snippets/2875/
"""
path = context[u'request'].path
url_parts = resolve(path)
view_name = url_parts.view_name
kwargs = url_parts.kwargs
try:
url_parts = resolve(path)
view_name = url_parts.view_name
kwargs = url_parts.kwargs

# Ask the view what to show after changing language.
if hasattr(url_parts.func, u'change_lang'):
view_name, kwargs = url_parts.func.change_lang(lang, **kwargs)
# Ask the view what to show after changing language.
if hasattr(url_parts.func, u'change_lang'):
view_name, kwargs = url_parts.func.change_lang(lang, **kwargs)

with translation(lang):
url = reverse(view_name, kwargs=kwargs)
with translation(lang):
url = reverse(view_name, kwargs=kwargs)
except Resolver404:
url = path

query = context[u'request'].GET.urlencode()
url = url + u'?' + query if query else url
Expand Down Expand Up @@ -371,7 +374,7 @@ def url(viewname, *args, **kwargs):
@register.simple_tag
def assets(types, external=False, local=False):
u"""
Render links to local and external assets defined in settins.ASSETS.
Render links to local and external assets defined in settings.ASSETS.
Example:
{% assets "js" external=True %}
Expand Down
4 changes: 2 additions & 2 deletions poleno/utils/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ def test_active_filter(self):
def test_change_lang_tag(self):
u"""
Tests ``change_lang`` template tag by requesting a view using a template with this tag.
Checking that it generates corrent links to the same view in different languages.
Checking that it generates correct links to the same view in different languages.
"""
lang = ((u'de', u'Deutsch'), ('en', u'English'), ('fr', u'Francais'))
lang = ((u'de', u'Deutsch'), (u'en', u'English'), (u'fr', u'Francais'))
with self.settings(LANGUAGES=lang): # Fix active languages
r1 = self.client.get(u'/en/language/')
r2 = self.client.get(u'/de/language/')
Expand Down

0 comments on commit da41d02

Please sign in to comment.