def test_publish_view_post(self): """ This posts to the publish view and checks that the page was published """ # Connect a mock signal handler to page_published signal mock_handler = mock.MagicMock() page_published.connect(mock_handler) # Post to the publish page response = self.client.post(self.url) # Should be redirected to explorer page self.assertEqual(response.status_code, 302) # Check that the child pages were published for child_page in self.pages_to_be_published: published_page = SimplePage.objects.get(id=child_page.id) self.assertTrue(published_page.live) self.assertIn("Hello published", published_page.content) # Check that the child pages not to be published remain for child_page in self.pages_not_to_be_published: self.assertFalse(Page.objects.get(id=child_page.id).live) # Check that the page_published signal was fired self.assertEqual(mock_handler.call_count, len(self.pages_to_be_published)) for i, child_page in enumerate(self.pages_to_be_published): mock_call = mock_handler.mock_calls[i][2] self.assertEqual(mock_call["sender"], child_page.specific_class) self.assertEqual(mock_call["instance"], child_page) self.assertIsInstance(mock_call["instance"], child_page.specific_class)
def test_approve_moderation_view(self): """ This posts to the approve moderation view and checks that the page was approved """ # Connect a mock signal handler to page_published signal mock_handler = mock.MagicMock() page_published.connect(mock_handler) # Post response = self.client.post( reverse("wagtailadmin_pages:approve_moderation", args=(self.revision.id, ))) # Check that the user was redirected to the dashboard self.assertRedirects(response, reverse("wagtailadmin_home")) page = Page.objects.get(id=self.page.id) # Page must be live self.assertTrue(page.live, "Approving moderation failed to set live=True") # Page should now have no unpublished changes self.assertFalse( page.has_unpublished_changes, "Approving moderation failed to set has_unpublished_changes=False", ) # Check that the page_published signal was fired self.assertEqual(mock_handler.call_count, 1) mock_call = mock_handler.mock_calls[0][2] self.assertEqual(mock_call["sender"], self.page.specific_class) self.assertEqual(mock_call["instance"], self.page) self.assertIsInstance(mock_call["instance"], self.page.specific_class)
def register_signal_handlers(): # Get list of models that are page types Page = apps.get_model("wagtailcore", "Page") indexed_models = [ model for model in apps.get_models() if issubclass(model, Page) ] # Loop through list and register signal handlers for each one for model in indexed_models: page_published.connect(page_published_signal_handler, sender=model) page_unpublished.connect(page_unpublished_signal_handler, sender=model)
def register_signal_handlers(): Image = get_image_model() Document = get_document_model() for model in get_page_models(): page_published.connect(purge_page_from_cache, sender=model) page_unpublished.connect(purge_page_from_cache, sender=model) post_save.connect(purge_image_from_cache, sender=Image) post_delete.connect(purge_image_from_cache, sender=Image) post_save.connect(purge_document_from_cache, sender=Document) post_delete.connect(purge_document_from_cache, sender=Document)
def test_go_live_page_will_be_published(self): # Connect a mock signal handler to page_published signal signal_fired = [False] signal_page = [None] def page_published_handler(sender, instance, **kwargs): signal_fired[0] = True signal_page[0] = instance page_published.connect(page_published_handler) page = SimplePage( title="Hello world!", slug="hello-world", content="hello", live=False, has_unpublished_changes=True, go_live_at=timezone.now() - timedelta(days=1), ) self.root_page.add_child(instance=page) page.save_revision(approved_go_live_at=timezone.now() - timedelta(days=1)) p = Page.objects.get(slug="hello-world") self.assertFalse(p.live) self.assertTrue( PageRevision.objects.filter(page=p).exclude( approved_go_live_at__isnull=True).exists()) management.call_command("publish_scheduled_pages") p = Page.objects.get(slug="hello-world") self.assertTrue(p.live) self.assertTrue(p.first_published_at) self.assertFalse(p.has_unpublished_changes) self.assertFalse( PageRevision.objects.filter(page=p).exclude( approved_go_live_at__isnull=True).exists()) # Check that the page_published signal was fired self.assertTrue(signal_fired[0]) self.assertEqual(signal_page[0], page) self.assertEqual(signal_page[0], signal_page[0].specific)