Skip to content

Commit

Permalink
Merge pull request #1067 from torchbox/feature/3217-further-info-bloc…
Browse files Browse the repository at this point in the history
…ks-display

#3217 - GUIDE PAGES | Enable multiple 'Further info' blocks display
  • Loading branch information
patrickcuagan authored Jan 17, 2025
2 parents 2b85e80 + fccfebc commit 437771d
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 17 deletions.
87 changes: 87 additions & 0 deletions rca/guides/migrations/0030_guidepage_further_information_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Generated by Django 4.2.16 on 2025-01-13 14:55

from django.db import migrations
import wagtail.fields


class Migration(migrations.Migration):

dependencies = [
("guides", "0029_accordion_streamfield_block"),
]

operations = [
migrations.AddField(
model_name="guidepage",
name="further_information_block",
field=wagtail.fields.StreamField(
[("accordion", 9)],
blank=True,
block_lookup={
0: ("wagtail.blocks.CharBlock", (), {}),
1: (
"wagtail.blocks.CharBlock",
(),
{
"help_text": "A large heading diplayed next to the block",
"required": False,
},
),
2: (
"wagtail.blocks.CharBlock",
(),
{
"help_text": "The text to display when the accordion is collapsed",
"required": False,
},
),
3: (
"wagtail.blocks.RichTextBlock",
(),
{
"features": [
"h2",
"h3",
"bold",
"italic",
"image",
"embed",
"ul",
"ol",
"link",
],
"help_text": "The content shown when the accordion expanded",
},
),
4: ("wagtail.blocks.CharBlock", (), {"required": False}),
5: ("wagtail.blocks.URLBlock", (), {"required": False}),
6: (
"wagtail.blocks.StructBlock",
[[("title", 4), ("url", 5)]],
{
"help_text": "An optional link to display below the expanded content",
"required": False,
},
),
7: (
"wagtail.blocks.StructBlock",
[
[
("heading", 1),
("preview_text", 2),
("body", 3),
("link", 6),
]
],
{},
),
8: ("wagtail.blocks.ListBlock", (7,), {}),
9: (
"wagtail.blocks.StructBlock",
[[("heading", 0), ("items", 8)]],
{},
),
},
),
),
]
63 changes: 63 additions & 0 deletions rca/guides/migrations/0031_migrate_further_information_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 4.2.16 on 2025-01-13 15:08
import json

from django.db import migrations
from wagtail.blocks import StreamValue
from django.core.serializers.json import DjangoJSONEncoder


def migrate_further_information_to_block(apps, schema_editor):
# Get the model where the fields are defined
GuidePage = apps.get_model('guides', 'GuidePage')

for page in GuidePage.objects.all():
# Create an empty list to hold the StreamField data
new_stream_data = []

# If there's a further_information_title and/or further_information, add them to the new block.
if page.further_information_title or page.further_information:

# Add accordion items from `further_information` StreamField
accordion_data = []
for item in page.further_information:
accordion_data.append({
'heading': item.value.get('heading', ''),
'preview_text': item.value.get('preview_text', ''),
'body': item.value.get('body').source if item.value.get('body') else '',
'link': {
'title': item.value.get('link').get('title', ''),
'url': item.value.get('link').get('url', ''),
},
})

# Add the heading and set the accordion items
accordion_block = {
'type': 'accordion',
'value': {
'heading': page.further_information_title,
'items': accordion_data,
},
}

# Append the new block data to the StreamField
new_stream_data.append(accordion_block)

# Assign the new StreamField value to `further_information_block`
page.further_information_block = StreamValue(
page.further_information_block.stream_block,
new_stream_data,
is_lazy=True,
)

# Save the instance
page.save()

class Migration(migrations.Migration):

dependencies = [
("guides", "0030_guidepage_further_information_block"),
]

operations = [
migrations.RunPython(migrate_further_information_to_block, reverse_code=migrations.RunPython.noop),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.16 on 2025-01-16 01:28

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("guides", "0031_migrate_further_information_data"),
]

operations = [
migrations.RemoveField(
model_name="guidepage",
name="further_information",
),
migrations.RemoveField(
model_name="guidepage",
name="further_information_title",
),
]
27 changes: 13 additions & 14 deletions rca/guides/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from wagtail.fields import StreamField
from wagtail.search import index

from rca.utils.blocks import AccordionBlockWithTitle, GuideBlock
from rca.utils.blocks import GuideBlock
from rca.utils.blocks.content import AccordionBlock
from rca.utils.models import (
BasePage,
ContactFieldsMixin,
Expand All @@ -36,11 +37,9 @@ class GuidePage(

introduction = models.CharField(max_length=500, blank=True)
body = StreamField(GuideBlock(), blank=True)
further_information_title = models.CharField(blank=True, max_length=120)
further_information = StreamField(
[("accordion_block", AccordionBlockWithTitle())],
further_information_block = StreamField(
[("accordion", AccordionBlock())],
blank=True,
verbose_name=_("Further information"),
)
related_staff_title = models.CharField(blank=True, max_length=120, default="Staff")
related_pages_title = models.CharField(blank=True, max_length=120)
Expand All @@ -64,8 +63,7 @@ class GuidePage(
),
MultiFieldPanel(
[
FieldPanel("further_information_title"),
FieldPanel("further_information"),
FieldPanel("further_information_block"),
],
heading=_("Further information"),
),
Expand Down Expand Up @@ -98,13 +96,14 @@ def anchor_nav(self):
items.append({"title": block.value, "link": f"#{slugify(block.value)}"})
if self.related_staff.first():
items.append({"title": self.related_staff_title, "link": "#staff"})
if self.further_information_title:
items.append(
{
"title": self.further_information_title,
"link": f"#{slugify(self.further_information_title)}",
}
)
if self.further_information_block:
for block in self.further_information_block:
items.append(
{
"title": block.value.get("heading"),
"link": f"#{slugify(block.value.get('heading'))}",
}
)
if self.related_pages_title:
items.append(
{
Expand Down
17 changes: 14 additions & 3 deletions rca/project_styleguide/templates/patterns/pages/guide/guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,22 @@ <h1 class="title-area__heading heading heading--display-two" id="main-content">
</div>
</section>

{% if page.further_information %}
{% if page.further_information_block %}
<section class="section bg bg--dark booking-bar-last-item">
<div class="section__row section__row--last">
{% include "patterns/organisms/accordion-block/accordion-block.html" with accordion_id='1' accordions=page.further_information course_type="short" title=page.further_information_title anchor_heading=True section_id=page.further_information_title|slugify %}
{% for block in page.further_information_block %}
<div class="accordion-block accordion-block--large-heading{% if forloop.counter > 1 %} accordion-block--no-margin{% endif %}">
<div class="grid">
<h4 class="accordion-block__heading heading heading--two anchor-heading" id="{{ block.value.heading|slugify }}">{{ block.value.heading }}</h4>
</div>

<div class="grid">
{% for item in block.value.items %}
{% include "patterns/molecules/accordion/accordion.html" with accordion=item %}
{% endfor %}
</div>
</div>
{% endfor %}
</div>
</section>
{% endif %}
Expand Down Expand Up @@ -120,7 +132,6 @@ <h1 class="title-area__heading heading heading--display-two" id="main-content">
</div>
</div>


{% if page.social_image %}
{% image page.social_image fill-1200x1200 as social_img %}
{% endif %}
Expand Down

0 comments on commit 437771d

Please sign in to comment.