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

DSRC-65: Call to action block #1648

Merged
merged 19 commits into from
Jun 18, 2024
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
2 changes: 1 addition & 1 deletion etna/api/tests/expected_results/article.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
{
"type": "featured_record_article",
"value": {
"page": {}
"page": null
},
"id": "b505f636-f3d1-4d4b-b368-69183e324e6e"
},
Expand Down
4 changes: 3 additions & 1 deletion etna/core/blocks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .base import SectionDepthAwareStructBlock
from .cta import LargeCardLinksBlock
from .cta import ButtonBlock, CallToActionBlock, LargeCardLinksBlock
from .featured_content import (
FeaturedCollectionBlock,
FeaturedRecordArticleBlock,
Expand All @@ -22,6 +22,8 @@
__all__ = [
"APIPageChooserBlock",
"AuthorPromotedPagesBlock",
"ButtonBlock",
"CallToActionBlock",
"ContentImageBlock",
"FeaturedRecordArticleBlock",
"FeaturedCollectionBlock",
Expand Down
52 changes: 52 additions & 0 deletions etna/core/blocks/cta.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

from wagtail import blocks

from .page_chooser import APIPageChooserBlock
from .paragraph import APIRichTextBlock


class LargeCardLinksBlock(blocks.StructBlock):
Expand All @@ -29,3 +32,52 @@ def get_context(self, value, parent_context=None):
link_pages.append(page_2.specific)
context["link_pages"] = link_pages
return context


class ButtonBlock(blocks.StructBlock):
label = blocks.CharBlock()
link = APIPageChooserBlock(required=False)
external_link = blocks.URLBlock(required=False)
accented = blocks.BooleanBlock(
required=False,
help_text="Use the accented button style",
label="Accented",
)

def clean(self, value):
data = super().clean(value)

if data.get("link") and data.get("external_link"):
raise ValidationError(
"You must provide either a page link or an external link, not both."
)
elif not (data.get("link") or data.get("external_link")):
raise ValidationError(
"You must provide either a page link or an external link."
)

return data

def get_api_representation(self, value, context=None):
representation = {
"label": value["label"],
"href": value.get("external_link") or value["link"].full_url,
"accent": value.get("accented") or False,
}

return representation

class Meta:
icon = "link"
label = "Button"


class CallToActionBlock(blocks.StructBlock):
body = APIRichTextBlock(
max_length=100, features=settings.RESTRICTED_RICH_TEXT_FEATURES
)
button = ButtonBlock()

class Meta:
icon = "link"
label = "Call to action"
2 changes: 1 addition & 1 deletion etna/core/serializers/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_api_data(object, required_api_fields: list = []) -> dict:
api_representation[field.name] = get_field_data(
object=specific, field=field
)
return api_representation
return api_representation or None


class DefaultPageSerializer(serializers.Serializer):
Expand Down
4 changes: 4 additions & 0 deletions etna/generic_pages/blocks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from wagtail import blocks

from etna.core.blocks import (
ButtonBlock,
CallToActionBlock,
ContentImageBlock,
FeaturedRecordArticleBlock,
ParagraphBlock,
Expand All @@ -16,6 +18,8 @@


class SectionContentBlock(blocks.StreamBlock):
button = ButtonBlock()
call_to_action = CallToActionBlock()
featured_record_article = FeaturedRecordArticleBlock()
image = ContentImageBlock()
media = MediaBlock()
Expand Down
Loading
Loading