Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preformatted and blockquote #947

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions blocks/blocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.validators import validate_slug
from django import forms
from django.forms.utils import flatatt
from django.utils.html import format_html, format_html_join
from wagtail import blocks as wagtail_blocks
Expand Down Expand Up @@ -166,3 +167,18 @@ class Meta:
class WfURLBlock(wagtail_blocks.URLBlock):
class Meta:
template = "blocks/blocks/wf_url.html"


class PreformattedTextBlock(wagtail_blocks.FieldBlock):
"""Renders input as preformatted text (<pre> tag)"""

class Meta:
template = "blocks/blocks/preformatted_text.html"

def __init__(self, required=True, help_text=None, **kwargs):
self.field = forms.CharField(
required=required,
help_text=help_text,
widget=forms.Textarea(attrs={"rows": 10}),
)
super().__init__(**kwargs)
1 change: 1 addition & 0 deletions blocks/templates/blocks/blocks/preformatted_text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<pre>{{ value }}</pre>
146 changes: 146 additions & 0 deletions library/migrations/0021_alter_libraryitem_body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Generated by Django 4.2.7 on 2023-11-17 17:28

import blocks.blocks
import django.core.validators
from django.db import migrations
import documents.blocks
import re
import wagtail.blocks
import wagtail.embeds.blocks
import wagtail.fields
import wagtail.images.blocks
import wagtail_color_panel.blocks


class Migration(migrations.Migration):
dependencies = [
("library", "0020_alter_libraryitem_body"),
]

operations = [
migrations.AlterField(
model_name="libraryitem",
name="body",
field=wagtail.fields.StreamField(
[
(
"heading",
wagtail.blocks.StructBlock(
[
(
"heading_level",
wagtail.blocks.ChoiceBlock(
choices=[
("h2", "Level 2 (child of level 1)"),
("h3", "Level 3 (child of level 2)"),
("h4", "Level 4 (child of level 3)"),
("h5", "Level 5 (child of level 4)"),
("h6", "Level 6 (child of level 5)"),
],
help_text="These different heading levels help to communicate the organization and hierarchy of the content on a page.",
),
),
(
"heading_text",
wagtail.blocks.CharBlock(
help_text="The text to appear in the heading."
),
),
(
"target_slug",
wagtail.blocks.CharBlock(
help_text="Used to link to a specific location within this page. A slug should only contain letters, numbers, underscore (_), or hyphen (-).",
required=False,
validators=(
django.core.validators.RegexValidator(
re.compile("^[-a-zA-Z0-9_]+\\Z"),
"Enter a valid “slug” consisting of letters, numbers, underscores or hyphens.",
"invalid",
),
),
),
),
(
"color",
wagtail_color_panel.blocks.NativeColorBlock(
required=False
),
),
]
),
),
(
"rich_text",
wagtail.blocks.RichTextBlock(
features=[
"bold",
"italic",
"ol",
"ul",
"link",
"hr",
"blockquote",
]
),
),
(
"image",
wagtail.blocks.StructBlock(
[
("image", wagtail.images.blocks.ImageChooserBlock()),
(
"width",
wagtail.blocks.IntegerBlock(
help_text="Enter the desired image width value in pixels up to 800 max.",
max_value=800,
min_value=0,
),
),
(
"align",
wagtail.blocks.ChoiceBlock(
choices=[("left", "Left"), ("right", "Right")],
help_test="Optionally align image left or right. Will default to block alignment.",
icon="file-richtext",
required=False,
),
),
(
"link",
wagtail.blocks.URLBlock(
help_text="Optional web address to use as image link.",
required=False,
),
),
],
classname="full title",
),
),
("document", documents.blocks.DocumentEmbedBlock()),
("media", blocks.blocks.MediaBlock(icon="media")),
("embed", wagtail.embeds.blocks.EmbedBlock()),
("url", blocks.blocks.WfURLBlock()),
("pullquote", blocks.blocks.PullQuoteBlock()),
(
"spacer",
wagtail.blocks.StructBlock(
[
(
"height",
wagtail.blocks.DecimalBlock(
decimal_places=1,
help_text="The height of this spacer in 'em' values where 1 em is one uppercase M.",
min_value=0,
),
)
]
),
),
("preformatted_text", blocks.blocks.PreformattedTextBlock()),
],
blank=True,
null=True,
use_json_field=True,
),
),
]
3 changes: 3 additions & 0 deletions library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
FormattedImageChooserStructBlock,
HeadingBlock,
MediaBlock,
PreformattedTextBlock,
PullQuoteBlock,
SpacerBlock,
WfURLBlock,
Expand Down Expand Up @@ -58,6 +59,7 @@ class LibraryItem(DrupalFields, Page): # type: ignore
"ul",
"link",
"hr",
"blockquote",
],
),
),
Expand All @@ -78,6 +80,7 @@ class LibraryItem(DrupalFields, Page): # type: ignore
("url", WfURLBlock()),
("pullquote", PullQuoteBlock()),
("spacer", SpacerBlock()),
("preformatted_text", PreformattedTextBlock()),
],
null=True,
blank=True,
Expand Down
144 changes: 144 additions & 0 deletions magazine/migrations/0029_alter_magazinearticle_body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Generated by Django 4.2.7 on 2023-11-17 17:28

import blocks.blocks
import django.core.validators
from django.db import migrations
import documents.blocks
import re
import wagtail.blocks
import wagtail.fields
import wagtail.images.blocks
import wagtail_color_panel.blocks


class Migration(migrations.Migration):
dependencies = [
("magazine", "0028_alter_magazineissue_publication_date"),
]

operations = [
migrations.AlterField(
model_name="magazinearticle",
name="body",
field=wagtail.fields.StreamField(
[
(
"heading",
wagtail.blocks.StructBlock(
[
(
"heading_level",
wagtail.blocks.ChoiceBlock(
choices=[
("h2", "Level 2 (child of level 1)"),
("h3", "Level 3 (child of level 2)"),
("h4", "Level 4 (child of level 3)"),
("h5", "Level 5 (child of level 4)"),
("h6", "Level 6 (child of level 5)"),
],
help_text="These different heading levels help to communicate the organization and hierarchy of the content on a page.",
),
),
(
"heading_text",
wagtail.blocks.CharBlock(
help_text="The text to appear in the heading."
),
),
(
"target_slug",
wagtail.blocks.CharBlock(
help_text="Used to link to a specific location within this page. A slug should only contain letters, numbers, underscore (_), or hyphen (-).",
required=False,
validators=(
django.core.validators.RegexValidator(
re.compile("^[-a-zA-Z0-9_]+\\Z"),
"Enter a valid “slug” consisting of letters, numbers, underscores or hyphens.",
"invalid",
),
),
),
),
(
"color",
wagtail_color_panel.blocks.NativeColorBlock(
required=False
),
),
]
),
),
(
"rich_text",
wagtail.blocks.RichTextBlock(
features=[
"bold",
"italic",
"ol",
"ul",
"hr",
"link",
"document-link",
"superscript",
"superscript",
"strikethrough",
"blockquote",
]
),
),
("pullquote", blocks.blocks.PullQuoteBlock()),
("document", documents.blocks.DocumentEmbedBlock()),
(
"image",
wagtail.blocks.StructBlock(
[
("image", wagtail.images.blocks.ImageChooserBlock()),
(
"width",
wagtail.blocks.IntegerBlock(
help_text="Enter the desired image width value in pixels up to 800 max.",
max_value=800,
min_value=0,
),
),
(
"align",
wagtail.blocks.ChoiceBlock(
choices=[("left", "Left"), ("right", "Right")],
help_test="Optionally align image left or right. Will default to block alignment.",
icon="file-richtext",
required=False,
),
),
(
"link",
wagtail.blocks.URLBlock(
help_text="Optional web address to use as image link.",
required=False,
),
),
],
classname="full title",
),
),
(
"spacer",
wagtail.blocks.StructBlock(
[
(
"height",
wagtail.blocks.DecimalBlock(
decimal_places=1,
help_text="The height of this spacer in 'em' values where 1 em is one uppercase M.",
min_value=0,
),
)
]
),
),
("preformatted_text", blocks.blocks.PreformattedTextBlock()),
],
use_json_field=True,
),
),
]
3 changes: 3 additions & 0 deletions magazine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from blocks.blocks import (
FormattedImageChooserStructBlock,
HeadingBlock,
PreformattedTextBlock,
PullQuoteBlock,
SpacerBlock,
)
Expand Down Expand Up @@ -300,13 +301,15 @@ class MagazineArticle(DrupalFields, Page): # type: ignore
"superscript",
"superscript",
"strikethrough",
"blockquote",
],
),
),
("pullquote", PullQuoteBlock()),
("document", DocumentEmbedBlock()),
("image", FormattedImageChooserStructBlock(classname="full title")),
("spacer", SpacerBlock()),
("preformatted_text", PreformattedTextBlock()),
],
use_json_field=True,
)
Expand Down
Loading