Ephes Blog

Miscellaneous things. Mostly Weeknotes and links I stumbled upon.

Date -

Weeknotes 2023-09-11

, Jochen
It was the best of times. It was the worst of terms and conditions. --Nein

At work, setting up Single Sign-On (SSO) via Azure AD for a Django project was easier than expected, thanks to django-saml2-auth. I also found that ChatGPT was surprisingly effective at generating code to create a simplified Excel file, which I then could use to test the effectiveness of the existing pandas code for Excel parsing.

Doing open source, I fixed a bug in the theme-selector feature of django-cast. The bug occurred because Django handles the HTML rendering for SPA-themes and exposes it via a JSON API. I chose this method to avoid rendering a Wagtail streamfield in JavaScript. However, it used the theme from the database instead of the session template, which broke the Vue.js theme. After addressing this, I updated django-stubs and fixed a minor issue that made mypy pass again. I also improved the documentation for the themes feature. While reviewing the request-handling code, I found a simpler method using APIField and wrote a 'Today I Learned' (TIL) post about it. And finally, I broke most of the images on my site by accidentally deleting all the Wagtail renditions. Well, something to do for next week.

Articles

Weeknotes

Software

Mastodon / Twitter

Videos

Podcasts


How to Pass a Request Object from Wagtail API to a Page

, Jochen

Introduction

When using Wagtail's API to fetch website pages, the APIField class allows you to add custom fields. But what if you also need to incorporate the request object into your custom field?

Why the Request Object is Important

I ran into this issue because I wanted to fetch the fully-rendered HTML of a page via the Wagtail API, eliminating the need to manually render the StreamField in my Vue.js blog theme.

Code Example

To achieve this, use the APIField class as demonstrated in the code snippet below:

from wagtail.models import Page
from wagtail.api import APIField

from rest_framework.fields import Field

class HtmlField(Field):
    def to_representation(self, page):
        return page.serve(self.context["request"])

class Post(Page):
    ...
    api_fields = [
        APIField("html", serializer=HtmlField(source="*")),
    ]

Understanding the source="*" Parameter

The `source="*"` parameter is special: it enables the `to_representation` method to work with the entire page object, rather than just a specific field on that page.