diff --git a/CHANGELOG.md b/CHANGELOG.md index bbe814f5..a6f2f2f9 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 - CMS-1832 - Update django - CMS-1753 - Remove slug links from markdown fields 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