def new_blog_entry(request): """ TODO: Use Ajax in preview """ # user_profile = request.user.get_profile() # All accessible sites from the current user: # user_site_ids = user_profile.sites.values_list("id", "name") # m2m_limit = {"sites": user_site_ids} # Limit the site choice field with LimitManyToManyFields try: plugin_page = PluginPage.objects.filter(app_label__endswith="blog")[0] except IndexError: messages.error( request, _("There exist no blog plugin page, yet. Please create one, first." )) return http.HttpResponseRedirect("/") context = { "title": _("Create a new blog entry"), "form_url": request.path, "tag_cloud": BlogEntryContent.objects.get_tag_cloud(request), "add_tag_filter_link": False, # Don't add filters in tag cloud } if request.method == "POST": if "cancel" in request.POST: messages.info(request, _("Create new blog entry aborted, ok.")) url = plugin_page.get_absolute_url() return http.HttpResponseRedirect(url) form = BlogForm(request.POST) if form.is_valid(): new_entry = BlogEntry.objects.create() new_entry.sites = form.cleaned_data["sites"] new_entry.save() blog_content = form.save(commit=False) blog_content.entry = new_entry blog_content.save() messages.success( request, _("New blog entry '%s' saved.") % blog_content.headline) return http.HttpResponseRedirect(blog_content.get_absolute_url()) else: # Get preferences pref_form = BlogPrefForm() initial = { "sites": get_site_preselection(pref_form, request), # preselect sites field "language": request.PYLUCID.current_language.pk, # preselect current language } form = BlogForm(initial=initial) context["form"] = form return context
def test_all_languages(self): self.pref_form = BlogPrefForm() self.pref_form["language_filter"] = BlogPrefForm.ALL_LANGUAGES self.pref_form.save() response = self.client.get("/%s/blog/" % self.default_language.code, HTTP_ACCEPT_LANGUAGE=self.default_language.code, ) self.assertResponse(response, must_contain=( 'Zweiter Eintrag in deutsch', 'Zweiter Eintrag in deutsch', 'Second entry in english', 'First entry in english', ), must_not_contain=("Traceback",) )
def _pre_setup(self, *args, **kwargs): """ create some blog articles """ super(BlogPluginArticleTest, self)._pre_setup(*args, **kwargs) self.pref_form = BlogPrefForm() self.pref_form["language_filter"] = BlogPrefForm.CURRENT_LANGUAGE self.pref_form.save() defaults = { "markup": MARKUP_CREOLE, "is_public": True, } self.entry_en1 = self.easy_create(BlogEntry, defaults, headline="First entry in english", content="1. **blog article** in //english//!", language=self.default_language, tags="sharedtag, first_tag, english-tag", ) self.entry_en2 = self.easy_create(BlogEntry, defaults, headline="Second entry in english", content="2. **blog article** in //english//!", language=self.default_language, tags="sharedtag, second_tag, english-tag", ) self.entry_de1 = self.easy_create(BlogEntry, defaults, headline="Erster Eintrag in deutsch", content="1. **Blog Artikel** in //deutsch//!", language=self.other_language, tags="sharedtag, erster_tag, deutsch-tag", ) self.entry_de2 = self.easy_create(BlogEntry, defaults, headline="Zweiter Eintrag in deutsch", content="2. **Blog Artikel** in //deutsch//!", language=self.other_language, tags="sharedtag, zweiter_tag, deutsch-tag", )
class BlogPluginArticleTest(BlogPluginTestCase): """ Test blog plugin with existing blog articles in different languages """ def _pre_setup(self, *args, **kwargs): """ create some blog articles """ super(BlogPluginArticleTest, self)._pre_setup(*args, **kwargs) self.pref_form = BlogPrefForm() self.pref_form["language_filter"] = BlogPrefForm.CURRENT_LANGUAGE self.pref_form.save() defaults = { "markup": MARKUP_CREOLE, "is_public": True, } self.entry_en1 = self.easy_create(BlogEntry, defaults, headline="First entry in english", content="1. **blog article** in //english//!", language=self.default_language, tags="sharedtag, first_tag, english-tag", ) self.entry_en2 = self.easy_create(BlogEntry, defaults, headline="Second entry in english", content="2. **blog article** in //english//!", language=self.default_language, tags="sharedtag, second_tag, english-tag", ) self.entry_de1 = self.easy_create(BlogEntry, defaults, headline="Erster Eintrag in deutsch", content="1. **Blog Artikel** in //deutsch//!", language=self.other_language, tags="sharedtag, erster_tag, deutsch-tag", ) self.entry_de2 = self.easy_create(BlogEntry, defaults, headline="Zweiter Eintrag in deutsch", content="2. **Blog Artikel** in //deutsch//!", language=self.other_language, tags="sharedtag, zweiter_tag, deutsch-tag", ) def assertSecondArticle(self, response): self.assertContentLanguage(response, self.default_language) self.assertResponse(response, must_contain=( '<title>PyLucid CMS - Second entry in english</title>', '<a href="/en/blog/2/second-entry-in-english/" class="blog_headline" hreflang="en">', 'Second entry in english</a>', '<a href="/en/blog/2/second-entry-in-english/" title="Article 'Second entry in english'">', 'Second entry in english</a>', # english tag cloud: 'tag cloud', '<a href="/en/blog/tags/english-tag/" style="font-size:2em;">english-tag</a>', 'class="content" lang="en"><p>2. <strong>blog article</strong> in <i>english</i>!</p>', "entry in english", "first_tag", "english-tag", 'Leave a comment</a>', # from pylucid comments ), must_not_contain=("Traceback", # Not the summary page: "All articles", "Alle Artikel", # not the german tag cloud: 'Tag Cloud', '<a href="/en/blog/tags/deutsch-tag/" style="font-size:2em;">', 'deutsch-tag</a>', ) ) def test_absolute_url(self): self.failUnlessEqual(self.entry_en1.get_absolute_url(), "/en/blog/1/first-entry-in-english/") self.failUnlessEqual(self.entry_de1.get_absolute_url(), "/de/blog/3/erster-eintrag-in-deutsch/") def test_summary_en(self): """ test the summary page in english """ response = self.client.get( SUMMARY_URL % self.default_language.code, HTTP_ACCEPT_LANGUAGE=self.default_language.code, ) self.assertSummaryEN(response) self.assertContentLanguage(response, self.default_language) self.assertResponse(response, must_contain=( 'All articles in English.', '<a href="/en/blog/1/first-entry-in-english/"', 'First entry in english', '<a href="/en/blog/2/second-entry-in-english/"', 'Second entry in english', '<p>1. <strong>blog article</strong> in <i>english</i>!</p>', '<p>2. <strong>blog article</strong> in <i>english</i>!</p>', # from django-tagging: 'tag cloud', '<a href="/en/blog/tags/english-tag/" style="font-size:2em;">english-tag</a>', '<a href="/en/blog/tags/first_tag/" style="font-size:1em;">first_tag</a>', '<a href="/en/blog/tags/second_tag/" style="font-size:1em;">second_tag</a>', '<a href="/en/blog/tags/sharedtag/" style="font-size:2em;">sharedtag</a>', ), must_not_contain=("Traceback", "Eintrag in deutsch", "erster_tag", "deutsch-tag" 'Leave a comment</a>', # from pylucid comments ) ) def test_summary_de(self): """ test the summary page in deutsch """ response = self.client.get( SUMMARY_URL % self.other_language.code, HTTP_ACCEPT_LANGUAGE=self.other_language.code, ) self.assertSummaryDE(response) self.assertContentLanguage(response, self.other_language) self.assertResponse(response, must_contain=( 'Alle Artikel in Deutsch.', '<a href="/de/blog/3/erster-eintrag-in-deutsch/"', 'Erster Eintrag in deutsch', '<a href="/de/blog/4/zweiter-eintrag-in-deutsch/"', 'Zweiter Eintrag in deutsch', '<p>1. <strong>Blog Artikel</strong> in <i>deutsch</i>!</p>', '<p>2. <strong>Blog Artikel</strong> in <i>deutsch</i>!</p>', # from django-tagging: 'Tag Cloud', '<a href="/de/blog/tags/deutsch-tag/" style="font-size:2em;">deutsch-tag</a>', '<a href="/de/blog/tags/erster_tag/" style="font-size:1em;">erster_tag</a>', '<a href="/de/blog/tags/sharedtag/" style="font-size:2em;">sharedtag</a>', '<a href="/de/blog/tags/zweiter_tag/" style="font-size:1em;">zweiter_tag</a>', ), must_not_contain=("Traceback", "entry in english", "first_tag", "english-tag" 'Leave a comment</a>', # from pylucid comments ) ) def test_update_journal_de(self): # Check if listed in update journal response = self.client.get("/", HTTP_ACCEPT_LANGUAGE="de") self.failUnlessEqual(response.status_code, 200) self.assertResponse(response, must_contain=( '(blog entry)', '<a href="/de/blog/4/zweiter-eintrag-in-deutsch/">', 'Zweiter Eintrag in deutsch</a>', '<a href="/de/blog/3/erster-eintrag-in-deutsch/">', 'Erster Eintrag in deutsch</a>', ), must_not_contain=("Traceback", "First entry", "Second entry") ) def test_update_journal_en(self): # Check if listed in update journal response = self.client.get("/", HTTP_ACCEPT_LANGUAGE="en") self.failUnlessEqual(response.status_code, 200) self.assertResponse(response, must_contain=( '(blog entry)', '<a href="/en/blog/2/second-entry-in-english/">', 'Second entry in english</a>', '<a href="/en/blog/1/first-entry-in-english/">', 'First entry in english</a>', ), must_not_contain=("Traceback", "Erster Eintrag", "Zweiter Eintrag") ) def test_second_entry(self): """ request the second, english entry. """ response = self.client.get( "/en/blog/2/second-entry-in-english/", HTTP_ACCEPT_LANGUAGE=self.default_language.code, ) self.assertSecondArticle(response) def test_second_entry_as_german(self): """ request the second, english entry, with http accept in german. The blog plugin switches to english. """ # The first request activae german language from http accept # But the blog article is written in english. PyLucid changed # the language in url and cookie and redirect response = self.client.get( "/de/blog/2/second-entry-in-english/", HTTP_ACCEPT_LANGUAGE=self.other_language.code, ) self.assertRedirect( response, url="http://testserver/en/blog/2/second-entry-in-english/", status_code=301 ) # 'Follow' the redirection and get the page and article in english response = self.client.get( "/en/blog/2/second-entry-in-english/", HTTP_ACCEPT_LANGUAGE=self.other_language.code, ) self.assertSecondArticle(response) def _test_atom_feed(self, language): language_code = language.code response = self.client.get( "/%s/blog/feed/feed.atom" % language_code, HTTP_ACCEPT_LANGUAGE=language_code, ) self.assertAtomFeed(response, language_code) def test_atom_feed_default_language(self): self._test_atom_feed(self.default_language) def test_atom_feed_other_language(self): self._test_atom_feed(self.other_language) def _test_rss_feed(self, language): language_code = language.code response = self.client.get( "/%s/blog/feed/feed.rss" % language_code, HTTP_ACCEPT_LANGUAGE=language_code, ) self.assertRssFeed(response, language_code) def test_rss_feed_default_language(self): self._test_rss_feed(self.default_language) def test_rss_feed_other_language(self): self._test_rss_feed(self.other_language) def test_all_languages(self): self.pref_form = BlogPrefForm() self.pref_form["language_filter"] = BlogPrefForm.ALL_LANGUAGES self.pref_form.save() response = self.client.get("/%s/blog/" % self.default_language.code, HTTP_ACCEPT_LANGUAGE=self.default_language.code, ) self.assertResponse(response, must_contain=( 'Zweiter Eintrag in deutsch', 'Zweiter Eintrag in deutsch', 'Second entry in english', 'First entry in english', ), must_not_contain=("Traceback",) )