From 0973ee9b78bcf1001e04ee69b337ac626f8e78e0 Mon Sep 17 00:00:00 2001 From: richtier Date: Tue, 27 Aug 2019 13:23:44 +0100 Subject: [PATCH] Management command that lists pages containing slug-base hyperlinking --- CHANGELOG.md | 3 ++ core/management/commands/list_slug_links.py | 19 +++++++++++ core/validators.py | 3 -- tests/core/test_list_slug_links.py | 36 +++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 core/management/commands/list_slug_links.py create mode 100644 tests/core/test_list_slug_links.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ed964099..c92b69a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Pre-release +### Hotfix +- no ticket - Management command that lists pages containing slug-base hyperlinking + ### Implemented enhancements - No ticket - Make teaser field optional in international articles - No ticket - Add featured industries to Invest home page diff --git a/core/management/commands/list_slug_links.py b/core/management/commands/list_slug_links.py new file mode 100644 index 00000000..0dc6b499 --- /dev/null +++ b/core/management/commands/list_slug_links.py @@ -0,0 +1,19 @@ +from django.core.management import BaseCommand +from django.core.management.base import no_translations +from wagtail.core.models import Page + +from django.forms.models import model_to_dict +from django.urls import reverse + + +class Command(BaseCommand): + help = 'List pages that contain hyperlinks using slugs to identify the target page' + + @no_translations + def handle(self, *args, **options): + for instance in Page.objects.all().specific(): + for value in model_to_dict(instance).values(): + if isinstance(value, str) and 'slug:' in value: + url = reverse('wagtailadmin_pages:edit', args=(instance.pk,)) + self.stdout.write(self.style.WARNING(url)) + break diff --git a/core/validators.py b/core/validators.py index 863d724e..faafd778 100644 --- a/core/validators.py +++ b/core/validators.py @@ -5,9 +5,6 @@ INCORRECT_SLUG = 'Slug is incorrect.' -ABSOLUTE_INTERNAL_HYPERKINK = ( - 'Please use a slug hyperlink. e.g., [%(text)s](slug:the-target-page-slug)' -) def slug_hyperlinks(value): diff --git a/tests/core/test_list_slug_links.py b/tests/core/test_list_slug_links.py new file mode 100644 index 00000000..166a8d7a --- /dev/null +++ b/tests/core/test_list_slug_links.py @@ -0,0 +1,36 @@ +from io import StringIO + +import pytest + +from django.core.management import call_command +from django.urls import reverse + +from tests.export_readiness.factories import ArticlePageFactory, CountryGuidePageFactory +from tests.invest.factories import InfoPageFactory + + +@pytest.mark.django_db +def test_list_slug_links(root_page): + stdout = StringIO() + + guide = CountryGuidePageFactory.create( + slug='bar', + parent=root_page, + section_one_body=f'[things](pk:{root_page.pk}) are nice', + ) + article = ArticlePageFactory.create( + parent=root_page, + article_body_text=f'[things](slug:{guide.slug}) are good', + ) + info = InfoPageFactory.create( + parent=root_page, + content_en_gb=f'[things](slug:{guide.slug}) are great', + ) + + call_command('list_slug_links', stdout=stdout) + stdout.seek(0) + urls = stdout.read() + + assert reverse('wagtailadmin_pages:edit', args=(article.pk,)) in urls + assert reverse('wagtailadmin_pages:edit', args=(guide.pk,)) not in urls + assert reverse('wagtailadmin_pages:edit', args=(info.pk,)) in urls