Ejemplo n.º 1
0
def convert_to_article(request, entry_id):
    def get_entry_author(entry):
        written_by_user = '******' % entry.user.username
        written_by_anon = 'Written by: Anonymous'
        if entry.hide_real_name:
            return written_by_anon
        return written_by_user

    entry = get_object_or_404(YourWordsCompetitionEntry, pk=entry_id)
    if not entry.article_page:
        competition_index_page = (
            YourWordsCompetitionIndexPage.objects.descendant_of(
                request.site.root_page).live().first())
        article = ArticlePage(title=entry.story_name,
                              slug='yourwords-entry-%s-%s' %
                              (cautious_slugify(entry.story_name), entry.pk),
                              body=json.dumps([{
                                  "type": "paragraph",
                                  "value": get_entry_author(entry)
                              }, {
                                  "type": "paragraph",
                                  "value": entry.story_text
                              }]))
        competition_index_page.add_child(instance=article)
        article.save_revision()
        article.unpublish()

        entry.article_page = article
        entry.save()
        return redirect('/admin/pages/%d/move/' % article.id)
    return redirect('/admin/pages/%d/edit/' % entry.article_page.id)
Ejemplo n.º 2
0
def submission_article(request, survey_id, submission_id):
    # get the specific submission entry
    survey_page = get_object_or_404(Page, id=survey_id).specific
    SubmissionClass = survey_page.get_submission_class()

    submission = SubmissionClass.objects.filter(page=survey_page).filter(
        pk=submission_id).first()
    if not submission.article_page:
        survey_index_page = (SurveysIndexPage.objects.descendant_of(
            request.site.root_page).live().first())
        body = []
        for value in submission.get_data().values():
            body.append({"type": "paragraph", "value": str(value)})
        article = ArticlePage(
            title='yourwords-entry-%s' % cautious_slugify(submission_id),
            slug='yourwords-entry-%s' % cautious_slugify(submission_id),
            body=json.dumps(body))
        survey_index_page.add_child(instance=article)
        article.save_revision()
        article.unpublish()

        submission.article_page = article
        submission.save()
        return redirect('/admin/pages/%d/move/' % article.id)
    return redirect('/admin/pages/%d/edit/' % submission.article_page.id)
Ejemplo n.º 3
0
    def mk_articles(self, parent, count=2, **kwargs):
        articles = []

        for i in range(count):
            data = {}
            data.update({
                'title':
                'Test page %s' % (i, ),
                'subtitle':
                'Sample page description for %s' % (i, ),
                'body':
                json.dumps([{
                    'type': 'paragraph',
                    'value': 'Sample page content for %s' % (i, )
                }]),
            })
            data.update(kwargs)
            data.update({'slug': generate_slug(data['title'])})
            article = ArticlePage(**data)
            if not article.first_published_at:
                article.first_published_at = timezone.now()
            parent.add_child(instance=article)
            article.save_revision().publish()
            articles.append(article)
        return articles
Ejemplo n.º 4
0
def submission_article(request, survey_id, submission_id):
    # get the specific submission entry
    survey_page = get_object_or_404(Page, id=survey_id).specific
    SubmissionClass = survey_page.get_submission_class()

    submission = SubmissionClass.objects.filter(
        page=survey_page).filter(pk=submission_id).first()
    if not submission.article_page:
        survey_index_page = (
            SurveysIndexPage.objects.descendant_of(
                request.site.root_page).live().first())
        body = []
        for value in submission.get_data().values():
            body.append({"type": "paragraph", "value": str(value)})
        article = ArticlePage(
            title='yourwords-entry-%s' % cautious_slugify(submission_id),
            slug='yourwords-entry-%s' % cautious_slugify(submission_id),
            body=json.dumps(body)
        )
        survey_index_page.add_child(instance=article)
        article.save_revision()
        article.unpublish()

        submission.article_page = article
        submission.save()
        return redirect('/admin/pages/%d/move/' % article.id)
    return redirect('/admin/pages/%d/edit/' % submission.article_page.id)
Ejemplo n.º 5
0
def convert_to_article(request, entry_id):
    def get_entry_author(entry):
        written_by_user = '******' % entry.user.username
        written_by_anon = 'Written by: Anonymous'
        if entry.hide_real_name:
            return written_by_anon
        return written_by_user

    entry = get_object_or_404(YourWordsCompetitionEntry, pk=entry_id)
    if not entry.article_page:
        competition_index_page = (
            YourWordsCompetitionIndexPage.objects.live().first())
        article = ArticlePage(
            title=entry.story_name,
            slug='yourwords-entry-%s' % cautious_slugify(entry.story_name),
            body=json.dumps([{
                "type": "paragraph", "value": get_entry_author(entry),
                "type": "paragraph", "value": entry.story_text,
            }])
        )
        competition_index_page.add_child(instance=article)
        article.save_revision()
        article.unpublish()

        entry.article_page = article
        entry.save()
        return redirect('/admin/pages/%d/move/' % article.id)
    return redirect('/admin/pages/%d/edit/' % entry.article_page.id)
Ejemplo n.º 6
0
 def add_article(self, title, tags):
     new_article = ArticlePage(title=title)
     self.section.add_child(instance=new_article)
     new_article.save_revision()
     for tag in tags:
         ArticlePageTags.objects.create(
             tag=tag,
             page=new_article,
         )
     return new_article
Ejemplo n.º 7
0
    def test_copy_to_all_scheduled_page(self):
        self.user = self.login()

        date = datetime.datetime(3099, 3, 10, 17, 0, tzinfo=UTC)
        article = ArticlePage(
            title='article 1', slug='article1', go_live_at=date, live=False)
        self.yourmind.add_child(instance=article)
        article.save_revision().publish()

        self.assertEqual(article.status_string, 'scheduled')

        self.client.post(reverse('copy-to-all', args=(article.id,)))
        new_article = ArticlePage.objects.descendant_of(
            self.main2).get(slug=article.slug)
        self.assertEqual(new_article.status_string, 'scheduled')
Ejemplo n.º 8
0
    def test_copy_to_all_scheduled_page(self):
        self.user = self.login()

        date = datetime.datetime(3099, 3, 10, 17, 0, tzinfo=UTC)
        article = ArticlePage(
            title='article 1', slug='article1', go_live_at=date, live=False)
        self.yourmind.add_child(instance=article)
        article.save_revision().publish()

        tag = self.mk_tag(parent=self.tag_index, slug='tag')
        tag.save_revision().publish()
        tag2 = self.mk_tag(parent=self.tag_index2, slug='tag')
        tag2.save_revision().publish()
        ArticlePageTags.objects.create(page=article, tag=tag)
        article.refresh_from_db()

        self.assertEqual(article.status_string, 'scheduled')

        self.client.post(reverse('copy-to-all', args=(article.id,)))
        new_article = ArticlePage.objects.descendant_of(
            self.main2).get(slug=article.slug)
        self.assertEqual(new_article.status_string, 'scheduled')
Ejemplo n.º 9
0
    def mk_articles(self, parent, count=2, **kwargs):
        articles = []

        for i in range(count):
            data = {}
            data.update({
                'title': 'Test page %s' % (i, ),
                'subtitle': 'Sample page description for %s' % (i, ),
                'body': json.dumps([{
                    'type': 'paragraph',
                    'value': 'Sample page content for %s' % (i, )}]),
            })
            data.update(kwargs)
            data.update({
                'slug': generate_slug(data['title'])
            })
            article = ArticlePage(**data)
            if not article.first_published_at:
                article.first_published_at = timezone.now()
            parent.add_child(instance=article)
            article.save_revision().publish()
            articles.append(article)
        return articles
Ejemplo n.º 10
0
class ManagementCommandsTest(TestCase, MoloTestCaseMixin):
    def setUp(self):
        self.mk_main()
        self.main = Main.objects.all().first()
        self.language_setting = Languages.objects.create(
            site_id=self.main.get_site().pk)
        self.english = SiteLanguageRelation.objects.create(
            language_setting=Languages.for_site(self.main.get_site()),
            locale='en',
            is_active=True)
        self.spanish = SiteLanguageRelation.objects.create(
            language_setting=Languages.for_site(self.main.get_site()),
            locale='sp',
            is_active=True)
        self.french = SiteLanguageRelation.objects.create(
            language_setting=Languages.for_site(self.main.get_site()),
            locale='fr',
            is_active=True)
        self.yourmind = self.mk_section(
            self.section_index, title='Your mind')
        self.article = ArticlePage(title='Test Article', slug='test-article')
        self.yourmind.add_child(instance=self.article)
        self.article.save_revision().publish()

    def test_switch_main_language(self):
        out = StringIO()
        tag = Tag(title='love', slug='love')
        self.tag_index.add_child(instance=tag)
        tag.save_revision().publish()
        for relation in LanguageRelation.objects.filter(
                language__is_main_language=True):
            self.assertEqual(relation.language.locale, 'en')
        self.assertTrue(self.english.is_main_language)
        call_command('switch_main_language', 'id', std_out=out)
        self.assertTrue(SiteLanguage.objects.get(locale='id').is_main_language)
        self.assertFalse(
            LanguageRelation.objects.filter(language__locale='en').exists())
        for relation in LanguageRelation.objects.filter(
                language__is_main_language=True):
            self.assertEqual(relation.language.locale, 'id')

    def test_add_language_to_pages(self):
        call_command('add_language_to_pages')
        english = SiteLanguage.objects.first()
        article = ArticlePage.objects.get(pk=self.article.pk)
        yourmind = SectionPage.objects.get(pk=self.yourmind.pk)
        self.assertEqual(article.language, english)
        self.assertEqual(yourmind.language, english)

    def test_add_translated_pages_to_pages(self):
        self.mk_main2()
        self.main2 = Main.objects.all().last()
        self.language_setting2 = Languages.objects.create(
            site_id=self.main2.get_site().pk)
        self.english2 = SiteLanguageRelation.objects.create(
            language_setting=self.language_setting2,
            locale='en',
            is_active=True)

        self.spanish2 = SiteLanguageRelation.objects.create(
            language_setting=self.language_setting2,
            locale='es',
            is_active=True)
        self.yourmind2 = self.mk_section(
            self.section_index2, title='Your mind')
        # create article in english with translation in spanish and french
        english_article = self.article
        spanish_article = self.mk_article_translation(
            english_article, self.spanish)
        french_article = self.mk_article_translation(
            english_article, self.french)
        PageTranslation.objects.create(
            page=english_article, translated_page=english_article)

        # create section in english with translations
        english_section = self.yourmind
        english_section2 = self.yourmind2
        spanish_section = self.mk_section_translation(
            self.yourmind, self.spanish)
        spanish_section2 = self.mk_section_translation(
            self.yourmind2, self.spanish2)
        french_section = self.mk_section_translation(
            self.yourmind, self.french)

        # need to delete it here since it gets added in base
        for article in ArticlePage.objects.all():
            article.translated_pages.clear()
            article.save()
        for section in SectionPage.objects.all():
            section.translated_pages.clear()
            section.save()
        self.assertFalse(english_article.translated_pages.exists())
        self.assertFalse(spanish_article.translated_pages.exists())
        self.assertFalse(french_article.translated_pages.exists())
        self.assertFalse(english_section.translated_pages.exists())
        self.assertFalse(english_section2.translated_pages.exists())
        self.assertFalse(spanish_section.translated_pages.exists())
        self.assertFalse(spanish_section2.translated_pages.exists())
        self.assertFalse(french_section.translated_pages.exists())

        # run command to add languages to these pages
        call_command('add_language_to_pages')

        # run command to add list of translated_pages to these pages
        call_command('add_translated_pages_to_pages')

        # test translated pages for articles
        self.assertTrue(english_article.translated_pages.filter(
            pk=spanish_article.pk).exists())
        self.assertTrue(english_article.translated_pages.filter(
            pk=french_article.pk).exists())
        self.assertFalse(english_article.translated_pages.filter(
            pk=english_article.pk).exists())
        self.assertTrue(spanish_article.translated_pages.filter(
            pk=english_article.pk).exists())
        self.assertTrue(spanish_article.translated_pages.filter(
            pk=french_article.pk).exists())
        self.assertFalse(spanish_article.translated_pages.filter(
            pk=spanish_article.pk).exists())
        self.assertTrue(french_article.translated_pages.filter(
            pk=english_article.pk).exists())
        self.assertTrue(french_article.translated_pages.filter(
            pk=spanish_article.pk).exists())

        # test translated pages for sections
        self.assertTrue(english_section.translated_pages.filter(
            pk=spanish_section.pk).exists())
        self.assertTrue(english_section2.translated_pages.filter(
            pk=spanish_section2.pk).exists())
        self.assertTrue(english_section.translated_pages.filter(
            pk=french_section.pk).exists())
        self.assertTrue(spanish_section.translated_pages.filter(
            pk=english_section.pk).exists())
        self.assertTrue(spanish_section2.translated_pages.filter(
            pk=english_section2.pk).exists())
        self.assertTrue(spanish_section.translated_pages.filter(
            pk=french_section.pk).exists())
        self.assertTrue(french_section.translated_pages.filter(
            pk=english_section.pk).exists())
        self.assertTrue(french_section.translated_pages.filter(
            pk=spanish_section.pk).exists())

    def test_add_feature_in_latest_date_to_article(self):
        self.assertEqual(self.article.featured_in_latest_start_date, None)
        now = datetime.now()
        now = pytz.utc.localize(now)

        # create the csv file with the article slug and date
        data = {
            self.article.slug: now
        }
        fake_namefile = tempfile.NamedTemporaryFile()
        with open(fake_namefile.name, 'w') as fake_csv:
            writer = csv.writer(fake_csv)
            writer.writerow(['slug', 'date'])
            for key, value in data.items():
                writer.writerow([key, value])
        # call the command
        out = StringIO()
        call_command(
            'set_promotional_dates_from_csv', fake_namefile.name, std_out=out)
        self.assertEqual(
            ArticlePage.objects.last().featured_in_latest_start_date, now)
        fake_namefile.close()

    def test_add_tag_to_article(self):
        self.assertEqual(ArticlePageTags.objects.count(), 0)
        # create the tag data
        tag = Tag(title='over 18', slug='over-18')
        self.tag_index.add_child(instance=tag)
        tag.save_revision().publish()
        tag_data = {
            self.article.slug: tag.title
        }

        # create csv
        fake_namefile = tempfile.NamedTemporaryFile()
        with open(fake_namefile.name, 'w') as fake_csv:
            writer = csv.writer(fake_csv)
            writer.writerow(['slug', 'date'])
            for key, value in tag_data.items():
                writer.writerow([key, value])

        # call command to link tag to article
        out = StringIO()
        call_command(
            'add_tag_to_article', fake_csv.name, 'en',
            std_out=out)

        # it should create a Article Relationship with the Tag
        self.assertEqual(ArticlePageTags.objects.count(), 1)
        self.assertEqual(
            ArticlePageTags.objects.first().tag.title, tag.title)
        self.assertEqual(
            ArticlePageTags.objects.first().page, self.article)

        fake_namefile.close()
Ejemplo n.º 11
0
 def add_article(self, title):
     new_article = ArticlePage(title=title)
     self.section.add_child(instance=new_article)
     new_article.save_revision()
     return new_article
Ejemplo n.º 12
0
class TestPersistentSurveysSegmentsAdapter(TestCase, MoloTestCaseMixin):
    def setUp(self):
        self.mk_main()
        self.request = RequestFactory().get('/')
        self.request.user = self.login()
        session_middleware = SessionMiddleware()
        session_middleware.process_request(self.request)
        self.request.session.save()

        self.section = SectionPage(title='test section')
        self.section_index.add_child(instance=self.section)

        self.important_tag = Tag(title='important tag')
        self.another_tag = Tag(title='other tag')
        self.tags = [self.important_tag, self.another_tag]
        for tag in self.tags:
            self.tag_index.add_child(instance=tag)
            tag.save_revision()

        self.page = ArticlePage(title='test article')
        self.section.add_child(instance=self.page)
        self.page.save_revision()

        for tag in self.tags:
            ArticlePageTags.objects.create(
                tag=tag,
                page=self.page,
            )

        self.adapter = PersistentSurveysSegmentsAdapter(self.request)

    def test_no_exception_raised_if_user_not_set(self):
        del self.request.user
        try:
            self.adapter.add_page_visit(self.page)
        except AttributeError as e:
            self.fail('add_page_visit() raised AttributeError: {0}'.format(e))

    def test_no_pageview_stored_if_not_articlepage(self):
        self.adapter.add_page_visit(self.section)
        self.assertEqual(MoloSurveyPageView.objects.all().count(), 0)

    def test_no_pageview_stored_for_anonymous_user(self):
        self.request.user = AnonymousUser()
        self.adapter.add_page_visit(self.page)
        self.assertEqual(MoloSurveyPageView.objects.all().count(), 0)

    def test_pageview_stored(self):
        self.adapter.add_page_visit(self.page)

        pageviews = MoloSurveyPageView.objects.all()

        self.assertEqual(pageviews.count(), 1)

        self.assertEqual(pageviews[0].user, self.request.user)
        self.assertEqual(pageviews[0].page, self.page)

    def test_get_tag_count_zero_if_no_user(self):
        del self.request.user

        with self.assertNumQueries(0):
            count = self.adapter.get_tag_count(self.important_tag)

        self.assertEqual(count, 0)

    def test_get_tag_count_zero_if_anonymous_user(self):
        self.request.user = AnonymousUser()

        with self.assertNumQueries(0):
            count = self.adapter.get_tag_count(self.important_tag)

        self.assertEqual(count, 0)

    def test_get_tag_count_only_counts_current_user(self):
        another_user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******',
        )
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloSurveyPageView.objects.create(
            user=another_user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_tag_count_only_counts_specified_tag(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_tag_count_uses_date_from_if_provided(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(
            self.important_tag,
            date_from=make_aware(datetime(2099, 12, 31)),
        ), 0)

    def test_get_tag_count_uses_date_to_if_provided(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(
            self.important_tag,
            date_to=make_aware(datetime(2000, 1, 1)),
        ), 0)

    def test_get_tag_count_groups_by_unique_article(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_visit_count_zero_no_request_user(self):
        del self.request.user
        self.assertEqual(self.adapter.get_visit_count(self.page), 0)

    def test_get_visit_count_zero_for_anonymous_user(self):
        self.request.user = AnonymousUser()
        self.assertEqual(self.adapter.get_visit_count(self.page), 0)

    def test_get_visit_count_zero_if_page_not_provided(self):
        self.assertEqual(self.adapter.get_visit_count(), 0)

    def test_get_visit_counts_pageviews_for_user_and_page(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_visit_count(self.page), 1)
Ejemplo n.º 13
0
class TestPersistentFormsSegmentsAdapter(TestCase, MoloTestCaseMixin):
    def setUp(self):
        self.mk_main()
        self.request = RequestFactory().get('/')
        self.request.user = self.login()
        session_middleware = SessionMiddleware()
        session_middleware.process_request(self.request)
        self.request.session.save()

        self.section = SectionPage(title='test section')
        self.section_index.add_child(instance=self.section)

        self.important_tag = Tag(title='important tag')
        self.another_tag = Tag(title='other tag')
        self.tags = [self.important_tag, self.another_tag]
        for tag in self.tags:
            self.tag_index.add_child(instance=tag)
            tag.save_revision()

        self.page = ArticlePage(title='test article')
        self.section.add_child(instance=self.page)
        self.page.save_revision()

        for tag in self.tags:
            ArticlePageTags.objects.create(
                tag=tag,
                page=self.page,
            )

        self.adapter = PersistentFormsSegmentsAdapter(self.request)

    def test_no_exception_raised_if_user_not_set(self):
        del self.request.user
        try:
            self.adapter.add_page_visit(self.page)
        except AttributeError as e:
            self.fail('add_page_visit() raised AttributeError: {0}'.format(e))

    def test_no_pageview_stored_if_not_articlepage(self):
        self.adapter.add_page_visit(self.section)
        self.assertEqual(MoloFormPageView.objects.all().count(), 0)

    def test_no_pageview_stored_for_anonymous_user(self):
        self.request.user = AnonymousUser()
        self.adapter.add_page_visit(self.page)
        self.assertEqual(MoloFormPageView.objects.all().count(), 0)

    def test_pageview_stored(self):
        self.adapter.add_page_visit(self.page)

        pageviews = MoloFormPageView.objects.all()

        self.assertEqual(pageviews.count(), 1)

        self.assertEqual(pageviews[0].user, self.request.user)
        self.assertEqual(pageviews[0].page, self.page)

    def test_get_tag_count_zero_if_no_user(self):
        del self.request.user

        with self.assertNumQueries(0):
            count = self.adapter.get_tag_count(self.important_tag)

        self.assertEqual(count, 0)

    def test_get_tag_count_zero_if_anonymous_user(self):
        self.request.user = AnonymousUser()

        with self.assertNumQueries(0):
            count = self.adapter.get_tag_count(self.important_tag)

        self.assertEqual(count, 0)

    def test_get_tag_count_only_counts_current_user(self):
        another_user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******',
        )
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloFormPageView.objects.create(
            user=another_user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_tag_count_only_counts_specified_tag(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_tag_count_uses_date_from_if_provided(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(
            self.adapter.get_tag_count(
                self.important_tag,
                date_from=make_aware(datetime(2099, 12, 31)),
            ), 0)

    def test_get_tag_count_uses_date_to_if_provided(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(
            self.adapter.get_tag_count(
                self.important_tag,
                date_to=make_aware(datetime(2000, 1, 1)),
            ), 0)

    def test_get_tag_count_groups_by_unique_article(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_visit_count_zero_no_request_user(self):
        del self.request.user
        self.assertEqual(self.adapter.get_visit_count(self.page), 0)

    def test_get_visit_count_zero_for_anonymous_user(self):
        self.request.user = AnonymousUser()
        self.assertEqual(self.adapter.get_visit_count(self.page), 0)

    def test_get_visit_count_zero_if_page_not_provided(self):
        self.assertEqual(self.adapter.get_visit_count(), 0)

    def test_get_visit_counts_pageviews_for_user_and_page(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_visit_count(self.page), 1)
Ejemplo n.º 14
0
class ManagementCommandsTest(TestCase, MoloTestCaseMixin):
    def setUp(self):
        self.mk_main()
        self.main = Main.objects.all().first()
        self.language_setting = Languages.objects.create(
            site_id=self.main.get_site().pk)
        self.english = SiteLanguageRelation.objects.create(
            language_setting=Languages.for_site(self.main.get_site()),
            locale='en',
            is_active=True)
        self.spanish = SiteLanguageRelation.objects.create(
            language_setting=Languages.for_site(self.main.get_site()),
            locale='sp',
            is_active=True)
        self.french = SiteLanguageRelation.objects.create(
            language_setting=Languages.for_site(self.main.get_site()),
            locale='fr',
            is_active=True)
        self.yourmind = self.mk_section(self.section_index, title='Your mind')
        self.article = ArticlePage(title='Test Article', slug='test-article')
        self.yourmind.add_child(instance=self.article)
        self.article.save_revision().publish()

    def test_switch_main_language(self):
        tag = Tag(title='love', slug='love')
        self.tag_index.add_child(instance=tag)
        tag.save_revision().publish()
        kw = dict(language__is_main_language=True)
        for relation in LanguageRelation.objects.filter(**kw):
            self.assertEqual(relation.language.locale, 'en')
        self.assertTrue(self.english.is_main_language)
        call_command('switch_main_language', 'id')
        self.assertTrue(SiteLanguage.objects.get(locale='id').is_main_language)
        self.assertFalse(
            LanguageRelation.objects.filter(language__locale='en').exists())
        for relation in LanguageRelation.objects.filter(**kw):
            self.assertEqual(relation.language.locale, 'id')

    def test_add_language_to_pages(self):
        call_command('add_language_to_pages')
        english = SiteLanguage.objects.first()
        article = ArticlePage.objects.get(pk=self.article.pk)
        yourmind = SectionPage.objects.get(pk=self.yourmind.pk)
        self.assertEqual(article.language, english)
        self.assertEqual(yourmind.language, english)

    def test_add_translated_pages_to_pages(self):
        self.mk_main2()
        self.main2 = Main.objects.all().last()
        self.language_setting2 = Languages.objects.create(
            site_id=self.main2.get_site().pk)
        self.english2 = SiteLanguageRelation.objects.create(
            language_setting=self.language_setting2,
            locale='en',
            is_active=True)

        self.spanish2 = SiteLanguageRelation.objects.create(
            language_setting=self.language_setting2,
            locale='es',
            is_active=True)
        self.yourmind2 = self.mk_section(self.section_index2,
                                         title='Your mind')
        # create article in english with translation in spanish and french
        english_article = self.article
        spanish_article = self.mk_article_translation(english_article,
                                                      self.spanish)
        french_article = self.mk_article_translation(english_article,
                                                     self.french)
        PageTranslation.objects.create(page=english_article,
                                       translated_page=english_article)

        # create section in english with translations
        english_section = self.yourmind
        english_section2 = self.yourmind2
        spanish_section = self.mk_section_translation(self.yourmind,
                                                      self.spanish)
        spanish_section2 = self.mk_section_translation(self.yourmind2,
                                                       self.spanish2)
        french_section = self.mk_section_translation(self.yourmind,
                                                     self.french)

        # need to delete it here since it gets added in base
        for article in ArticlePage.objects.all():
            article.translated_pages.clear()
            article.save()
        for section in SectionPage.objects.all():
            section.translated_pages.clear()
            section.save()
        self.assertFalse(english_article.translated_pages.exists())
        self.assertFalse(spanish_article.translated_pages.exists())
        self.assertFalse(french_article.translated_pages.exists())
        self.assertFalse(english_section.translated_pages.exists())
        self.assertFalse(english_section2.translated_pages.exists())
        self.assertFalse(spanish_section.translated_pages.exists())
        self.assertFalse(spanish_section2.translated_pages.exists())
        self.assertFalse(french_section.translated_pages.exists())

        # run command to add languages to these pages
        call_command('add_language_to_pages')

        # run command to add list of translated_pages to these pages
        call_command('add_translated_pages_to_pages')

        # test translated pages for articles
        self.assertTrue(
            english_article.translated_pages.filter(
                pk=spanish_article.pk).exists())
        self.assertTrue(
            english_article.translated_pages.filter(
                pk=french_article.pk).exists())
        self.assertFalse(
            english_article.translated_pages.filter(
                pk=english_article.pk).exists())
        self.assertTrue(
            spanish_article.translated_pages.filter(
                pk=english_article.pk).exists())
        self.assertTrue(
            spanish_article.translated_pages.filter(
                pk=french_article.pk).exists())
        self.assertFalse(
            spanish_article.translated_pages.filter(
                pk=spanish_article.pk).exists())
        self.assertTrue(
            french_article.translated_pages.filter(
                pk=english_article.pk).exists())
        self.assertTrue(
            french_article.translated_pages.filter(
                pk=spanish_article.pk).exists())

        # test translated pages for sections
        self.assertTrue(
            english_section.translated_pages.filter(
                pk=spanish_section.pk).exists())
        self.assertTrue(
            english_section2.translated_pages.filter(
                pk=spanish_section2.pk).exists())
        self.assertTrue(
            english_section.translated_pages.filter(
                pk=french_section.pk).exists())
        self.assertTrue(
            spanish_section.translated_pages.filter(
                pk=english_section.pk).exists())
        self.assertTrue(
            spanish_section2.translated_pages.filter(
                pk=english_section2.pk).exists())
        self.assertTrue(
            spanish_section.translated_pages.filter(
                pk=french_section.pk).exists())
        self.assertTrue(
            french_section.translated_pages.filter(
                pk=english_section.pk).exists())
        self.assertTrue(
            french_section.translated_pages.filter(
                pk=spanish_section.pk).exists())

    def test_add_feature_in_latest_date_to_article(self):
        self.assertEqual(self.article.featured_in_latest_start_date, None)
        now = datetime.now()
        now = pytz.utc.localize(now)

        # create the csv file with the article slug and date
        data = {self.article.slug: now}
        fake_namefile = tempfile.NamedTemporaryFile()
        with open(fake_namefile.name, 'w') as fake_csv:
            writer = csv.writer(fake_csv)
            writer.writerow(['slug', 'date'])
            for key, value in data.items():
                writer.writerow([key, value])
        # call the command
        call_command('set_promotional_dates_from_csv', fake_namefile.name)
        self.assertEqual(
            ArticlePage.objects.last().featured_in_latest_start_date, now)
        fake_namefile.close()

    def test_add_tag_to_article(self):
        self.assertEqual(ArticlePageTags.objects.count(), 0)
        # create the tag data
        tag = Tag(title='over 18', slug='over-18')
        self.tag_index.add_child(instance=tag)
        tag.save_revision().publish()
        tag_data = {self.article.slug: tag.title}

        # create csv
        fake_namefile = tempfile.NamedTemporaryFile()
        with open(fake_namefile.name, 'w') as fake_csv:
            writer = csv.writer(fake_csv)
            writer.writerow(['slug', 'date'])
            for key, value in tag_data.items():
                writer.writerow([key, value])

        # call command to link tag to article
        call_command('add_tag_to_article', fake_csv.name, 'en')

        # it should create a Article Relationship with the Tag
        self.assertEqual(ArticlePageTags.objects.count(), 1)
        self.assertEqual(ArticlePageTags.objects.first().tag.title, tag.title)
        self.assertEqual(ArticlePageTags.objects.first().page, self.article)

        fake_namefile.close()