g:如何覆盖默认ImageEmbedHandler?

时间:2019-09-17 20:22:03

标签: python django wagtail

我在自己的Django后端上实施Wagtail CMS时遇到了一些麻烦。我正在尝试使用“无头”版本并在自己的SPA上呈现内容。结果,我需要创建自己的EmbedHandlers,以便可以生成指向文档和图像的URL,并将其生成到私有S3存储桶。不幸的是,尽管我已经注册了自己的PrivateS3ImageEmbedHandler,但Wagtail仍在使用默认的ImageEmbedHandler将类似html的主体转换为html。有没有办法设置我的位置,以便Wagtail使用自定义的EmbedHandler而不是内置的默认值?

这是我的代码:

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)

1 个答案:

答案 0 :(得分:1)

您需要确保您的@hooks.register('register_rich_text_features')通话发生在wagtail.images应用中的通话之后;这可以通过将您的应用放在wagtail.images中的INSTALLED_APPS之后,或通过传递大于0的order参数来实现:

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