Skip to content

Commit

Permalink
Add "wide" option to article TableBlock (#12843)
Browse files Browse the repository at this point in the history
* Create wide table block and template

* Add WideTableBlock to customblocks

* Use new WideTableBlock instead of TableBlock

* Convert TableBlock blockdata to WideTableBlock data

* Linting

* Help text correction

* Remove unused block import

* flake8 logic check pattern

* Additional formatting

* Apply narrow table width rule for only large and up viewports

* Remove ignore file for article_table_block

* Update migration file

---------

Co-authored-by: Daniel Miranda <[email protected]>
  • Loading branch information
robdivincenzo and danielfmiranda authored Sep 19, 2024
1 parent 4ffdb29 commit c129386
Show file tree
Hide file tree
Showing 7 changed files with 1,383 additions and 70 deletions.
1,285 changes: 1,285 additions & 0 deletions network-api/networkapi/wagtailpages/migrations/0162_alter_articlepage_body.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

from wagtail import blocks
from wagtail.contrib.table_block.blocks import TableBlock

from . import customblocks
from .customblocks.base_rich_text_options import base_rich_text_options
Expand Down Expand Up @@ -37,7 +36,7 @@
("single_quote", customblocks.SingleQuoteBlock()),
("slider", customblocks.FoundationSliderBlock()),
("spacer", customblocks.BootstrapSpacerBlock()),
("table", TableBlock(template="wagtailpages/blocks/article_table_block.html")),
("table", customblocks.WideTableBlock()),
("video", customblocks.VideoBlock()),
("advanced_table", customblocks.AdvancedTableBlock()),
]
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from .session_slider_block import SessionSliderBlock
from .single_quote_block import SingleQuoteBlock
from .spaces_block import SpacesBlock
from .table_block import WideTableBlock
from .text_only_teaser import TextOnlyTeaserBlock
from .tito_widget import TitoWidgetBlock
from .video_block import ExternalVideoBlock, VideoBlock, WagtailVideoChooserBlock
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from wagtail import blocks
from wagtail.contrib.table_block.blocks import TableBlock


class WideTableBlock(blocks.StructBlock):
"""
A custom block that contains a table and an option to render it wider than the page body content.
"""

table = TableBlock()

wide = blocks.BooleanBlock(
required=False,
default=True,
help_text="If checked, the table will render wider than the other page body content.",
)

def get_context(self, value, parent_context=None):
"""
Override get_context to add the table body logic to the template context.
If `first_row_is_table_header` is True, it slices the data to skip the first row.
Otherwise, it returns the full data set.
"""
context = super().get_context(value, parent_context=parent_context)

# Check if the first row is a header and slice the data accordingly
if value.get("table").get("first_row_is_table_header") is True:
context["table_body"] = value["table"]["data"][1:] # Skip the first row
else:
context["table_body"] = value["table"]["data"] # Return the full data set

return context

class Meta:
icon = "table"
label = "Table"
template = "wagtailpages/blocks/wide_table_block.html"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{% extends "./base_streamfield_block.html" %}
{% load wagtailcore_tags table_block_tags %}

{% block block_content %}
{% with self.table as table %}
<div class="article-table-block table-responsive{% if not self.wide %} large:tw-mx-auto large:tw-max-w-[80%]{% endif %}">
{% comment %}
Modified table template comes from "table_block/blocks/table.html"
{% endcomment %}
<table class="table">
{% if table.table_caption %}
<caption>{{ table.table_caption }}</caption>
{% endif %}
{% if table.first_row_is_table_header %}
<thead>
<tr>
{% for column in table.data.0 %}
<th>{{ column }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tbody>
{% for row in table_body %}
{% with forloop.counter0 as row_index %}
<tr>
{% for column in row %}
{% with forloop.counter0 as col_index %}
{% if table.first_col_is_header and forloop.first %}
<th scope="row" class="{% if table.first_col_is_header and forloop.first %}highlighted{% endif %}" {% cell_classname row_index col_index table_header %}>
{% if column.strip %}
{% if html_renderer %}
{{ column.strip|safe|linebreaksbr }}
{% else %}
{{ column.strip|linebreaksbr }}
{% endif %}
{% endif %}
</th>
{% else %}
<td class="{% if table.first_col_is_header and forloop.first %}highlighted{% endif %}" {% cell_classname row_index col_index table_header %}>
{% if column.strip %}
{% if html_renderer %}
{{ column.strip|safe|linebreaksbr }}
{% else %}
{{ column.strip|linebreaksbr }}
{% endif %}
{% endif %}
</td>
{% endif %}
{% endwith %}
{% endfor %}
</tr>
{% endwith %}
{% endfor %}
</tbody>
</table>
</div>
{% endwith %}
{% endblock block_content %}
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ ignore="H017"
"network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/article_double_image_block.html" = "H006,T003"
"network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/article_full_width_image_block.html" = "H006,T003"
"network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/article_image_block.html" = "H006,T003"
"network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/article_table_block.html" = "T003"
"network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/article_teaser_block.html" = "H006,T003"
"network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/audio_block.html" = "T002,T003"
"network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/base_streamfield_block.html" = "T003"
Expand Down

0 comments on commit c129386

Please sign in to comment.