How to reuse common Wagtail Page objects (streamfields, edit_handler, content_panels)

725 views Asked by At

From the demo page, I've seen reuse of common StreamBlock elements, with abstract classes that inherit from django.Models.

I would like to know how I can reuse a set of Wagtail Page objects. Given this model:

class EventPage(Page):
    body = StreamField(
        HomePageStreamBlock(),
        verbose_name='main content',
        default=''
    )
    headingpanel = StreamField(
        HeadingPanelStreamBlock(),
        verbose_name='overpanel',
        default=''
    )
    sidepanel = StreamField(
        SidePanelStreamBlock(),
        verbose_name='sidepanel',
        default=''
    )

    master_event_id = models.CharField(max_length=1024, unique=True)
    creator = JSONField(null=True, blank=True)
    recurrence = ArrayField(
        models.CharField(max_length=200),
        null=True,
        blank=True
    )
    google_cal_url = models.URLField('Google calendar link', max_length=1024)
    calendar = models.ForeignKey(
        Calendar,
        on_delete=models.PROTECT,
        verbose_name='Calendar',
        related_name='events',
        null=True,
        blank=False
    )

    search_fields = Page.search_fields + [index.SearchField('body')]

    content_panels = Page.content_panels + [
        FieldPanel('calendar'),
        FieldPanel('title'),
        StreamFieldPanel('body'),
    ]

    pagesection_panels = [
        StreamFieldPanel('headingpanel'),
        StreamFieldPanel('sidepanel'),
    ]

    edit_handler = TabbedInterface([
        ObjectList(content_panels),
        ObjectList(pagesection_panels),
        ObjectList(Page.promote_panels),
        ObjectList(Page.settings_panels, classname='settings'),
    ])

I would want to reuse the streamfield objects (body, headingpanel, sidepanel) and the content_panel StreamFieldPanel('body'), and the edit_handler. The only compelling reason for wanting this is the common DRY principle.

I would like to know if trying to do code reuse here is viable and worth pursuing. I don't fully understand the Django ORM and the exact way of how Wagtail implements the Page model. My inclination is to just keep things super basic by avoiding code reuse, to avoid potential foot shooting in the future. However a part of me feels that duplicating this much code can quickly get ugly.

If I can safely reuse this common code, what is the correct way to go about it? I've tried putting these common objects in an abstract base class, but even though nothing broke doesn't mean that it's correct and that I have not set myself up for a near future foot shooting appointment.

I've also heard of mixins, but haven't tried them out.

I would like to deploy soon. The functionality is all there, but I'd really like to have these fundamentals ironed out before committing to a production setup.

Any advice would be much appreciated.

0

There are 0 answers