def get_absolute_url(self): """ This is the trickiest bit of this whole project. reverse_app is a simple wrapper around feincms3.applications.reverse_any, which is exactly the same as django.core.urlresolvers.reverse with the small difference that it accepts a list of viewnames and returns the first result where no NoReverseMatch exception is raised. The viewnames tried in sequence by reverse_app are (assuming that the project is configured with german, english and french as available languages, and french as active language, and assuming that the current article is a publication): - fr.publications.article-detail - fr.articles.article-detail - de.publications.article-detail - de.articles.article-detail - en.publications.article-detail - en.articles.article-detail - Otherwise, let the NoReverseMatch exception bubble. reverse_app tries harder returning an URL in the correct language than returning an URL for the correct app. Better show a PR publication on the blog page than switching languages. """ return reverse_app( (self.category, "articles"), "article-detail", kwargs={ "year": self.publication_date.year, "slug": self.slug, }, )
def test_apps(self): """Article app test (two instance namespaces, two languages)""" home_de = Page.objects.create( title="home", slug="home", path="/de/", static_path=True, language_code="de", is_active=True, menu="main", ) home_en = Page.objects.create( title="home", slug="home", path="/en/", static_path=True, language_code="en", is_active=True, menu="main", ) for root in (home_de, home_en): for app in ("blog", "publications"): Page.objects.create( title=app, slug=app, static_path=False, language_code=root.language_code, is_active=True, application=app, parent_id=root.pk, ) for i in range(7): for category in ("publications", "blog"): Article.objects.create(title="%s %s" % (category, i), category=category) self.assertContains(self.client.get("/de/blog/all/"), 'class="article"', 7) self.assertContains(self.client.get("/de/blog/?page=2"), 'class="article"', 2) self.assertContains( self.client.get("/de/blog/?page=42"), 'class="article"', 2, # Last page with instances (2nd) ) self.assertContains( self.client.get("/de/blog/?page=invalid"), 'class="article"', 5, # First page ) response = self.client.get("/de/blog/") self.assertContains(response, 'class="article"', 5) response = self.client.get("/en/publications/") self.assertContains(response, 'class="article"', 5) with override_urlconf(apps_urlconf()): article = Article.objects.order_by("pk").first() with override("de"): self.assertEqual(article.get_absolute_url(), "/de/publications/%s/" % article.pk) with override("en"): self.assertEqual(article.get_absolute_url(), "/en/publications/%s/" % article.pk) # The german URL is returned when specifying the ``languages`` # list explicitly. self.assertEqual( reverse_app( (article.category, "articles"), "article-detail", kwargs={"pk": article.pk}, languages=["de", "en"], ), "/de/publications/%s/" % article.pk, ) response = self.client.get("/de/publications/%s/" % article.pk) self.assertContains(response, "<h1>publications 0</h1>", 1) # The exact value of course does not matter, just the fact that the # value does not change all the time. self.assertEqual(apps_urlconf(), "urlconf_fe9552a8363ece1f7fcf4970bf575a47")
def get_absolute_url(self): with override(self.language_code): return reverse_app("translated-articles", "detail", kwargs={"pk": self.pk})
def get_absolute_url(self): return reverse_app((self.category, "articles"), "article-detail", kwargs={"pk": self.pk})