misc: add title field to text cells (#42968)
This commit is contained in:
parent
d31cf3a1b3
commit
afde19c1fe
|
@ -0,0 +1,64 @@
|
|||
# Generated by Django 2.2.26 on 2022-08-10 16:32
|
||||
|
||||
from io import StringIO
|
||||
|
||||
import lxml
|
||||
import lxml.html
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def forward_title_migration(apps, schema_editor):
|
||||
TextCell = apps.get_model('data', 'TextCell')
|
||||
for cell in TextCell.objects.all():
|
||||
if not cell.text:
|
||||
continue
|
||||
|
||||
parser = lxml.etree.HTMLParser()
|
||||
html_document = lxml.etree.parse(StringIO(cell.text), parser)
|
||||
root = html_document.getroot()
|
||||
|
||||
if root is None:
|
||||
continue
|
||||
|
||||
title_nodes = root.xpath('/html/body/h2[1]')
|
||||
if not title_nodes:
|
||||
continue
|
||||
|
||||
assert len(title_nodes) == 1
|
||||
title_node = title_nodes[0]
|
||||
if len(title_node) != 0 or len(title_node.attrib) != 0:
|
||||
continue
|
||||
|
||||
body = title_node.getparent()
|
||||
title = title_node.text or ''
|
||||
text = title_node.tail or ''
|
||||
text += body.text or ''
|
||||
text += ''.join([lxml.html.tostring(child).decode() for child in body[1:]])
|
||||
cell.title = title
|
||||
cell.text = text
|
||||
print(title)
|
||||
cell.save()
|
||||
|
||||
|
||||
def reverse_title_migration(apps, schema_editor):
|
||||
TextCell = apps.get_model('data', 'TextCell')
|
||||
for cell in TextCell.objects.all():
|
||||
if cell.title:
|
||||
cell.text = f'<h2>{cell.title}</h2>{cell.text}'
|
||||
cell.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('data', '0057_pagesnapshot_label'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='textcell',
|
||||
name='title',
|
||||
field=models.CharField(blank=True, max_length=150, null=True, verbose_name='Title'),
|
||||
),
|
||||
migrations.RunPython(forward_title_migration, reverse_title_migration),
|
||||
]
|
|
@ -1510,6 +1510,7 @@ class CellBase(models.Model, metaclass=CellMeta):
|
|||
|
||||
@register_cell_class
|
||||
class TextCell(CellBase):
|
||||
title = models.CharField(_('Title'), max_length=150, blank=True, null=True)
|
||||
text = RichTextField(_('Text'), blank=True, null=True)
|
||||
|
||||
default_template_name = 'combo/text-cell.html'
|
||||
|
@ -1568,7 +1569,9 @@ class TextCell(CellBase):
|
|||
|
||||
text = re.sub(r'src="(.*?)"', sub_src, text)
|
||||
text = re.sub(r'href="(.*?)"', sub_href, text)
|
||||
extra_context['text'] = mark_safe(text)
|
||||
extra_context["text"] = mark_safe(text)
|
||||
if self.title:
|
||||
extra_context["title"] = mark_safe(self.title)
|
||||
return extra_context
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
{% block cell-content %}
|
||||
{% include "combo/asset_picture_fragment.html" %}
|
||||
{% if title %}
|
||||
<h2 class="cell--title text-cell--title">{{ title }}</h2>
|
||||
{% endif %}
|
||||
{{text}}
|
||||
{% endblock %}
|
||||
|
|
|
@ -122,6 +122,25 @@ def test_text_cell_variadic_url():
|
|||
assert 'href="/plop"' in cell.render(ctx)
|
||||
|
||||
|
||||
def test_text_cell_title():
|
||||
page = Page()
|
||||
page.save()
|
||||
|
||||
cell = TextCell(page=page, order=0)
|
||||
cell.text = '<p>body</p>'
|
||||
cell.title = 'Cell Title'
|
||||
cell.save()
|
||||
ctx = {}
|
||||
assert re.search(r'<h2[^>]*>Cell Title</h2>', cell.render(ctx))
|
||||
|
||||
cell = TextCell(page=page, order=0)
|
||||
cell.text = '<p>body</p>'
|
||||
cell.title = None
|
||||
cell.save()
|
||||
ctx = {}
|
||||
assert not re.search(r'<h2[^>]*>.*</h2>', cell.render(ctx))
|
||||
|
||||
|
||||
def test_link_cell():
|
||||
page = Page(title='example page', slug='example-page')
|
||||
page.save()
|
||||
|
|
Loading…
Reference in New Issue