def test_site_apps(self): """Test that apps are only available inside their sites""" page = Page.objects.create( title="blog", slug="blog", static_path=False, language_code="en", is_active=True, application="blog", site=Site.objects.create(host="testserver2"), ) a = Article.objects.create(title="article", category="blog") # No urlconf. self.assertRaises(NoReverseMatch, a.get_absolute_url) # No apps on this site with set_current_site(self.test_site): self.assertEqual(apps_urlconf(), "testapp.urls") # Apps on this site with set_current_site(page.site): self.assertEqual(apps_urlconf(), "urlconf_01c07a48384868b2300536767c9879e2") try: set_urlconf("urlconf_01c07a48384868b2300536767c9879e2") self.assertEqual(a.get_absolute_url(), "/blog/%s/" % a.pk) finally: set_urlconf(None)
def test_language_and_translation_of_mixin_in_app(self): Page.objects.create( title="home-en", slug="home-en", language_code="en", is_active=True, application="translated-articles", ) Page.objects.create( title="home-de", slug="home-de", language_code="de", is_active=True, application="translated-articles", ) original = TranslatedArticle.objects.create(title="News", language_code="en") translated = TranslatedArticle.objects.create(title="Neues", language_code="de", translation_of=original) self.assertEqual( [ language["object"] for language in translations(original.translations()) ], [original, translated, None], ) with override_urlconf(apps_urlconf()): self.assertEqual(original.get_absolute_url(), "/home-en/{}/".format(original.pk)) self.assertEqual(translated.get_absolute_url(), "/home-de/{}/".format(translated.pk))
def test_reverse_app_tag(self): """Exercise the {% reverse_app %} template tag""" Page.objects.create( title="blog", slug="blog", static_path=False, language_code="en", is_active=True, application="blog", ) tests = [ ("{% reverse_app 'blog' 'article-detail' pk=42 %}", "/blog/42/", {}), ( "{% reverse_app 'blog' 'article-detail' pk=42 fallback='/a/' %}", "/blog/42/", {}, ), ( "{% reverse_app namespaces 'article-detail' pk=42 fallback='/a/' as a %}{{ a }}", # noqa "/blog/42/", { "namespaces": ["stuff", "blog"] }, ), ("{% reverse_app 'bla' 'bla' fallback='/test/' %}", "/test/", {}), ( "{% reverse_app 'bla' 'bla' fallback='/test/' as t %}{{ t }}", "/test/", {}, ), ("{% reverse_app 'bla' 'bla' as t %}{{ t|default:'blub' }}", "blub", {}), ] with override_urlconf(apps_urlconf()): for tpl, out, ctx in tests: t = Template("{% load feincms3 %}" + tpl) self.assertEqual(t.render(Context(ctx)).strip(), out) self.assertRaises( NoReverseMatch, Template( "{% load feincms3 %}{% reverse_app 'a' 'a' %}").render, Context(), )
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 test_apps_urlconf_no_apps(self): self.assertEqual(apps_urlconf(apps=[]), "testapp.urls")
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", site=self.test_site, ) home_en = Page.objects.create( title="home", slug="home", path="/en/", static_path=True, language_code="en", is_active=True, menu="main", site=self.test_site, ) 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, site=self.test_site, ) 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 set_current_site(self.test_site): set_urlconf(apps_urlconf()) try: 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) finally: set_urlconf(None) 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. with set_current_site(self.test_site): self.assertEqual(apps_urlconf(), "urlconf_fe9552a8363ece1f7fcf4970bf575a47") p = Page.objects.create( title="new", slug="new", path="/bla/", static_path=True, language_code="en", is_active=True, application="blog", site=Site.objects.create(host="testserver3"), ) with set_current_site(self.test_site): self.assertEqual(apps_urlconf(), "urlconf_fe9552a8363ece1f7fcf4970bf575a47") p.site = self.test_site p.save() with set_current_site(self.test_site): self.assertEqual(apps_urlconf(), "urlconf_0ca4c18b8aca69acfe121a9cbbdbd00e")
def test_apps_urlconf_no_apps(self): """apps_urlconf returns the ROOT_URLCONF when there are no apps at all""" self.assertEqual(apps_urlconf(apps=[]), "testapp.urls")