wagtail / wagtail-newsletter

Send email newsletters based on Wagtail content
https://wagtail-newsletter.readthedocs.io
BSD 3-Clause "New" or "Revised" License
9 stars 3 forks source link

Spike: keep newsletter page fields out of revisions #10

Closed mgax closed 2 months ago

mgax commented 2 months ago

This approach to storing the newsletter audience, as a field on the Page model, has the nasty side-effect of copying the field value into revisions. Restoring a revision will overwrite the current value, which is probably not what people expect.

Find a way to store newsletter metadata for a page outside of the revision system. Maybe:

mgax commented 2 months ago

It's possible to hook into the revision loading mechanism and replace a field with whatever is saved on the Page object:

    def with_content_json(self, content):
         obj = super().with_content_json(content)
         obj.newsletter_audience_id = self.newsletter_audience_id
         return obj

Then, when saving a revision (which happens when saving a draft), we can save the field on the main Page object, in a similar fashion to how Wagtail's Page model saves its own internal fields:

     def save_revision(self, *args, **kwargs):
         revision = super().save_revision(*args, **kwargs)
         self.save(update_fields=["newsletter_audience_id"], clean=False)
         return revision

This will leave copies of our fields in revisions, but they will get replaced by with_content_json every time they are loaded.