def test_cache_gets_called_when_visiting_filterable_page(self):
        # Create a filterable page
        page = BrowseFilterablePage(
            title='test browse filterable page',
            slug='test-browse-filterable-page'
        )
        page.content = StreamValue(
            page.content.stream_block,
            [atomic.filter_controls],
            True
        )
        publish_page(page)

        # Add a child to that filterable page so that there are results
        # with a post preview
        child_page = BlogPage(
            title='test blog page',
            slug='test-blog-page'
        )
        page.add_child(instance=child_page)

        cache = caches['post_preview']
        with patch.object(cache, 'add') as add_to_cache:
            # Navigate to the filterable page so that `post-preview.html` loads
            self.client.get('/test-browse-filterable-page/')

            self.assertTrue(add_to_cache.called)
Exemple #2
0
    def test_cache_gets_called_when_visiting_filterable_page(self):
        # Create a filterable page
        page = BrowseFilterablePage(
            title='test browse filterable page',
            slug='test-browse-filterable-page',
            content=json.dumps([atomic.filter_controls])
        )
        publish_page(page)

        # Add a child to that filterable page so that there are results
        # with a post preview
        child_page = BlogPage(
            title='test blog page',
            slug='test-blog-page'
        )
        page.add_child(instance=child_page)

        self.rebuild_elasticsearch_index('v1', stdout=StringIO())

        cache = caches['post_preview']
        with patch.object(cache, 'add') as add_to_cache:
            # Navigate to the filterable page so that `post-preview.html` loads
            self.client.get('/test-browse-filterable-page/')

            self.assertTrue(add_to_cache.called)
    def setUp(self):
        self.filterable_page = BrowseFilterablePage(title="Blog", slug="test")
        self.root = Site.objects.get(is_default_site=True).root_page
        self.root.add_child(instance=self.filterable_page)

        self.page = BlogPage(
            title="Test",
            slug="one",
            live=True,
        )
        self.filterable_page.add_child(instance=self.page)
 def setUp(self):
     self.factory = RequestFactory()
     self.filterable_page = BrowseFilterablePage(title="Blog", slug="test")
     self.root = Site.objects.get(is_default_site=True).root_page
     self.root.add_child(instance=self.filterable_page)
     self.page = BlogPage(title="Child test page", live=True)
     self.archived_page = BlogPage(title="Archive test page",
                                   live=True,
                                   is_archived='yes')
     self.filterable_page.add_child(instance=self.page)
     self.filterable_page.add_child(instance=self.archived_page)
 def test_text_intro(self):
     """Text introduction value correctly displays on a Browse Filterable Page"""
     bfp = BrowseFilterablePage(
         title='Browse Filterable Page',
         slug='browse-filterable-page',
     )
     bfp.header = StreamValue(bfp.header.stream_block,
                              [atomic.text_introduction], True)
     publish_page(child=bfp)
     response = django_client.get('/browse-filterable-page/')
     self.assertContains(response, 'this is an intro')
    def setUp(self):
        self.filterable_page = BrowseFilterablePage(title="Blog", slug="test")
        self.root = Site.objects.get(is_default_site=True).root_page
        self.root.add_child(instance=self.filterable_page)

        self.page = BlogPage(
            title="Test",
            slug="one",
            live=True,
        )
        self.filterable_page.add_child(instance=self.page)

        self.rebuild_elasticsearch_index('v1', stdout=StringIO())
Exemple #7
0
    def test_no_events(self):
        events_page = BrowseFilterablePage(title='Events',
                                           slug='events',
                                           content='Events',
                                           live=True)
        self.root_page.add_child(instance=events_page)
        archive_page = EventArchivePage(title='Archive',
                                        slug='archive',
                                        content='archive',
                                        live=True)
        events_page.add_child(instance=archive_page)

        self.assertIn('No live event pages found.', self.call_command())
Exemple #8
0
    def test_past_events_get_archived(self):
        events_page = BrowseFilterablePage(title='Events',
                                           slug='events',
                                           content='Events',
                                           live=True)
        self.root_page.add_child(instance=events_page)
        archive_page = EventArchivePage(title='Archive',
                                        slug='archive',
                                        content='archive',
                                        live=True)
        events_page.add_child(instance=archive_page)
        single_day_event_page = EventPage(
            title='Single-day event with implied end_dt',
            start_dt=datetime.datetime(2020, 2, 1, tzinfo=pytz.UTC))
        events_page.add_child(instance=single_day_event_page)
        multi_day_event_page = EventPage(
            title='Multi-day event',
            start_dt=datetime.datetime(2020, 1, 1, tzinfo=pytz.UTC),
            end_dt=datetime.datetime(2020, 1, 2, tzinfo=pytz.UTC))
        events_page.add_child(instance=multi_day_event_page)

        self.call_command()
        single_day_event_page.refresh_from_db()
        multi_day_event_page.refresh_from_db()

        self.assertEqual(single_day_event_page.parent(), archive_page)
        self.assertEqual(multi_day_event_page.parent(), archive_page)
 def test_text_intro(self):
     """Text introduction value correctly displays on a Browse Filterable Page"""
     bfp = BrowseFilterablePage(
         title='Browse Filterable Page',
         slug='browse-filterable-page',
     )
     bfp.header = StreamValue(
         bfp.header.stream_block,
         [atomic.text_introduction],
         True
     )
     publish_page(child=bfp)
     response = django_client.get('/browse-filterable-page/')
     self.assertContains(response, 'this is an intro')
    def setUp(self):
        self.filterable_page = BrowseFilterablePage(
            title="Blog",
            slug="test",
            live=True
        )
        self.url_path = self.filterable_page.url

        self.root = Site.objects.get(is_default_site=True).root_page
        self.root.add_child(instance=self.filterable_page)

        self.page1 = BlogPage(
            title="Page archived in 2018",
            live=True,
            is_archived="yes",
            first_published_at=datetime.datetime(2018, 1, 1, tzinfo=pytz.UTC),
            last_published_at=datetime.datetime(2021, 1, 1, tzinfo=pytz.UTC)
        )
        self.page2 = BlogPage(
            title="Page archived in 2019",
            live=True,
            is_archived="yes",
            first_published_at=datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC),
            last_published_at=datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC)
        )
        self.page3 = BlogPage(
            title="Page archived in 2020",
            live=True,
            is_archived="yes",
            first_published_at=datetime.datetime(2020, 1, 1, tzinfo=pytz.UTC),
            last_published_at=datetime.datetime(2020, 1, 1, tzinfo=pytz.UTC)
        )
        self.page4 = BlogPage(
            title="Page published in 2021",
            live=True,
            is_archived="no",
            first_published_at=datetime.datetime(2021, 1, 1, tzinfo=pytz.UTC),
            last_published_at=datetime.datetime(2021, 1, 1, tzinfo=pytz.UTC)
        )

        self.filterable_page.add_child(instance=self.page1)
        self.filterable_page.add_child(instance=self.page2)
        self.filterable_page.add_child(instance=self.page3)
        self.filterable_page.add_child(instance=self.page4)

        self.stdout = StringIO()
    def setUp(self):
        self.filter_controls = filter_controls

        self.filterable_page = BrowseFilterablePage(title="Blog", slug="test")
        self.root = Site.objects.get(is_default_site=True).root_page
        self.root.add_child(instance=self.filterable_page)

        self.set_filterable_controls(filter_controls)

        self.child_page = BlogPage(title="Child test page", live=True)
        self.sibling_page = BlogPage(title="Sibling test page", live=True)
        self.archived_sibling_page = BlogPage(title="Archive test page",
                                              live=True,
                                              is_archived='yes')
        self.filterable_page.add_child(instance=self.child_page)
        self.filterable_page.get_parent().add_child(instance=self.sibling_page)
        self.filterable_page.get_parent().add_child(
            instance=self.archived_sibling_page)
Exemple #12
0
    def test_no_archive_page(self):
        events_page = BrowseFilterablePage(title='Events',
                                           slug='events',
                                           content='Events',
                                           live=True)
        self.root_page.add_child(instance=events_page)

        with self.assertRaises(CommandError):
            self.call_command()
Exemple #13
0
class FilterableListRelationsTestCase(TestCase):
    def setUp(self):
        self.filter_controls = filter_controls

        self.filterable_page = BrowseFilterablePage(title="Blog", slug="test")
        self.root = Site.objects.get(is_default_site=True).root_page
        self.root.add_child(instance=self.filterable_page)

        self.set_filterable_controls(filter_controls)

        self.child_page = BlogPage(title="Child test page", live=True)
        self.sibling_page = BlogPage(title="Sibling test page", live=True)
        self.archived_sibling_page = BlogPage(title="Archive test page",
                                              live=True,
                                              is_archived='yes')
        self.filterable_page.add_child(instance=self.child_page)
        self.filterable_page.get_parent().add_child(instance=self.sibling_page)
        self.filterable_page.get_parent().add_child(
            instance=self.archived_sibling_page)

    def set_filterable_controls(self, value):
        self.filterable_page.content = StreamValue(
            self.filterable_page.content.stream_block, [value], True)
        self.filterable_page.save()

    def test_get_filterable_children_pages(self):
        filter_controls['value']['filter_children'] = True
        filter_controls['value']['filter_siblings'] = False
        self.set_filterable_controls(self.filter_controls)

        qs = self.filterable_page.get_filterable_queryset()
        self.assertEqual(qs.count(), 1)
        self.assertEqual(qs[0].pk, self.child_page.pk)

    def test_get_filterable_siblings_pages(self):
        filter_controls['value']['filter_children'] = False
        filter_controls['value']['filter_siblings'] = True
        self.set_filterable_controls(self.filter_controls)

        qs = self.filterable_page.get_filterable_queryset()
        self.assertEqual(qs.count(), 2)
        self.assertEqual(qs[0].pk, self.sibling_page.pk)
Exemple #14
0
    def test_append_date_to_duplicate_slug(self):
        events_page = BrowseFilterablePage(title='Events',
                                           slug='events',
                                           content='Events',
                                           live=True)
        self.root_page.add_child(instance=events_page)
        archive_page = EventArchivePage(title='Archive',
                                        slug='archive',
                                        content='archive',
                                        live=True)
        events_page.add_child(instance=archive_page)
        already_archived_event_page = EventPage(
            title='Already archived event page',
            slug='event',
            start_dt=datetime.datetime(2020, 1, 1, tzinfo=pytz.UTC))
        archive_page.add_child(instance=already_archived_event_page)
        same_slug_event_page = EventPage(title='To-be-archived event page',
                                         slug='event',
                                         start_dt=datetime.datetime(
                                             2020, 1, 2, tzinfo=pytz.UTC))
        events_page.add_child(instance=same_slug_event_page)

        self.call_command()
        same_slug_event_page.refresh_from_db()

        self.assertEqual(same_slug_event_page.slug, 'event-2020-01-02')
class TestSocialSharingImage(TestCase):
    def setUp(self):
        self.page = BrowseFilterablePage(
            title='Browse Filterable Page',
            slug='browse-filterable-page',
        )
        save_new_page(self.page)

    def test_validation_error_thrown_if_width_height_too_large(self):
        self.page.social_sharing_image = baker.prepare(CFGOVImage,
                                                       width=5000,
                                                       height=5000)
        with self.assertRaisesRegexp(
                ValidationError,
                'Social sharing image must be less than 4096w x 4096h'):
            self.page.save()

    def test_validation_error_thrown_if_width_too_large(self):
        self.page.social_sharing_image = baker.prepare(CFGOVImage,
                                                       width=4097,
                                                       height=1500)
        with self.assertRaisesRegexp(
                ValidationError,
                'Social sharing image must be less than 4096w x 4096h'):
            self.page.save()

    def test_validation_error_thrown_if_height_too_large(self):
        self.page.social_sharing_image = baker.prepare(CFGOVImage,
                                                       width=1500,
                                                       height=4097)
        with self.assertRaisesRegexp(
                ValidationError,
                'Social sharing image must be less than 4096w x 4096h'):
            self.page.save()
class TestSocialSharingImage(TestCase):
    def setUp(self):
        self.page = BrowseFilterablePage(
            title='Browse Filterable Page',
            slug='browse-filterable-page',
        )
        save_new_page(self.page)

    def test_validation_error_thrown_if_width_height_too_large(self):
        self.page.social_sharing_image = mommy.prepare(CFGOVImage, width=5000, height=5000)
        with self.assertRaisesRegexp(
            ValidationError,
            'Social sharing image must be less than 4096w x 4096h'
        ):
            self.page.save()

    def test_validation_error_thrown_if_width_too_large(self):
        self.page.social_sharing_image = mommy.prepare(CFGOVImage, width=4097, height=1500)
        with self.assertRaisesRegexp(
            ValidationError,
            'Social sharing image must be less than 4096w x 4096h'
        ):
            self.page.save()

    def test_validation_error_thrown_if_height_too_large(self):
        self.page.social_sharing_image = mommy.prepare(CFGOVImage, width=1500, height=4097)
        with self.assertRaisesRegexp(
            ValidationError,
            'Social sharing image must be less than 4096w x 4096h'
        ):
            self.page.save()
class FilterableRoutesTestCase(TestCase):
    def setUp(self):
        self.filterable_page = BrowseFilterablePage(title="Blog", slug="test")
        self.root = Site.objects.get(is_default_site=True).root_page
        self.root.add_child(instance=self.filterable_page)

        self.page = BlogPage(
            title="Test",
            slug="one",
            live=True,
        )
        self.filterable_page.add_child(instance=self.page)

    def test_index_route(self):
        response = self.client.get("/test/")
        self.assertEqual(
            response.context_data["filter_data"]["page_set"][0].title, "Test")

    def test_feed_route(self):
        response = self.client.get("/test/feed/")
        self.assertEqual(response["content-type"],
                         "application/rss+xml; charset=utf-8")
Exemple #18
0
    def test_current_future_events_stay_in_place(self):
        events_page = BrowseFilterablePage(title='Events',
                                           slug='events',
                                           content='Events',
                                           live=True)
        self.root_page.add_child(instance=events_page)
        archive_page = EventArchivePage(title='Archive',
                                        slug='archive',
                                        content='archive',
                                        live=True)
        events_page.add_child(instance=archive_page)
        current_event_page = EventPage(
            title='Current event',
            start_dt=datetime.datetime(2020, 2, 1, tzinfo=pytz.UTC),
            end_dt=datetime.datetime(2020, 2, 2, tzinfo=pytz.UTC))
        events_page.add_child(instance=current_event_page)
        future_event_page = EventPage(title='Future event',
                                      start_dt=datetime.datetime(
                                          2020, 3, 1, tzinfo=pytz.UTC))
        events_page.add_child(instance=future_event_page)

        self.assertIn('No past events found to be archived.',
                      self.call_command())
class FilterableListContextTestCase(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.filterable_page = BrowseFilterablePage(title="Blog", slug="test")
        self.root = Site.objects.get(is_default_site=True).root_page
        self.root.add_child(instance=self.filterable_page)
        self.page = BlogPage(title="Child test page", live=True)
        self.archived_page = BlogPage(title="Archive test page",
                                      live=True,
                                      is_archived='yes')
        self.filterable_page.add_child(instance=self.page)
        self.filterable_page.add_child(instance=self.archived_page)

    def test_get_context_has_archived_posts(self):
        context = self.filterable_page.get_context(
            request=self.factory.get("/test/"))
        self.assertTrue(context['has_archived_posts'])

    @override_settings(
        FLAGS={"HIDE_ARCHIVE_FILTER_OPTIONS": [("boolean", True)]})
    def test_get_context_has_archived_posts_with_hide_archive_flag_on(self):
        context = self.filterable_page.get_context(
            request=self.factory.get("/test/"))
        self.assertFalse(context['has_archived_posts'])
Exemple #20
0
def run():
    print 'Running script \'scripts.initial_test_data\' ...'

    admin_user = User.objects.filter(username='******')
    if not admin_user:
        admin_user = User(username='******',
                          password=make_password(
                              os.environ.get('WAGTAIL_ADMIN_PW')),
                          is_superuser=True,
                          is_active=True,
                          is_staff=True)
        admin_user.save()
    else:
        admin_user = admin_user[0]

    # # Creates a new site root `CFGov`
    site_root = HomePage.objects.filter(title='CFGOV')
    if not site_root:
        root = Page.objects.first()
        site_root = HomePage(title='CFGOV',
                             slug='home',
                             depth=2,
                             owner=admin_user)
        site_root.live = True
        root.add_child(instance=site_root)
        latest = site_root.save_revision(user=admin_user,
                                         submitted_for_moderation=False)
        latest.save()
    else:
        site_root = site_root[0]

    # Setting new site root
    site = Site.objects.first()
    if site.root_page_id != site_root.id:
        site.port = 8000
        site.root_page_id = site_root.id
        site.save()
        content_site = Site(hostname='content.localhost',
                            port=8000,
                            root_page_id=site_root.id)
        content_site.save()

    def publish_page(child=None, root=site_root):
        try:
            root.add_child(instance=child)
        except NodeAlreadySaved:
            pass
        revision = child.save_revision(
            user=admin_user,
            submitted_for_moderation=False,
        )
        revision.publish()

    # Create each Page Type
    contact = Contact.objects.filter(heading='Test User')
    if not contact:
        contact = Contact(heading='Test User')
    else:
        contact = contact[0]
    contact.contact_info = StreamValue(
        contact.contact_info.stream_block, [{
            "type": "email",
            "value": {
                "emails": [{
                    "url": "/",
                    "text": "*****@*****.**"
                }]
            }
        }, {
            "type": "phone",
            "value": {
                "phones": [{
                    "tty": "",
                    "number": "1234567890",
                    "vanity": ""
                }],
                "fax": True
            }
        }, {
            "type": "address",
            "value": {
                "city": "Washington",
                "title": "",
                "label": "Address",
                "state": "DC",
                "street": "123 abc street",
                "zip_code": "20012"
            }
        }], True)
    contact.save()

    lap = LandingPage.objects.filter(title='Landing Page')
    if not lap:
        lap = LandingPage(title='Landing Page',
                          slug='landing-page',
                          owner=admin_user)
    else:
        lap = lap[0]
    lap.content = StreamValue(lap.content.stream_block, [{
        "type": "image_text_25_75_group",
        "value": {
            "heading":
            "Image 25 75 Group",
            "image_texts": [{
                "body": "",
                "has_rule": False,
                "image": {
                    "alt": "",
                    "upload": 84
                },
                "heading": "",
                "links": [{
                    "url": "/",
                    "text": "test"
                }]
            }]
        }
    }, {
        "type": "image_text_50_50_group",
        "value": {
            "heading":
            "Image 50 50 Group",
            "image_texts": [{
                "body": "",
                "links": [{
                    "url": "/",
                    "text": "test"
                }],
                "image": {
                    "alt": "",
                    "upload": 84
                },
                "is_widescreen": False,
                "is_button": False,
                "heading": ""
            }]
        }
    }, {
        "type": "half_width_link_blob_group",
        "value": {
            "link_blobs": [{
                "body": "",
                "heading": "",
                "links": [{
                    "url": "/",
                    "text": "test"
                }]
            }],
            "heading":
            "Half Width Link Blob Group"
        }
    }], True)
    publish_page(lap)

    sp = SublandingPage.objects.filter(title='Sublanding Page')
    if not sp:
        sp = SublandingPage(title='Sublanding Page',
                            slug='sublanding-page',
                            owner=admin_user)
    else:
        sp = sp[0]
    sp.content = StreamValue(sp.content.stream_block, [{
        "type": "contact",
        "value": {
            "body": "",
            "header": "Contact",
            "contact": contact.id
        }
    }], True)
    sp.sidefoot = StreamValue(sp.sidefoot.stream_block, [{
        "type": "email_signup",
        "value": {
            "text":
            "",
            "gd_code":
            "",
            "heading":
            "Email Sign Up",
            "form_field": [{
                "info": "",
                "type": "",
                "required": False,
                "label": "Email Sign up",
                "btn_text": "",
                "placeholder": ""
            }]
        }
    }, {
        "type": "rss_feed",
        "value": "blog_feed"
    }], True)

    publish_page(sp)

    bp = BrowsePage.objects.filter(title='Browse Page')
    if not bp:
        bp = BrowsePage(title='Browse Page',
                        slug='browse-page',
                        owner=admin_user)
    else:
        bp = bp[0]
    bp.header = StreamValue(bp.header.stream_block, [{
        'type': 'featured_content',
        'value': {
            'body': "<p>this is a featured content body</p>"
        }
    }], True)
    bp.content = StreamValue(bp.content.stream_block, [{
        'type': 'expandable',
        'value': {
            'label': 'this is an expandable'
        }
    }], True)
    publish_page(bp)

    # Filterable Pages
    bfp = BrowseFilterablePage.objects.filter(title='Browse Filterable Page')
    if not bfp:
        bfp = BrowseFilterablePage(title='Browse Filterable Page',
                                   slug='browse-filterable-page',
                                   owner=admin_user)
    else:
        bfp = bfp[0]
    bfp.header = StreamValue(bfp.header.stream_block, [{
        'type': 'text_introduction',
        'value': {
            'intro': 'this is an intro'
        }
    }], True)
    publish_page(bfp)

    sfp = SublandingFilterablePage.objects.filter(
        title='Sublanding Filterable Page')
    if not sfp:
        sfp = SublandingFilterablePage(title='Sublanding Filterable Page',
                                       slug='sublanding-filterable-page',
                                       owner=admin_user)
    else:
        sfp = sfp[0]
    sfp.header = StreamValue(sfp.header.stream_block, [{
        'type': 'hero',
        'value': {
            'heading': "this is a hero heading"
        }
    }], True)
    publish_page(sfp)

    eap = EventArchivePage.objects.filter(title='Event Archive Page')
    if not eap:
        eap = EventArchivePage(title='Event Archive Page',
                               slug='event-archive-page',
                               owner=admin_user)
    else:
        eap = eap[0]
    publish_page(eap)

    nlp = NewsroomLandingPage.objects.filter(title='Newsroom Landing Page')
    if not nlp:
        nlp = NewsroomLandingPage(title='Newsroom Landing Page',
                                  slug='newsroom-landing-page',
                                  owner=admin_user)
    else:
        nlp = nlp[0]
    publish_page(nlp)

    # Filter Pages
    if not EventPage.objects.filter(title='Event Page'):
        ep = EventPage(title='Event Page', slug='event-page', owner=admin_user)
        publish_page(ep, bfp)

    ddp = DocumentDetailPage.objects.filter(title='Document Detail Page')
    if not ddp:
        ddp = DocumentDetailPage(title='Document Detail Page',
                                 slug='document-detail-page',
                                 owner=admin_user)
    else:
        ddp = ddp[0]
    ddp.sidefoot = StreamValue(ddp.sidefoot.stream_block, [{
        'type': 'related_metadata',
        'value': {
            'content': [{
                'type': 'text',
                'value': {
                    'heading': 'this is a related metadata heading'
                }
            }]
        }
    }], True)
    publish_page(ddp, bfp)

    lp = LearnPage.objects.filter(title='Learn Page')
    if not lp:
        lp = LearnPage(title='Learn Page', slug='learn-page', owner=admin_user)
    else:
        lp = lp[0]
    lp.content = StreamValue(lp.content.stream_block, [{
        'type':
        'full_width_text',
        'value': [{
            'type': 'quote',
            'value': {
                'body': 'this is a quote',
                'citation': 'a citation'
            }
        }]
    }, {
        'type': 'call_to_action',
        'value': {
            'paragraph_text': 'this is a call to action'
        }
    }], True)
    publish_page(lp, bfp)

    if not ActivityLogPage.objects.filter(title='Activity Log Page'):
        alp = ActivityLogPage(title='Activity Log Page',
                              slug='activity-log-page',
                              owner=admin_user)
        publish_page(alp)

    if not NewsroomPage.objects.filter(title='Newsroom Page'):
        np = NewsroomPage(title='Newsroom Page',
                          slug='newsroom-page',
                          owner=admin_user)
        publish_page(np, nlp)

    if not LegacyNewsroomPage.objects.filter(title='Legacy Newsroom Page'):
        lnp = LegacyNewsroomPage(title='Legacy Newsroom Page',
                                 slug='legacy-newsroom-page',
                                 owner=admin_user)
        publish_page(lnp, nlp)

    if not BlogPage.objects.filter(title='Blog Page'):
        bp = BlogPage(title='Blog Page', slug='blog-page', owner=admin_user)
        publish_page(bp, sfp)

    if not LegacyBlogPage.objects.filter(title='Legacy Blog Page'):
        lbp = LegacyBlogPage(title='Legacy Blog Page',
                             slug='legacy-blog-page',
                             owner=admin_user)
        publish_page(lbp, sfp)

    # Demo Page
    dp = DemoPage.objects.filter(title='Demo Page')
    if not dp:
        dp = DemoPage(title='Demo Page', slug='demo-page', owner=admin_user)
    else:
        dp = dp[0]
    dp.sidefoot = StreamValue(dp.sidefoot.stream_block, [{
        'type': 'related_links',
        'value': {
            'links': [{
                'url': '/url',
                'text': 'this is a related link'
            }]
        }
    }], True)
    publish_page(dp)

    # Create and configure pages for testing page states
    draft = DemoPage.objects.filter(slug='draft-page')
    if not draft:
        draft = DemoPage(title='Draft Page',
                         slug='draft-page',
                         owner=admin_user,
                         live=False,
                         shared=False)
        site_root.add_child(instance=draft)
    else:
        draft = draft[0]
    draft.save_revision(user=admin_user)

    shared = DemoPage.objects.filter(slug='shared-page')
    if not shared:
        shared = DemoPage(title='Shared Page',
                          slug='shared-page',
                          owner=admin_user,
                          live=False,
                          shared=True)
        site_root.add_child(instance=shared)
    else:
        shared = shared[0]
    shared.save_revision(user=admin_user)

    shared_draft = DemoPage.objects.filter(slug='shared-draft-page')
    if not shared_draft:
        shared_draft = DemoPage(title='Shared Page',
                                slug='shared-draft-page',
                                owner=admin_user,
                                live=False,
                                shared=True)
        site_root.add_child(instance=shared_draft)
    else:
        shared_draft = shared_draft[0]
    shared_draft.save_revision(user=admin_user)
    shared_draft.title = 'Shared Draft Page'
    shared_draft.save()
    shared_draft.save_revision(user=admin_user)

    live = DemoPage.objects.filter(slug='live-page')
    if not live:
        live = DemoPage(title='Live Page',
                        slug='live-page',
                        owner=admin_user,
                        live=True,
                        shared=True)
    else:
        live = live[0]
    publish_page(live)

    livedraft = DemoPage.objects.filter(slug='live-draft-page')
    if not livedraft:
        livedraft = DemoPage(title='Live Draft Page',
                             slug='live-draft-page',
                             owner=admin_user,
                             live=True,
                             shared=True)
    else:
        livedraft = livedraft[0]
    publish_page(livedraft)
    livedraft.live = False
    livedraft.shared = False
    livedraft.title = 'Live Page'
    livedraft.save_revision(user=admin_user)
class InactiveUsersTestCase(TestCase):

    def setUp(self):
        self.filterable_page = BrowseFilterablePage(
            title="Blog",
            slug="test",
            live=True
        )
        self.url_path = self.filterable_page.url

        self.root = Site.objects.get(is_default_site=True).root_page
        self.root.add_child(instance=self.filterable_page)

        self.page1 = BlogPage(
            title="Page published in 2017",
            live=True,
            first_published_at=datetime.datetime(2017, 1, 1, tzinfo=pytz.UTC),
            last_published_at=datetime.datetime(2020, 1, 1, tzinfo=pytz.UTC)
        )
        self.page2 = BlogPage(
            title="Page published in 2019",
            live=True,
            first_published_at=datetime.datetime(2018, 1, 1, tzinfo=pytz.UTC),
            last_published_at=datetime.datetime(2018, 1, 1, tzinfo=pytz.UTC)
        )
        self.page3 = BlogPage(
            title="Page published in 2019",
            live=True,
            first_published_at=datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC),
            last_published_at=datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC)
        )
        self.page4 = BlogPage(
            title="Page in archived in 2020",
            live=True,
            is_archived="yes",
            first_published_at=datetime.datetime(2020, 1, 1, tzinfo=pytz.UTC),
            last_published_at=datetime.datetime(2020, 1, 1, tzinfo=pytz.UTC)
        )

        self.filterable_page.add_child(instance=self.page1)
        self.filterable_page.add_child(instance=self.page2)
        self.filterable_page.add_child(instance=self.page3)
        self.filterable_page.add_child(instance=self.page4)

        self.stdout = StringIO()

    def test_archive_bad_path_errors(self):
        with self.assertRaises(CommandError):
            call_command("archive_pages", "foo/bar")

    @freeze_time("2020-1-1")
    def test_archive_default_opts(self):
        call_command(
            "archive_pages",
            self.filterable_page.url,
            stdout=self.stdout,
        )

        self.page1.refresh_from_db()
        self.page2.refresh_from_db()
        self.page3.refresh_from_db()
        self.page4.refresh_from_db()

        self.assertTrue(self.page1.archived)
        self.assertFalse(self.page2.archived)
        self.assertFalse(self.page3.archived)
        self.assertTrue(self.page4.archived)

    @freeze_time("2019-2-3")
    def test_archive_months_days(self):
        call_command(
            "archive_pages",
            self.filterable_page.url,
            years=1,
            months=1,
            days=1,
            stdout=self.stdout,
        )

        self.page1.refresh_from_db()
        self.page2.refresh_from_db()
        self.page3.refresh_from_db()
        self.page4.refresh_from_db()

        self.assertTrue(self.page1.archived)
        self.assertTrue(self.page2.archived)
        self.assertFalse(self.page3.archived)
        self.assertTrue(self.page4.archived)

    @freeze_time("2020-1-2")
    def test_archive_last_publish_date(self):
        call_command(
            "archive_pages",
            self.filterable_page.url,
            years=1,
            by_published_date="last",
            stdout=self.stdout,
        )

        self.page1.refresh_from_db()
        self.page2.refresh_from_db()
        self.page3.refresh_from_db()
        self.page4.refresh_from_db()

        self.assertFalse(self.page1.archived)
        self.assertTrue(self.page2.archived)
        self.assertTrue(self.page3.archived)
        self.assertTrue(self.page4.archived)
 def setUp(self):
     self.page = BrowseFilterablePage(
         title='Browse Filterable Page',
         slug='browse-filterable-page',
     )
     save_new_page(self.page)
def run():
    print 'Running script \'scripts.initial_test_data\' ...'

    admin_user = User.objects.filter(username='******')
    if not admin_user:
        admin_user = User(username='******',
                          password=make_password(os.environ.get('WAGTAIL_ADMIN_PW')),
                          is_superuser=True, is_active=True, is_staff=True)
        admin_user.save()
    else:
        admin_user = admin_user[0]


    # # Creates a new site root `CFGov`
    site_root = HomePage.objects.filter(title='CFGOV')
    if not site_root:
        root = Page.objects.first()
        site_root = HomePage(title='CFGOV', slug='home', depth=2, owner=admin_user)
        site_root.live = True
        root.add_child(instance=site_root)
        latest = site_root.save_revision(user=admin_user, submitted_for_moderation=False)
        latest.save()
    else:
        site_root = site_root[0]

    # Setting new site root
    site = Site.objects.first()
    if site.root_page_id != site_root.id:
        site.port = 8000
        site.root_page_id = site_root.id
        site.save()
        content_site = Site(hostname='content.localhost', port=8000, root_page_id=site_root.id)
        content_site.save()

    def publish_page(child=None, root=site_root):
        try:
            root.add_child(instance=child)
        except NodeAlreadySaved:
            pass
        revision = child.save_revision(
            user=admin_user,
            submitted_for_moderation=False,
        )
        revision.publish()

    # Create snippets
    contact = Contact.objects.filter(heading='Test User')
    if not contact:
        contact = Contact(heading='Test User')
    else:
        contact = contact[0]
    contact.contact_info = StreamValue(contact.contact_info.stream_block,
        [
            atomic.contact_email,
            atomic.contact_phone,
            atomic.contact_address
        ], True)
    contact.save()

    # Create each Page Type
    lap = LandingPage.objects.filter(title='Landing Page')
    if not lap:
        lap = LandingPage(title='Landing Page', slug='landing-page', owner=admin_user)
    else:
        lap = lap[0]
    lap.content = StreamValue(lap.content.stream_block,
        [
            atomic.image_text_25_75_group,
            atomic.image_text_50_50_group,
            atomic.half_width_link_blob_group,
            atomic.well
        ], True)
    lap.sidefoot = StreamValue(lap.sidefoot.stream_block,
        [
            atomic.related_links,
            atomic.sidebar_contact(contact.id)
        ], True)
    publish_page(lap)

    sp = SublandingPage.objects.filter(title='Sublanding Page')
    if not sp:
        sp = SublandingPage(title='Sublanding Page', slug='sublanding-page', owner=admin_user)
    else:
        sp = sp[0]
    sp.content = StreamValue(sp.content.stream_block,
        [
            atomic.main_contact_info(contact.id),
            atomic.reg_comment
        ], True)
    sp.sidefoot = StreamValue(sp.sidefoot.stream_block,
        [
            atomic.email_signup,
            atomic.rss_feed
        ], True)

    publish_page(sp)

    bp = BrowsePage.objects.filter(title='Browse Page')
    if not bp:
        bp = BrowsePage(title='Browse Page', slug='browse-page', owner=admin_user)
    else:
        bp = bp[0]
    bp.header = StreamValue(bp.header.stream_block,
        [
            atomic.featured_content
        ], True)
    bp.content = StreamValue(bp.content.stream_block,
        [
            atomic.expandable,
            atomic.expandable_group
        ], True)
    publish_page(bp)

    # Filterable Pages
    bfp = BrowseFilterablePage.objects.filter(title='Browse Filterable Page')
    if not bfp:
        bfp = BrowseFilterablePage(title='Browse Filterable Page', slug='browse-filterable-page', owner=admin_user)
    else:
        bfp = bfp[0]
    bfp.header = StreamValue(bfp.header.stream_block, [atomic.text_introduction], True)
    publish_page(bfp)

    sfp = SublandingFilterablePage.objects.filter(title='Sublanding Filterable Page')
    if not sfp:
        sfp = SublandingFilterablePage(title='Sublanding Filterable Page', slug='sublanding-filterable-page',
                                       owner=admin_user)
    else:
        sfp = sfp[0]
    sfp.header = StreamValue(sfp.header.stream_block, [atomic.hero], True)
    publish_page(sfp)

    eap = EventArchivePage.objects.filter(title='Event Archive Page')
    if not eap:
        eap = EventArchivePage(title='Event Archive Page', slug='event-archive-page', owner=admin_user)
    else:
        eap = eap[0]
    publish_page(eap)

    nlp = NewsroomLandingPage.objects.filter(title='Newsroom Landing Page')
    if not nlp:
        nlp = NewsroomLandingPage(title='Newsroom Landing Page', slug='newsroom-landing-page', owner=admin_user)
    else:
        nlp = nlp[0]
    publish_page(nlp)

    alp = ActivityLogPage.objects.filter(title='Activity Log Page')
    if not alp:
        alp = ActivityLogPage(title='Activity Log Page', slug='activity-log-page', owner=admin_user)
    else:
        alp = alp[0]
    publish_page(alp)

    # Filter Pages
    if not EventPage.objects.filter(title='Event Page'):
        ep = EventPage(title='Event Page', slug='event-page', owner=admin_user)
        publish_page(ep, bfp)

    ddp = DocumentDetailPage.objects.filter(title='Document Detail Page')
    if not ddp:
        ddp = DocumentDetailPage(title='Document Detail Page', slug='document-detail-page', owner=admin_user)
    else:
        ddp = ddp[0]
    ddp.sidefoot = StreamValue(ddp.sidefoot.stream_block, [atomic.related_metadata], True)
    publish_page(ddp, bfp)

    lp = LearnPage.objects.filter(title='Learn Page')
    if not lp:
        lp = LearnPage(title='Learn Page', slug='learn-page', owner=admin_user)
    else:
        lp = lp[0]
    lp.header = StreamValue(lp.header.stream_block, [atomic.item_introduction], True)
    lp.content = StreamValue(lp.content.stream_block,
        [
            atomic.full_width_text,
            atomic.call_to_action,
            atomic.table
        ], True)
    publish_page(lp, bfp)

    if not NewsroomPage.objects.filter(title='Newsroom Page'):
        np = NewsroomPage(title='Newsroom Page', slug='newsroom-page', owner=admin_user)
        publish_page(np, nlp)

    if not LegacyNewsroomPage.objects.filter(title='Legacy Newsroom Page'):
        lnp = LegacyNewsroomPage(title='Legacy Newsroom Page', slug='legacy-newsroom-page', owner=admin_user)
        publish_page(lnp, nlp)

    if not BlogPage.objects.filter(title='Blog Page'):
        bp = BlogPage(title='Blog Page', slug='blog-page', owner=admin_user)
        publish_page(bp, sfp)

    if not LegacyBlogPage.objects.filter(title='Legacy Blog Page'):
        lbp = LegacyBlogPage(title='Legacy Blog Page', slug='legacy-blog-page', owner=admin_user)
        publish_page(lbp, sfp)


    # Create and configure pages for testing page states
    draft = LandingPage.objects.filter(slug='draft-page')
    if not draft:
        draft = LandingPage(title='Draft Page', slug='draft-page', owner=admin_user, live=False, shared=False)
        site_root.add_child(instance=draft)
    else:
        draft = draft[0]
    draft.save_revision(user=admin_user)

    shared = LandingPage.objects.filter(slug='shared-page')
    if not shared:
        shared = LandingPage(title='Shared Page', slug='shared-page', owner=admin_user, live=False, shared=True)
        site_root.add_child(instance=shared)
    else:
        shared = shared[0]
    shared.save_revision(user=admin_user)

    shared_draft = LandingPage.objects.filter(slug='shared-draft-page')
    if not shared_draft:
        shared_draft = LandingPage(title='Shared Page', slug='shared-draft-page', owner=admin_user, live=False, shared=True)
        site_root.add_child(instance=shared_draft)
    else:
        shared_draft = shared_draft[0]
    shared_draft.save_revision(user=admin_user)
    shared_draft.title = 'Shared Draft Page'
    shared_draft.shared = False
    shared_draft.save()
    shared_draft.save_revision(user=admin_user)

    live = LandingPage.objects.filter(slug='live-page')
    if not live:
        live = LandingPage(title='Live Page', slug='live-page', owner=admin_user, live=True, shared=True)
    else:
        live = live[0]
    publish_page(live)

    livedraft = LandingPage.objects.filter(slug='live-draft-page')
    if not livedraft:
        livedraft = LandingPage(title='Live Draft Page', slug='live-draft-page', owner=admin_user, live=True, shared=True)
    else:
        livedraft = livedraft[0]
    publish_page(livedraft)
    livedraft.live = False
    livedraft.shared = False
    livedraft.title = 'Live Page'
    livedraft.save_revision(user=admin_user)
 def setUp(self):
     self.page = BrowseFilterablePage(
         title='Browse Filterable Page',
         slug='browse-filterable-page',
     )
     save_new_page(self.page)