ifiore
ifiore

Reputation: 470

Wagtail: How to override default ImageEmbedHandler?

I've been having some trouble implementing Wagtail CMS on my own Django backend. I'm attempting to use the 'headless' version and render content on my own SPA. As a result, I need to create my own EmbedHandlers so that I can generate URL's to documents and images to a private S3 bucket. Unfortunately, though I've registered my own PrivateS3ImageEmbedHandler, Wagtail is still using the default ImageEmbedHandler to convert the html-like bodies to html. Is there a way for me to set it so that Wagtail uses my custom EmbedHandler over the built in default?

Here's my code:

from wagtail.core import blocks, hooks
from messaging.utils import create_presigned_url


class PrivateS3ImageEmbedHandler(EmbedHandler):

    identifier = "image"

    @staticmethod
    def get_model():
        return get_user_model()

    @classmethod
    def get_instance(cls, attrs):
        model = cls.get_instance(attrs)
        print(model)
        return model.objects.get(id=attrs['id'])

    @classmethod
    def expand_db_attributes(cls, attrs):
        image = cls.get_instance(attrs)
        print(image)
        presigned_url = create_presigned_url('empirehealth-mso', image.file)
        print(presigned_url)
        return f'<img src="{presigned_url}" alt="it works!"/>'

@hooks.register('register_rich_text_features')
def register_private_images(features):
    features.register_embed_type(PrivateS3ImageEmbedHandler)

Upvotes: 1

Views: 360

Answers (1)

gasman
gasman

Reputation: 25227

You need to ensure that your @hooks.register('register_rich_text_features') call happens after the one in the wagtail.images app; this can be done by either putting your app after wagtail.images in INSTALLED_APPS, or by passing an order argument greater than 0:

@hooks.register('register_rich_text_features', order=10)

Upvotes: 2

Related Questions