Wagtail: possible to pass parent page to structblock when creating a new page?

Viewed 141

I'm creating a website that needs to be available in multiple languages. For most pages this is not a problem since we just duplicate the page tree. There are however, some components (think ModelAdmin but also some pages that are used in a similar way) that can't be translated this way.

I have a few structblocks that reference a model like this, here is one:

class StoriesSection(blocks.StructBlock):
    sub_title = blocks.CharBlock(required=True, help_text='Sub-title displayed at the very top')

    stories = blocks.ListBlock(
        blocks.StructBlock(
            [
                ('image', APIImageChooserBlock(required=True)),
                ('cta_copy', blocks.CharBlock(required=True, default='Read the article')),
                ('story', blocks.ChoiceBlock(required=True, choices=get_stories, help_text='Linked story')),
            ]
        )
    )

Now I can pass all of the stories with the method get_stories like I'm doing now:

def get_stories():
    # use get model to prevent circular import (pages.models <-> streams.blocks)
    model = apps.get_model('pages', 'StoryPage')
    return [(story.id, story.title) for story in model.objects.all()]

The problem is that a story has a specific language. When creating a new page, I would like to only display stories that match the language of the parent page in the 'story' field. In this case that is:

/en/  
    /stories/   
        /story-1/ <- would like to only pass story pages that are in here  
    /some-other-page/ <- creating this page  
/fr/  
    /stories/  
        /a-french-story/  
    ...  

Is there any way to get the correct language into the get_stories method? I am using the headless setup

0 Answers
Related