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 some more block types #1573

Merged
merged 11 commits into from
Jul 26, 2024
66 changes: 60 additions & 6 deletions pages/custom_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class LinkBlock(blocks.StreamBlock):
external = blocks.URLBlock(required=False)
internal = blocks.PageChooserBlock(required=False)
document = DocumentChooserBlock(required=False)
anchor = blocks.CharBlock(required=False)

class Meta:
icon = 'link'
Expand All @@ -46,26 +47,76 @@ def get_api_representation(self, value, context=None):
'value': child.value.url_path,
'type': child.block_type,
}
elif child.block_type == 'anchor':
return {
'value': "#{anchor}".format(anchor=child.value),
'type': child.block_type,
}
else:
return None


class CTALinkBlock(blocks.StructBlock):
class LinkInfoBlock(blocks.StructBlock):
text = blocks.CharBlock(required=True)
aria_label = blocks.CharBlock(required=False)
target = LinkBlock(required=True)

class Meta:
icon = 'placeholder'
label = "Link"

class CTALinkBlock(LinkInfoBlock):
text = blocks.CharBlock(required=True)
aria_label = blocks.CharBlock(required=False)
target = LinkBlock(required=True)
config = blocks.StreamBlock([
('style', blocks.ChoiceBlock(choices=[
('primary', 'Primary'),
('white', 'White'),
('blue_outline', 'Blue Outline'),
('deep_green_outline', 'Deep Green Outline'),
], default='descending')),
], block_counts={
'style': {'max_num': 1},
}, required=False)

class Meta:
icon = 'placeholder'
label = "Call to Action"


class LinksGroupBlock(blocks.StructBlock):
links = blocks.ListBlock(
LinkInfoBlock(required=False, label="Link"),
default=[], label='Links'
)
config = blocks.StreamBlock([
('color', blocks.ChoiceBlock(choices=[
('white', 'White'),
('blue', 'Blue'),
('deep_green', 'Deep Green'),
], default='descending')),
], block_counts={
'color': {'max_num': 1},
}, required=False)

class Meta:
icon = 'placeholder'
label = "Links Group"

class CTAButtonBarBlock(blocks.StructBlock):
actions = blocks.ListBlock(CTALinkBlock(required=False, label="Button"),
default=[],
max_num=2,
label='Actions'
)
actions = blocks.ListBlock(
CTALinkBlock(required=False, label="Button"),
default=[], max_num=2, label='Actions'
)
config = blocks.StreamBlock([
('priority', blocks.ChoiceBlock(choices=[
('descending', 'Descending'),
('equal', 'Equal'),
], default='descending')),
], block_counts={
'priority': {'max_num': 1},
}, required=False)

class Meta:
icon = 'placeholder'
Expand All @@ -88,6 +139,9 @@ def get_api_representation(self, value, context=None):
except AttributeError:
return None

class QuoteBlock(StructBlock):
image = APIImageChooserBlock()
content = blocks.RichTextBlock()

class DividerBlock(StructBlock):
image = APIImageChooserBlock()
Expand Down
611 changes: 611 additions & 0 deletions pages/migrations/0137_alter_rootpage_body_alter_rootpage_layout.py

Large diffs are not rendered by default.

676 changes: 676 additions & 0 deletions pages/migrations/0138_alter_rootpage_body.py

Large diffs are not rendered by default.

698 changes: 698 additions & 0 deletions pages/migrations/0139_alter_rootpage_body.py

Large diffs are not rendered by default.

716 changes: 716 additions & 0 deletions pages/migrations/0140_alter_rootpage_body.py

Large diffs are not rendered by default.

734 changes: 734 additions & 0 deletions pages/migrations/0141_alter_rootpage_body.py

Large diffs are not rendered by default.

786 changes: 786 additions & 0 deletions pages/migrations/0142_alter_rootpage_body.py

Large diffs are not rendered by default.

985 changes: 985 additions & 0 deletions pages/migrations/0143_alter_rootpage_body_alter_rootpage_layout.py

Large diffs are not rendered by default.

985 changes: 985 additions & 0 deletions pages/migrations/0144_alter_rootpage_body_alter_rootpage_layout.py

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
DividerBlock, \
APIRichTextBlock, \
CTAButtonBarBlock, \
LinksGroupBlock, \
QuoteBlock, \
CTALinkBlock

from .custom_fields import Group
Expand Down Expand Up @@ -82,6 +84,8 @@
('text', APIRichTextBlock()),
('html', blocks.RawHTMLBlock()),
('cta_block', CTAButtonBarBlock()),
('links_group', LinksGroupBlock()),
('quote', QuoteBlock()),
]

# we have one RootPage, which is the parent of all other pages
Expand All @@ -105,6 +109,11 @@ class RootPage(Page):
('image', APIImageChooserBlock(required=False)),
('image_alt', blocks.CharBlock(required=False)),
('config', blocks.StreamBlock([
('id', blocks.RegexBlock(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are the options for this going to be documented somewhere?
how do you expect this will be used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the id is the HTML id, which can be used in anchor links to link to a part of the page

i should probably go through and add some more useful help text to all the options. i was also gonna make some persistent pages on dev to showcase the different content types

regex=r'[a-zA-Z0-9\-_]',
help_text='html id of this element. eg: cool_section',
error_mssages={'invalid': 'not a valid id.'}
)),
('image_alignment', blocks.ChoiceBlock(choices=HERO_IMAGE_ALIGNMENT_CHOICES)),
('image_size', blocks.ChoiceBlock(choices=HERO_IMAGE_SIZE_CHOICES)),
('padding', blocks.IntegerBlock(min_value=0, help_text='Padding multiplier. default 0.')),
Expand All @@ -114,6 +123,7 @@ class RootPage(Page):
error_mssages={'invalid': 'not a valid hex color.'}
)),
], block_counts={
'id': {'max_num': 1},
'image_alignment': {'max_num': 1},
'image_size': {'max_num': 1},
'padding': {'max_num': 1},
Expand All @@ -123,6 +133,11 @@ class RootPage(Page):
('section', blocks.StructBlock([
('content', blocks.StreamBlock(SECTION_CONTENT_BLOCKS)),
('config', blocks.StreamBlock([
('id', blocks.RegexBlock(
regex=r'[a-zA-Z0-9\-_]',
help_text='html id of this element. eg: cool_section',
error_mssages={'invalid': 'not a valid id.'}
)),
('background_color', blocks.RegexBlock(
regex=r'#[a-zA-Z0-9]{6}',
help_text='eg: #ff0000',
Expand All @@ -135,6 +150,7 @@ class RootPage(Page):
('right', 'Right'),
], default='left')),
], block_counts={
'id': {'max_num': 1},
'background_color': {'max_num': 1},
'padding': {'max_num': 1},
'text_alignment': {'max_num': 1},
Expand Down
Loading