BrendaD
BrendaD

Reputation: 35

How can I use a feature flag within a custom wagtail block?

I’m working on a project where we would like to add a new option for a StreamBlock but only have that option be available to some (paid) users. So, ideally we would like to put this new option behind a feature flag.

A very simplified version of what I want to do with our code looks like this:

class OneColumnBlock(StreamBlock):
    paragraph = ParagraphBlock()
    image = ImageBlock()
    if feature_flag_is_on_for_site:
        embed = CustomEmbedBlock(max_width=800)


class ScrollingExhibitPage(Page):
    body = StreamField(
        [("one_column", OneColumnBlock()), ("two_column", TwoColumnBlock()), ("full_width", FullWidthBlock())]
    )

    content_panels = Page.content_panels + [
        StreamFieldPanel("body", heading="Body"),
    ]

We want to make that embed option only available to some specific users. If the embed was at the same level as body I think I could figure out how to put it behind a flag in the content_panels section but since it is inside a custom StreamBlock and that affects the database set up, I'm not sure how to do that effectively.

Upvotes: 0

Views: 36

Answers (1)

cnk
cnk

Reputation: 1306

We do something like this - but at the block level and the feature is enabled at the site level rather than the user level. The code is in our wagtail-jetstream repository. The Iframe block is the block that has to be 'enabled' to be used. Code for our FeatureCustomizedStreamBlock is https://github.com/caltechads/wagtail-jetstream/blob/master/jetstream/blocks.py#L46

Feel free to ask additional questions if this approach sounds useful.

Upvotes: 0

Related Questions