浏览代码

Add custom get_api_representation for CaptionedImageBlock and EmbedBlock

Sage Abdullah 2 月之前
父节点
当前提交
463c03770f
共有 2 个文件被更改,包括 36 次插入4 次删除
  1. 22 1
      bakerydemo/base/blocks.py
  2. 14 3
      bakerydemo/recipes/blocks.py

+ 22 - 1
bakerydemo/base/blocks.py

@@ -12,6 +12,17 @@ from wagtail.images import get_image_model
 from wagtail.images.blocks import ImageChooserBlock
 
 
+def get_image_api_representation(image):
+    return {
+        "id": image.pk,
+        "title": image.title,
+        "meta": {
+            "type": type(image)._meta.app_label + "." + type(image).__name__,
+            "download_url": image.file.url,
+        },
+    }
+
+
 class CaptionedImageBlock(StructBlock):
     """
     Custom `StructBlock` for utilizing images with associated caption and
@@ -34,6 +45,11 @@ class CaptionedImageBlock(StructBlock):
             "caption": self.preview_image.description,
         }
 
+    def get_api_representation(self, value, context=None):
+        data = super().get_api_representation(value, context)
+        data["image"] = get_image_api_representation(value["image"])
+        return data
+
     class Meta:
         icon = "image"
         template = "blocks/captioned_image_block.html"
@@ -86,6 +102,11 @@ class BlockQuote(StructBlock):
         description = "A quote with an optional attribution"
 
 
+class CustomEmbedBlock(EmbedBlock):
+    def get_api_representation(self, value, context=None):
+        return {"url": value.url, "html": value.html}
+
+
 # StreamBlocks
 class BaseStreamBlock(StreamBlock):
     """
@@ -110,7 +131,7 @@ class BaseStreamBlock(StreamBlock):
     )
     image_block = CaptionedImageBlock()
     block_quote = BlockQuote()
-    embed_block = EmbedBlock(
+    embed_block = CustomEmbedBlock(
         help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
         icon="media",
         template="blocks/embed_block.html",

+ 14 - 3
bakerydemo/recipes/blocks.py

@@ -13,7 +13,18 @@ from wagtail.contrib.typed_table_block.blocks import TypedTableBlock
 from wagtail.embeds.blocks import EmbedBlock
 from wagtail.images.blocks import ImageBlock
 
-from bakerydemo.base.blocks import BlockQuote, HeadingBlock
+from bakerydemo.base.blocks import (
+    BlockQuote,
+    HeadingBlock,
+    get_image_api_representation,
+)
+
+
+class CustomImageBlock(ImageBlock):
+    def get_api_representation(self, value, context=None):
+        data = super().get_api_representation(value, context)
+        data["image"] = get_image_api_representation(value)
+        return data
 
 
 class RecipeStepBlock(StructBlock):
@@ -57,7 +68,7 @@ class RecipeStreamBlock(StreamBlock):
             ("text", CharBlock()),
             ("numeric", FloatBlock()),
             ("rich_text", RichTextBlock()),
-            ("image", ImageBlock()),
+            ("image", CustomImageBlock()),
         ],
         group="Content",
         description=(
@@ -104,7 +115,7 @@ class RecipeStreamBlock(StreamBlock):
         },
     )
 
-    image_block = ImageBlock(group="Media")
+    image_block = CustomImageBlock(group="Media")
     embed_block = EmbedBlock(
         help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
         icon="media",