def test_get_translated_url_query_string(self): """ Test that the querystring is copied to the translated URL. """ # Pretend there is a request on /af/article-lang1/ with translation.override(self.other_lang1): self.article.set_current_language(self.other_lang1) context = { "request": RequestFactory().get( f"/{self.other_lang1}/article/lang1/", {"next": "/fr/propri\xe9t\xe9/add/"} ), "object": self.article, } # Simulate {% get_translated_url CODE object %} syntax. # The object.get_absolute_url() will be used to get a translated URL. added_qs = "?next=%2Ffr%2Fpropri%C3%A9t%C3%A9%2Fadd%2F" self.assertEqual( get_translated_url(context, lang_code=self.other_lang2), f"/{self.other_lang2}/article/lang2/{added_qs}", ) self.assertEqual( get_translated_url(context, lang_code=self.conf_fallback), f"/{self.conf_fallback}/article/default/{added_qs}", ) # If the object is passed explicitly, it's likely not the current page. # Hence the querystring will not be copied in this case. self.assertEqual( get_translated_url(context, lang_code=self.other_lang2, object=self.article), f"/{self.other_lang2}/article/lang2/", )
def test_get_translated_url_view_kwargs(self): """ Test that get_translated_url can handle view kwargs. """ with translation.override(self.other_lang1): url = reverse('view-kwargs-test-view') self.assertEqual(url, '/{0}/tests/kwargs-view/'.format(self.other_lang1)) context = { 'request': RequestFactory().get(url), } # Simulate {% get_translated_url CODE object %} syntax 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))
def test_get_translated_url(self): """ Test whether get_translated_url works properly in templates. """ # Pretend there is a request on /af/article-lang1/ with translation.override(self.other_lang1): self.article.set_current_language(self.other_lang1) context = { 'request': RequestFactory().get('/{0}/article/lang1/'.format(self.other_lang1)), 'object': self.article } # Simulate {% get_translated_url CODE object %} syntax 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))
def test_get_translated_url(self): """ Test whether get_translated_url works properly in templates. """ # Pretend there is a request on /af/article-lang1/ with translation.override(self.other_lang1): self.article.set_current_language(self.other_lang1) context = { 'request': RequestFactory().get('/{0}/article/lang1/'.format(self.other_lang1)), 'object': self.article } # 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))
def get_action_icons(self, node): from parler.templatetags.parler_tags import get_translated_url if issubclass(node.get_real_instance_class(), PublishingModel): node = node.get_real_instance() actions = [] if node.can_have_children: actions.append( """<a href="add/?{parent_attr}={id}" title="{title}" class="add-child-object"><img src="{static}polymorphic_tree/icons/page_new.gif" width="16" height="16" alt="{title}" /></a>""".format(parent_attr=self.model._mptt_meta.parent_attr, id=node.pk, title=_('Add sub node'), static=settings.STATIC_URL)) else: actions.append(self.EMPTY_ACTION_ICON.format(STATIC_URL=settings.STATIC_URL, css_class='add-child-object')) if hasattr(node, 'publishing_linked'): if node.publishing_linked: temp_url = get_translated_url( dict(request=self.request), self.get_form_language(self.request, node), node.publishing_linked, ) actions.append( """<a href="{url}" title="{title}" target="_blank"><img src="{static}polymorphic_tree/icons/world.gif" width="16" height="16" alt="{title}" /></a>""".format(url=temp_url, title=_('View on site'), static=settings.STATIC_URL)) # The is_first_sibling and is_last_sibling is quite heavy. Instead rely on CSS to hide the arrows. move_up = u'<a href="{0}/move_up/" class="move-up">\u2191</a>'.format(node.pk) move_down = u'<a href="{0}/move_down/" class="move-down">\u2193</a>'.format(node.pk) actions.append(u'<span class="no-js">{0}{1}</span>'.format(move_up, move_down)) return actions
def test_get_translated_url_view_kwargs(self): """ Test that get_translated_url can handle view kwargs. """ with translation.override(self.other_lang1): url = reverse('view-kwargs-test-view') self.assertEqual(url, '/{0}/tests/kwargs-view/'.format(self.other_lang1)) context = { 'request': RequestFactory().get(url), } context['request'].resolver_match = resolve(url) # Simulate WSGIHandler.get_response() # 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))
def test_get_translated_url_view_kwargs(self): """ Test that get_translated_url can handle view kwargs. """ with translation.override(self.other_lang1): url = reverse('view-kwargs-test-view') self.assertEqual( url, '/{0}/tests/kwargs-view/'.format(self.other_lang1)) context = { 'request': RequestFactory().get(url), } # Simulate {% get_translated_url CODE object %} syntax 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))
def test_get_translated_url_query_string(self): """ Test that the querystring is copied to the translated URL. """ # Pretend there is a request on /af/article-lang1/ with translation.override(self.other_lang1): self.article.set_current_language(self.other_lang1) context = { 'request': RequestFactory().get('/{0}/article/lang1/'.format(self.other_lang1), { 'next': '/fr/propri\xe9t\xe9/add/' }), 'object': self.article } # Simulate {% get_translated_url CODE object %} syntax. # The object.get_absolute_url() will be used to get a translated URL. added_qs = "?next=%2Ffr%2Fpropri%C3%A9t%C3%A9%2Fadd%2F" self.assertEqual(get_translated_url(context, lang_code=self.other_lang2), '/{0}/article/lang2/{1}'.format(self.other_lang2, added_qs)) self.assertEqual(get_translated_url(context, lang_code=self.conf_fallback), '/{0}/article/default/{1}'.format(self.conf_fallback, added_qs)) # If the object is passed explicitly, it's likely not the current page. # Hence the querystring will not be copied in this case. self.assertEqual(get_translated_url(context, lang_code=self.other_lang2, object=self.article), '/{0}/article/lang2/'.format(self.other_lang2))