Skip to content

Commit

Permalink
Remove local footnote custom rendering
Browse files Browse the repository at this point in the history
This updates wagtail-footnotes to 0.12 and removes our custom `RichTextBlockWithFootnotes` subclass of wagtail-footnotes own `RichTextBlockWithFootnotes`, tests, and switches to using the upstream `RichTextBlockWithFootnotes`. 0.12 includes our changes in torchbox/wagtail-footnotes#70, which were based on this code being removed.

This change updates `RichTextBlockWithFootnotes` in-place in old migrations. Removing `v1.blocks.RichTextBlockWithFootnotes` requires these migrations to be updated, and because there are no schema changes, new migrations are not required.
  • Loading branch information
willbarton committed Aug 14, 2024
1 parent 3f2866b commit 6095a35
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 152 deletions.
4 changes: 2 additions & 2 deletions cfgov/ask_cfpb/migrations/0008_add_footnotes.py

Large diffs are not rendered by default.

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cfgov/form_explainer/migrations/0010_add_footnotes.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cfgov/paying_for_college/migrations/0012_add_footnotes.py

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions cfgov/regulations3k/migrations/0011_add_footnotes.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cfgov/v1/atomic_elements/organisms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from wagtail.models import Page
from wagtail.snippets.blocks import SnippetChooserBlock

from wagtail_footnotes.blocks import RichTextBlockWithFootnotes
from wagtailmedia.blocks import AbstractMediaChooserBlock

from v1 import blocks as v1_blocks
Expand All @@ -24,7 +25,6 @@
ContactUsTable,
Table,
)
from v1.blocks import RichTextBlockWithFootnotes
from v1.util import ref


Expand Down
4 changes: 3 additions & 1 deletion cfgov/v1/atomic_elements/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from wagtail.documents.blocks import DocumentChooserBlock
from wagtail.telepath import register

from v1.blocks import HeadingBlock, RichTextBlockWithFootnotes
from wagtail_footnotes.blocks import RichTextBlockWithFootnotes

from v1.blocks import HeadingBlock


class ContactUsRow(blocks.StructBlock):
Expand Down
57 changes: 0 additions & 57 deletions cfgov/v1/blocks.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.template.loader import get_template
from django.utils.safestring import mark_safe
from django.utils.text import slugify

from wagtail import blocks
from wagtail.models import Page
from wagtail.snippets.blocks import SnippetChooserBlock

from wagtail_footnotes.blocks import (
FIND_FOOTNOTE_TAG,
)
from wagtail_footnotes.blocks import (
RichTextBlockWithFootnotes as WagtailFootnotesRichTextBlockWithFootnotes,
)

from v1.util.util import get_unique_id


Expand Down Expand Up @@ -96,49 +85,3 @@ class Meta:

class Media:
js = ["email-signup.js"]


class RichTextBlockWithFootnotes(WagtailFootnotesRichTextBlockWithFootnotes):
def render_footnote_tag(self, index):
template = get_template(settings.WAGTAIL_FOOTNOTES_REFERENCE_TEMPLATE)
return template.render({"index": index})

def replace_footnote_tags(self, value, html, context=None):
# This is a wholesale copy of the replace_footnote_tags() method in
# wagtail-footnotes's RichTextBlockWithFootnotes. It modifies the
# embedded replace_tag() function to call our own
# render_footnote_tag() method. This is a change that should be
# contributed back upstream to allow straight-forward modification of
# footnote link rendering.
#
# There is an alternative implementation proposed in a PR in 2022:
# https://github.com/torchbox/wagtail-footnotes/pull/27
# But I think I prefer providing a new method to a new embedded func.
if context is None:
new_context = self.get_context(value)
else:
new_context = self.get_context(value, parent_context=dict(context))

if not isinstance(new_context.get("page"), Page):
return html

page = new_context["page"]
if not hasattr(page, "footnotes_list"):
page.footnotes_list = []
self.footnotes = {
str(footnote.uuid): footnote for footnote in page.footnotes.all()
}

def replace_tag(match):
try:
index = self.process_footnote(match.group(1), page)
except (KeyError, ValidationError): # pragma: no cover
return ""
else:
# This line is the only change to wagtail-footnote's
# replace_footnote_tags(), providing a separate method that
# can be overriden for rendering the footnote reference link.
return self.render_footnote_tag(index)

# note: we return safe html
return mark_safe(FIND_FOOTNOTE_TAG.sub(replace_tag, html)) # noqa: S308
23 changes: 12 additions & 11 deletions cfgov/v1/migrations/0035_add_footnotes.py

Large diffs are not rendered by default.

75 changes: 2 additions & 73 deletions cfgov/v1/tests/test_blocks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import json
import uuid
from unittest import mock

from django.test import RequestFactory, TestCase
from django.test import TestCase

from wagtail.models import Site

from wagtail_footnotes.models import Footnote

from v1.blocks import AnchorLink, RichTextBlockWithFootnotes
from v1.models import DocumentDetailPage
from v1.blocks import AnchorLink


class TestAnchorLink(TestCase):
Expand Down Expand Up @@ -50,67 +43,3 @@ def test_clean_called_with_literally_anchor(self):

assert "anchor_" in result["link_id"]
assert self.stringContainsNumbers(result["link_id"])


class RichTextBlockWithFootnotesTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.root_page = Site.objects.get(is_default_site=True).root_page

footnote_uuid = uuid.uuid4()
self.page = DocumentDetailPage(
title="Test page",
content=json.dumps(
[
{
"type": "full_width_text",
"value": [
{
"type": "content_with_footnotes",
"value": (
"<p>Test with note<footnote "
f'id="{footnote_uuid}">1</footnote></p>'
),
}
],
}
]
),
)
self.root_page.add_child(instance=self.page)
self.page.save_revision().publish()
self.footnote = Footnote.objects.create(
uuid=footnote_uuid,
page=self.page,
text="Test footnote",
)

def test_render_footnote_tag(self):
block = RichTextBlockWithFootnotes()
html = block.render_footnote_tag(2)
self.assertHTMLEqual(
html,
'<a aria-labelledby="footnotes" href="#footnote-2" '
'id="footnote-source-2"><sup>2</sup></a>',
)

def test_block_replace_footnote_tags_no_notes(self):
block = RichTextBlockWithFootnotes()
html = block.replace_footnote_tags(None, "foo")
self.assertEqual(html, "foo")

def test_block_replace_footnote_tags(self):
rich_text_with_footnotes = self.page.content.stream_block.child_blocks[
"full_width_text"
].child_blocks["content_with_footnotes"]
value = rich_text_with_footnotes.get_prep_value(
self.page.content[0].value[0].value
)
request = self.factory.get("/test-page/")
context = self.page.get_context(request)
result = rich_text_with_footnotes.render(value, context=context)
self.assertEqual(
result,
'<p>Test with note<a aria-labelledby="footnotes" '
'href="#footnote-1" id="footnote-source-1"><sup>1</sup></a></p>',
)
2 changes: 1 addition & 1 deletion requirements/libraries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ wagtail-autocomplete==0.11.0
wagtail-content-audit==0.1
wagtail_draftail_anchors==0.6.0
wagtail-flags==5.3.1
wagtail-footnotes==0.11
wagtail-footnotes==0.12
wagtail-inventory==2.6
wagtail-placeholder-images==0.1.1
wagtail-sharing==2.12.1
Expand Down

0 comments on commit 6095a35

Please sign in to comment.