Skip to content

Commit

Permalink
Merge pull request #624 from uktrade/hotfix-list-slug-linked-pages
Browse files Browse the repository at this point in the history
Management command that lists pages containing slug-base hyperlinking
  • Loading branch information
Richard Tier authored Aug 27, 2019
2 parents be35825 + 0973ee9 commit fb8fa16
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions core/management/commands/list_slug_links.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
36 changes: 36 additions & 0 deletions tests/core/test_list_slug_links.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fb8fa16

Please sign in to comment.