Skip to content

Commit

Permalink
return a url for link values
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo committed Jul 16, 2024
1 parent 66d126e commit fe21d6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
42 changes: 9 additions & 33 deletions pages/custom_blocks.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from django import forms
from filetype.types import document

from wagtail import blocks
from wagtail.blocks import FieldBlock, StructBlock, StructValue
from wagtail.images.blocks import ImageChooserBlock
from wagtail.documents.blocks import DocumentChooserBlock
from wagtail.models import Page

from api.serializers import ImageSerializer
from openstax.functions import build_image_url, build_document_url
from wagtail.templatetags import wagtailcore_tags
from wagtail.rich_text import expand_db_html


class APIRichTextBlock(blocks.RichTextBlock):
def get_api_representation(self, value, context=None):
return wagtailcore_tags.richtext(value.source)
representation = super().get_api_representation(value, context)
return expand_db_html(representation)

class Meta:
icon = 'doc-full'
Expand Down Expand Up @@ -47,6 +51,8 @@ class Meta:
icon = 'link'
max_num = 1

def get_api_representation(self, value, context=None):
return self.render_basic(value)

class CTALinkBlock(blocks.StructBlock):
text = blocks.CharBlock(required=True)
Expand All @@ -63,44 +69,14 @@ class ImageFormatChoiceBlock(FieldBlock):
('left', 'Wrap left'), ('right', 'Wrap right'), ('mid', 'Mid width'), ('full', 'Full width'),))


class ImageStructValue(StructValue):
def image(self):
return build_image_url(self['image'])

def alt_text(self):
return self['alt_text']

def alignment(self):
return self['alignment']

def cta(self):
return self['cta']


# Use this block to return the path in the v2/pages API
class APIImageBlock(StructBlock):
image = ImageChooserBlock(required=False)
alt_text = blocks.CharBlock(required=False)
alignment = ImageFormatChoiceBlock(required=False)
cta = CTALinkBlock(required=False, label="CTA")

def get_api_representation(self, value, context=None):
try:
return ImageSerializer(context=context).to_representation(value)
except AttributeError:
return None

class Meta:
icon = 'image'
# value_class = ImageStructValue

class APIImageChooserBlock(ImageChooserBlock):
def get_api_representation(self, value, context=None):
try:
return ImageSerializer(context=context).to_representation(value)
except AttributeError:
return None


class DividerBlock(StructBlock):
image = APIImageChooserBlock()
config = blocks.StreamBlock([
Expand Down
7 changes: 6 additions & 1 deletion pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
from django.db import models

from modelcluster.fields import ParentalKey
from wagtail import hooks
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel, TitleFieldPanel
from wagtail.admin.widgets.slug import SlugInput
from wagtail import blocks
from wagtail.fields import RichTextField, StreamField
from wagtail.models import Orderable, Page
from wagtail.api import APIField
from wagtail.models import Site
from wagtail.rich_text import LinkHandler
from wagtail.images.models import Image
import rest_framework.fields as fields
from rest_framework.fields import Field
from rest_framework import serializers

from api.models import FeatureFlag
from openstax.functions import build_image_url, build_document_url
Expand All @@ -33,7 +38,7 @@
AllyLogoBlock, \
AssignableBookBlock, \
DividerBlock, \
APIRichTextBlock, APIImageBlock, CTALinkBlock, FAQBlock
APIRichTextBlock, CTALinkBlock, FAQBlock

from .custom_fields import \
Group
Expand Down

0 comments on commit fe21d6c

Please sign in to comment.