示例#1
0
    def test_index_simple(self):
        self.request = self.get_request('en')
        self.index = ArticleIndex()
        content0 = self.rand_str(prefix='content0_')
        self.setup_categories()

        article = self.create_article(content=content0,
                                      lead_in='lead in text',
                                      title='a title')
        article.categories.add()
        for tag_name in ('tag 1', 'tag2'):
            article.tags.add(tag_name)
        for category in (self.category1, self.category2):
            article.categories.add(category)
        article.update_search_on_save = True
        article.save()

        self.assertEqual(self.index.get_title(article), 'a title')
        self.assertEqual(self.index.get_description(article), 'lead in text')
        self.assertTrue('lead in text' in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue(content0 in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue(
            'tag 1' in self.index.get_search_data(article, 'en', self.request))
        self.assertTrue(self.category1.name in self.index.get_search_data(
            article, 'en', self.request))
    def test_index_multilingual(self):
        self.index = ArticleIndex()
        content0 = self.rand_str(prefix='content0_')
        self.setup_categories()

        article_1 = self.create_article(content=content0,
                                        lead_in=u'lead in text',
                                        title=u'a title')
        article_2 = self.create_article(content=content0,
                                        lead_in=u'lead in text',
                                        title=u'second title')
        for article in (article_1, article_2):
            for category in (self.category1, self.category2):
                article.categories.add(category)
        with switch_language(article_2, 'de'):
            article_2.title = u'de title'
            article_2.lead_in = u'de lead in'
            article_2.save()

        LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE)
        with override_parler_settings(PARLER_LANGUAGES=LANGUAGES):
            with smart_override('de'):
                language = get_current_language()
                # english-only article is excluded
                qs = self.index.index_queryset(language)
                self.assertEqual(qs.count(), 1)
                self.assertEqual(
                    qs.translated(language, title__icontains='title').count(),
                    1)
                # the language is correctly setup
                for article_de in qs:
                    self.assertEqual(self.index.get_title(article_de),
                                     'de title')
                    self.assertEqual(self.index.get_description(article_de),
                                     'de lead in')
示例#3
0
    def test_index_simple(self):
        self.request = self.get_request('en')
        self.index = ArticleIndex()
        content0 = self.rand_str(prefix='content0_')
        self.setup_categories()

        article = self.create_article(content=content0, lead_in='lead in text',
                                      title='a title')
        article.categories.add()
        for tag_name in ('tag 1', 'tag2'):
            article.tags.add(tag_name)
        for category in (self.category1, self.category2):
            article.categories.add(category)
        article.save()

        self.assertEqual(self.index.get_title(article), 'a title')
        self.assertEqual(self.index.get_description(article), 'lead in text')
        self.assertTrue('lead in text' in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue(content0 in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue('tag 1' in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue(self.category1.name in self.index.get_search_data(
            article, 'en', self.request))
    def test_index_multilingual(self):
        self.index = ArticleIndex()
        content0 = self.rand_str(prefix='content0_')
        self.setup_categories()

        article_1 = self.create_article(
            content=content0, lead_in=u'lead in text', title=u'a title')
        article_2 = self.create_article(
            content=content0, lead_in=u'lead in text', title=u'second title')
        for article in (article_1, article_2):
            for tag_name in ('tag 1', 'tag2'):
                article.tags.add(tag_name)
            for category in (self.category1, self.category2):
                article.categories.add(category)
        with switch_language(article_2, 'de'):
            article_2.title = u'de title'
            article_2.lead_in = u'de lead in'
            article_2.save()

        LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE)
        with override_parler_settings(PARLER_LANGUAGES=LANGUAGES):
            with smart_override('de'):
                language = get_current_language()
                # english-only article is excluded
                qs = self.index.index_queryset(language)
                self.assertEqual(qs.count(), 1)
                self.assertEqual(
                    qs.translated(language, title__icontains='title').count(),
                    1
                )
                # the language is correctly setup
                for article_de in qs:
                    self.assertEqual(
                        self.index.get_title(article_de), 'de title')
                    self.assertEqual(
                        self.index.get_description(article_de), 'de lead in')
示例#5
0
    def get_index(self):
        from haystack.constants import DEFAULT_ALIAS

        index = ArticleIndex()
        index._backend_alias = DEFAULT_ALIAS
        return index
示例#6
0
class TestIndex(NewsBlogTestCase):
    def test_index_simple(self):
        self.request = self.get_request('en')
        self.index = ArticleIndex()
        content0 = self.rand_str(prefix='content0_')
        self.setup_categories()

        article = self.create_article(content=content0,
                                      lead_in='lead in text',
                                      title='a title')
        article.categories.add()
        for tag_name in ('tag 1', 'tag2'):
            article.tags.add(tag_name)
        for category in (self.category1, self.category2):
            article.categories.add(category)
        article.update_search_on_save = True
        article.save()

        self.assertEqual(self.index.get_title(article), 'a title')
        self.assertEqual(self.index.get_description(article), 'lead in text')
        self.assertTrue('lead in text' in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue(content0 in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue(
            'tag 1' in self.index.get_search_data(article, 'en', self.request))
        self.assertTrue(self.category1.name in self.index.get_search_data(
            article, 'en', self.request))

    def test_index_multilingual(self):
        self.index = ArticleIndex()
        content0 = self.rand_str(prefix='content0_')
        self.setup_categories()

        article_1 = self.create_article(content=content0,
                                        lead_in=u'lead in text',
                                        title=u'a title')
        article_2 = self.create_article(content=content0,
                                        lead_in=u'lead in text',
                                        title=u'second title')
        for article in (article_1, article_2):
            for tag_name in ('tag 1', 'tag2'):
                article.tags.add(tag_name)
            for category in (self.category1, self.category2):
                article.categories.add(category)
        with switch_language(article_2, 'de'):
            article_2.title = u'de title'
            article_2.lead_in = u'de lead in'
            article_2.save()

        LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE)
        with override_parler_settings(PARLER_LANGUAGES=LANGUAGES):
            with smart_override('de'):
                language = get_current_language()
                # english-only article is excluded
                qs = self.index.index_queryset(language)
                self.assertEqual(qs.count(), 1)
                self.assertEqual(
                    qs.translated(language, title__icontains='title').count(),
                    1)
                # the language is correctly setup
                for article_de in qs:
                    self.assertEqual(self.index.get_title(article_de),
                                     'de title')
                    self.assertEqual(self.index.get_description(article_de),
                                     'de lead in')
示例#7
0
    def get_index(self):
        from haystack.constants import DEFAULT_ALIAS

        index = ArticleIndex()
        index._backend_alias = DEFAULT_ALIAS
        return index
示例#8
0
class TestAldrynNewsBlog(NewsBlogTestsMixin, TransactionTestCase):
    def test_create_article(self):
        article = self.create_article()
        response = self.client.get(article.get_absolute_url())
        self.assertContains(response, article.title)

    def test_delete_article(self):
        article = self.create_article()
        article_pk = article.pk
        article_url = article.get_absolute_url()
        response = self.client.get(article_url)
        self.assertContains(response, article.title)
        Article.objects.get(pk=article_pk).delete()
        response = self.client.get(article_url)
        self.assertEqual(response.status_code, 404)

    def test_article_not_published(self):
        article = self.create_article(is_published=False)
        response = self.client.get(article.get_absolute_url())
        self.assertEqual(response.status_code, 404)

    def test_articles_list(self):
        articles = [self.create_article() for _ in range(10)]
        unpublished_article = articles[0]
        unpublished_article.is_published = False
        unpublished_article.save()
        response = self.client.get(reverse('aldryn_newsblog:article-list'))
        for article in articles[1:]:
            self.assertContains(response, article.title)
        self.assertNotContains(response, unpublished_article.title)

    def test_articles_list_pagination(self):
        paginate_by = settings.ALDRYN_NEWSBLOG_PAGINATE_BY
        articles = [
            self.create_article(publishing_date=datetime(2000 - i, 1, 1, 1, 1))
            for i in range(paginate_by + 5)
        ]

        response = self.client.get(reverse('aldryn_newsblog:article-list'))
        for article in articles[:paginate_by]:
            self.assertContains(response, article.title)
        for article in articles[paginate_by:]:
            self.assertNotContains(response, article.title)

        response = self.client.get(
            reverse('aldryn_newsblog:article-list') + '?page=2')
        for article in articles[:paginate_by]:
            self.assertNotContains(response, article.title)
        for article in articles[paginate_by:]:
            self.assertContains(response, article.title)

    def test_articles_by_author(self):
        author1, author2 = self.create_person(), self.create_person()
        for author in (author1, author2):
            articles = [self.create_article(author=author) for _ in range(10)]
            response = self.client.get(
                reverse('aldryn_newsblog:article-list-by-author',
                        kwargs={'author': author.slug}))
            for article in articles:
                self.assertContains(response, article.title)

    def test_articles_by_category(self):
        """Tests that we can find articles by their categories, in ANY of the
        languages they are translated to"""
        author = self.create_person()
        for category in (self.category1, self.category2):
            articles = []
            code = "{0}-".format(self.language)
            for _ in range(10):
                article = Article.objects.create(
                    title=rand_str(),
                    slug=rand_str(prefix=code),
                    app_config=self.app_config,
                    author=author,
                    owner=author.user,
                    publishing_date=datetime.now())
                # Make sure there are translations in place for the articles.
                for language, _ in settings.LANGUAGES[1:]:
                    with switch_language(article, language):
                        code = "{0}-".format(language)
                        article.title = rand_str(prefix=code)
                        article.save()

                article.categories.add(category)
                articles.append(article)

            for language, _ in settings.LANGUAGES:
                with switch_language(category, language):
                    url = reverse('aldryn_newsblog:article-list-by-category',
                                  kwargs={'category': category.slug})
                response = self.client.get(url)
                for article in articles:
                    if language in article.get_available_languages():
                        article.set_current_language(language)
                        self.assertContains(response, article.title)
                    else:
                        article.set_current_language(language)
                        self.assertNotContains(response, article.title)

    def test_article_detail_not_translated_fallback(self):
        """
        If the fallback is configured, article is available in any (configured) language
        """
        author = self.create_person()
        code = "{0}-".format(self.language)
        article = Article.objects.create(title=rand_str(),
                                         slug=rand_str(prefix=code),
                                         app_config=self.app_config,
                                         author=author,
                                         owner=author.user,
                                         publishing_date=datetime.now())
        article.save()
        article.categories.add(self.category1)

        # current language - it still exists
        article = Article.objects.get(pk=article.pk)
        language = settings.LANGUAGES[0][0]
        with switch_language(self.category1, language):
            url = reverse('aldryn_newsblog:article-detail',
                          kwargs={'slug': article.slug})
            response = self.client.get(url)
            self.assertContains(response, article.title)

        # non existing language - it still exists
        language = settings.LANGUAGES[1][0]
        with switch_language(self.category1, language):
            url = reverse('aldryn_newsblog:article-detail',
                          kwargs={'slug': article.slug})
            response = self.client.get(url)
            self.assertContains(response, article.title)

    def test_article_detail_not_translated_no_fallback(self):
        """
        If the fallback is disabled, article is available only in the
        language in which is translated
        """
        author = self.create_person()
        code = "{0}-".format(self.language)
        article = Article.objects.create(title=rand_str(),
                                         slug=rand_str(prefix=code),
                                         app_config=self.app_config,
                                         author=author,
                                         owner=author.user,
                                         publishing_date=datetime.now())
        article.save()
        article.categories.add(self.category1)

        PARLER_LANGUAGES = {
            1: (
                {
                    'code': 'de'
                },
                {
                    'code': 'fr'
                },
                {
                    'code': 'en'
                },
            ),
            'default': {
                'hide_untranslated': True,
            }
        }
        LANGUAGES = add_default_language_settings(PARLER_LANGUAGES)
        with override_parler_settings(PARLER_LANGUAGES=LANGUAGES):

            # current language - it still exists
            article = Article.objects.get(pk=article.pk)
            language = settings.LANGUAGES[0][0]
            with switch_language(self.category1, language):
                url = reverse('aldryn_newsblog:article-detail',
                              kwargs={'slug': article.slug})
                response = self.client.get(url)
                self.assertContains(response, article.title)

            # non existing language - it still exists
            language = settings.LANGUAGES[1][0]
            with switch_language(self.category1, language):
                url = reverse('aldryn_newsblog:article-detail',
                              kwargs={'slug': article.slug})
                response = self.client.get(url)
                self.assertEqual(response.status_code, 404)

    def test_article_detail_show_featured_image(self):
        author = self.create_person()
        with open(FEATURED_IMAGE_PATH, 'rb') as f:
            file_obj = DjangoFile(f, name='featured_image.jpg')
            image = Image.objects.create(
                owner=author.user,
                original_filename='featured_image.jpg',
                file=file_obj,
                subject_location='fooobar')
        article = self.create_article(author=author, featured_image=image)
        response = self.client.get(article.get_absolute_url())
        image_url = get_thumbnailer(article.featured_image).get_thumbnail({
            'size': (800, 300),
            'crop':
            True,
            'subject_location':
            article.featured_image.subject_location
        }).url
        self.assertContains(response, image_url)

    def test_articles_by_tag(self):
        """
        Tests that TagArticleList view properly filters articles by their tags.

        This uses ANY of the languages articles are translated to.
        """

        untagged_articles = []
        for _ in range(5):
            article = self.create_article()
            untagged_articles.append(article)

        articles = self.create_tagged_articles(3,
                                               tags=(rand_str(), rand_str()))

        # tags are created in previous loop on demand, we need their slugs
        tag_slug1, tag_slug2 = articles.keys()
        url = reverse('aldryn_newsblog:article-list-by-tag',
                      kwargs={'tag': tag_slug2})
        response = self.client.get(url)
        for article in articles[tag_slug2]:
            self.assertContains(response, article.title)
        for article in articles[tag_slug1]:
            self.assertNotContains(response, article.title)
        for article in untagged_articles:
            self.assertNotContains(response, article.title)

    def test_articles_count_by_month(self):
        months = [
            {
                'date': date(1914, 7, 3),
                'num_entries': 1
            },
            {
                'date': date(1914, 8, 3),
                'num_entries': 3
            },
            {
                'date': date(1945, 9, 3),
                'num_entries': 5
            },
        ]
        for month in months:
            for _ in range(month['num_entries']):
                self.create_article(publishing_date=month['date'])
        self.assertEquals(
            sorted(Article.objects.get_months(
                namespace=self.app_config.namespace),
                   key=itemgetter('num_entries')), months)

    def test_articles_count_by_author(self):
        authors = []
        for num_entries in [1, 3, 5]:
            person = self.create_person()
            person.num_entries = num_entries
            authors.append((person, num_entries))

        for i, data in enumerate(authors):
            for _ in range(data[1]):
                self.create_article(author=data[0])
            # replace author with it's pk, as we need it to easily compare
            authors[i] = (data[0].pk, data[1])

        self.assertEquals(
            sorted(Article.objects.get_authors(
                namespace=self.app_config.namespace).values_list(
                    'pk', 'num_entries'),
                   key=itemgetter(1)), authors)

    def test_articles_count_by_tags(self):
        untagged_articles = []
        for _ in range(5):
            article = self.create_article()
            untagged_articles.append(article)
        # Tag objects are created on attaching tag name to Article,
        # so this looks not very DRY
        tag_names = ('tag foo', 'tag bar', 'tag buzz')
        tag_slug1 = self.create_tagged_articles(
            1, tags=(tag_names[0], )).keys()[0]
        tag_slug2 = self.create_tagged_articles(
            3, tags=(tag_names[1], )).keys()[0]
        tag_slug3 = self.create_tagged_articles(
            5, tags=(tag_names[2], )).keys()[0]
        tags_expected = [
            (tag_slug3, 5),
            (tag_slug2, 3),
            (tag_slug1, 1),
        ]
        tags = Article.objects.get_tags(namespace=self.app_config.namespace)
        tags = map(lambda x: (x.slug, x.num_entries), tags)
        self.assertEquals(tags, tags_expected)

    def test_articles_by_date(self):
        in_articles = [
            self.create_article(publishing_date=datetime(
                1914, 7, 28, randint(0, 23), randint(0, 59)))
            for _ in range(10)
        ]
        out_articles = [
            self.create_article(publishing_date=datetime(
                1939, 9, 1, randint(0, 23), randint(0, 59))) for _ in range(10)
        ]
        response = self.client.get(
            reverse('aldryn_newsblog:article-list-by-day',
                    kwargs={
                        'year': '1914',
                        'month': '07',
                        'day': '28'
                    }))
        for article in out_articles:
            self.assertNotContains(response, article.title)
        for article in in_articles:
            self.assertContains(response, article.title)

    def test_articles_by_month(self):
        in_articles = [
            self.create_article(publishing_date=datetime(
                1914, 7, randint(1, 31), randint(0, 23), randint(0, 59)))
            for _ in range(10)
        ]
        out_articles = [
            self.create_article(publishing_date=datetime(
                1939, 9, 1, randint(0, 23), randint(0, 59))) for _ in range(10)
        ]
        response = self.client.get(
            reverse('aldryn_newsblog:article-list-by-month',
                    kwargs={
                        'year': '1914',
                        'month': '07'
                    }))
        for article in out_articles:
            self.assertNotContains(response, article.title)
        for article in in_articles:
            self.assertContains(response, article.title)

    def test_articles_by_year(self):
        in_articles = [
            self.create_article(
                publishing_date=datetime(1914, randint(1, 12), randint(1, 28),
                                         randint(0, 23), randint(0, 59)))
            for _ in range(10)
        ]
        out_articles = [
            self.create_article(
                publishing_date=datetime(1939, randint(1, 12), randint(1, 28),
                                         randint(0, 23), randint(0, 59)))
            for _ in range(10)
        ]
        response = self.client.get(
            reverse('aldryn_newsblog:article-list-by-year',
                    kwargs={'year': '1914'}))
        for article in out_articles:
            self.assertNotContains(response, article.title)
        for article in in_articles:
            self.assertContains(response, article.title)

    def test_has_content(self):
        # Just make sure we have a known language
        activate(self.language)
        title = rand_str()
        content = rand_str()
        author = self.create_person()
        article = Article.objects.create(title=title,
                                         slug=rand_str(),
                                         author=author,
                                         owner=author.user,
                                         app_config=self.app_config,
                                         publishing_date=datetime.now())
        article.save()
        api.add_plugin(article.content, 'TextPlugin', self.language)
        plugin = article.content.get_plugins()[0].get_plugin_instance()[0]
        plugin.body = content
        plugin.save()
        response = self.client.get(article.get_absolute_url())
        self.assertContains(response, title)
        self.assertContains(response, content)

    def test_unattached_namespace(self):
        # create a new namespace that has no corresponding blog app page
        app_config = NewsBlogConfig.objects.create(namespace='another')
        articles = [
            self.create_article(app_config=app_config) for _ in range(10)
        ]
        response = self.client.get(articles[0].get_absolute_url())
        self.assertEqual(response.status_code, 404)
        response = self.client.get(reverse('aldryn_newsblog:article-list'))
        for article in articles:
            self.assertNotContains(response, article.title)

    def test_auto_slugifies(self):
        activate(self.language)
        title = u'This is a title'
        author = self.create_person()
        article = Article.objects.create(title=title,
                                         author=author,
                                         owner=author.user,
                                         app_config=self.app_config,
                                         publishing_date=datetime.now())
        article.save()
        self.assertEquals(article.slug, 'this-is-a-title')
        # Now, let's try another with the same title
        article.id = None
        # Note, it cannot be the exact same title, else we'll fail the unique
        # constraint on the field.
        article.title = title.lower()
        article.save()
        # Note that this should be "incremented" slug here.
        self.assertEquals(article.slug, 'this-is-a-title_1')
        article.id = None
        article.title = title.upper()
        article.save()
        self.assertEquals(article.slug, 'this-is-a-title_2')

    def test_auto_existing_author(self):
        author = self.create_person()
        article = Article.objects.create(title=rand_str(),
                                         owner=author.user,
                                         app_config=self.app_config,
                                         publishing_date=datetime.now())
        article.save()
        self.assertEquals(article.author.user, article.owner)
        with self.settings(ALDRYN_NEWSBLOG_CREATE_AUTHOR=False):
            article = Article.objects.create(title=rand_str(),
                                             owner=author.user,
                                             app_config=self.app_config,
                                             publishing_date=datetime.now())
        self.assertEquals(article.author, None)

    def test_auto_new_author(self):
        user = self.create_user()
        article = Article.objects.create(title=rand_str(),
                                         owner=user,
                                         app_config=self.app_config,
                                         publishing_date=datetime.now())
        article.save()
        self.assertEquals(article.author.name, u' '.join(
            (user.first_name, user.last_name)))

    def test_latest_entries_plugin(self):
        page = api.create_page('plugin page',
                               self.template,
                               self.language,
                               parent=self.root_page,
                               published=True)
        placeholder = page.placeholders.all()[0]
        api.add_plugin(placeholder,
                       'LatestEntriesPlugin',
                       self.language,
                       app_config=self.app_config,
                       latest_entries=7)
        plugin = placeholder.get_plugins()[0].get_plugin_instance()[0]
        plugin.save()
        page.publish(self.language)
        articles = [self.create_article() for _ in range(7)]
        another_app_config = NewsBlogConfig.objects.create(namespace='another')
        another_articles = [
            self.create_article(app_config=another_app_config)
            for _ in range(3)
        ]
        response = self.client.get(page.get_absolute_url())
        for article in articles:
            self.assertContains(response, article.title)
        for article in another_articles:
            self.assertNotContains(response, article.title)

    def test_index_simple(self):
        self.index = ArticleIndex()
        content0 = rand_str(prefix='content0_')
        self.setup_categories()

        article = self.create_article(content=content0,
                                      lead_in='lead in text',
                                      title='a title')
        article.categories.add()
        for tag_name in ('tag 1', 'tag2'):
            article.tags.add(tag_name)
        for category in (self.category1, self.category2):
            article.categories.add(category)

        self.assertEqual(self.index.get_title(article), 'a title')
        self.assertEqual(self.index.get_description(article), 'lead in text')
        self.assertTrue('lead in text' in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue(content0 in self.index.get_search_data(
            article, 'en', self.request))
        self.assertTrue(
            'tag 1' in self.index.get_search_data(article, 'en', self.request))
        self.assertTrue(self.category1.name in self.index.get_search_data(
            article, 'en', self.request))

    def test_index_multilingual(self):
        self.index = ArticleIndex()
        content0 = rand_str(prefix='content0_')
        self.setup_categories()

        article_1 = self.create_article(content=content0,
                                        lead_in=u'lead in text',
                                        title=u'a title')
        article_2 = self.create_article(content=content0,
                                        lead_in=u'lead in text',
                                        title=u'second title')
        for article in (article_1, article_2):
            for tag_name in ('tag 1', 'tag2'):
                article.tags.add(tag_name)
            for category in (self.category1, self.category2):
                article.categories.add(category)
        with switch_language(article_2, 'de'):
            article_2.title = u'de title'
            article_2.lead_in = u'de lead in'
            article_2.save()

        PARLER_LANGUAGES = {
            1: (
                {
                    'code': 'de',
                },
                {
                    'code': 'fr',
                },
                {
                    'code': 'en',
                },
            ),
            'default': {
                'hide_untranslated': True,
            }
        }
        LANGUAGES = add_default_language_settings(PARLER_LANGUAGES)
        with override_parler_settings(PARLER_LANGUAGES=LANGUAGES):
            with smart_override('de'):
                language = get_language()
                # english-only article is excluded
                qs = self.index.index_queryset(language)
                self.assertEqual(qs.count(), 1)
                self.assertEqual(
                    qs.translated(language, title__icontains='title').count(),
                    1)
                # the language is correctly setup
                for article_de in qs:
                    self.assertEqual(self.index.get_title(article_de),
                                     'de title')
                    self.assertEqual(self.index.get_description(article_de),
                                     'de lead in')