Skip to content

Commit

Permalink
display value for LinkBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo committed Jul 18, 2024
1 parent f8aa3b0 commit a41dabf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
2 changes: 2 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CustomizationRequest(models.Model):
complete = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)


class FeatureFlag(models.Model):
name = models.CharField(max_length=255, unique=True, help_text='Create flag names with underscores to be more machine friendly. Eg. awesome_feature')
description = models.TextField(blank=True, default='')
Expand All @@ -29,6 +30,7 @@ class FeatureFlag(models.Model):
def __str__(self):
return self.name


class WebviewSettings(models.Model):
name = models.CharField(max_length=255, unique=True)
value = models.CharField(max_length=255)
Expand Down
2 changes: 2 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from wagtail.documents.models import Document
from api.models import ProgressTracker, CustomizationRequest


class AdopterSerializer(serializers.HyperlinkedModelSerializer):

class Meta:
Expand Down Expand Up @@ -72,6 +73,7 @@ class ModuleListingField(serializers.StringRelatedField):
def to_internal_value(self, value):
return value


class CustomizationRequestSerializer(serializers.ModelSerializer):
modules = ModuleListingField(many=True)

Expand Down
12 changes: 12 additions & 0 deletions openstax/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@
WAGTAIL_USAGE_COUNT_ENABLED = False
WAGTAIL_USER_CUSTOM_FIELDS = ['is_staff']
WAGTAIL_GRAVATAR_PROVIDER_URL = '//www.gravatar.com/avatar'
# serve wagtail documents direct for use with remote (s3) storage
WAGTAILADMIN_EXTERNAL_LINK_CONVERSION = 'exact'
WAGTAIL_REDIRECTS_FILE_STORAGE = 'cache'
WAGTAILFORMS_HELP_TEXT_ALLOW_HTML = True

WAGTAILSEARCH_BACKENDS = {
'default': {
Expand Down Expand Up @@ -455,6 +459,14 @@
},
}

from wagtail.embeds.oembed_providers import youtube, vimeo
WAGTAILEMBEDS_FINDERS = [
{
'class': 'wagtail.embeds.finders.oembed',
'providers': [youtube, vimeo]
}
]

##########
# Sentry #
##########
Expand Down
49 changes: 22 additions & 27 deletions pages/custom_blocks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from django import forms
from filetype.types import document

from wagtail import blocks
from wagtail.blocks import FieldBlock, StructBlock, StructValue
from wagtail.blocks import FieldBlock, StructBlock
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
Expand All @@ -21,27 +19,6 @@ class Meta:
icon = 'doc-full'


class LinkStructValue(StructValue):
def url(self):
if self['external']:
return self['external']
elif self['internal']:
return self['internal']
elif self['document']:
return build_document_url(self['document'].url)
else:
return 'link issue'

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

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

def __bool__(self):
return bool(self.url())


class LinkBlock(blocks.StreamBlock):
external = blocks.URLBlock(required=False)
internal = blocks.PageChooserBlock(required=False)
Expand All @@ -52,7 +29,26 @@ class Meta:
max_num = 1

def get_api_representation(self, value, context=None):
return self.render_basic(value)
for child in value:
if child.block_type == 'document':
return {
'value': child.value.url,
'type': child.block_type,
'metadata': child.value.content_type,
}
elif child.block_type == 'external':
return {
'value': child.block.get_prep_value(child.value),
'type': child.block_type,
}
elif child.block_type == 'internal':
return {
'value': child.value.url_path,
'type': child.block_type,
}
else:
return None


class CTALinkBlock(blocks.StructBlock):
text = blocks.CharBlock(required=True)
Expand All @@ -62,7 +58,7 @@ class CTALinkBlock(blocks.StructBlock):
class Meta:
icon = 'placeholder'
label = "Call to Action"
value_class = LinkStructValue


class CTAButtonBarBlock(blocks.StructBlock):
actions=blocks.ListBlock(CTALinkBlock(required=False, label="Button"),
Expand All @@ -74,7 +70,6 @@ class CTAButtonBarBlock(blocks.StructBlock):
class Meta:
icon = 'placeholder'
label = "Calls to Action"
value_class = LinkStructValue

class ImageFormatChoiceBlock(FieldBlock):
field = forms.ChoiceField(required=False, choices=(
Expand Down
7 changes: 4 additions & 3 deletions pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
AllyLogoBlock, \
AssignableBookBlock, \
DividerBlock, \
APIRichTextBlock, CTAButtonBarBlock, CTALinkBlock, FAQBlock
APIRichTextBlock, \
CTAButtonBarBlock

from .custom_fields import \
Group
from .custom_fields import Group
import snippets.models as snippets

# Constants for styling options on Root/Flex pages
Expand Down Expand Up @@ -78,6 +78,7 @@
('cta_block', CTAButtonBarBlock()),
]


class LayoutSnippetSerializer(Field):
def to_representation(self, value):
return {
Expand Down

0 comments on commit a41dabf

Please sign in to comment.