djangowagtaildjango-messageswagtail-admin

wagtail on-publish custom sucess message


I have a question regarding the success message wagtail is displaying when an editor is publishing a page in the admin interface. It's usually "Page {Page.title} has been published". 
I’am trying to figure out how to add one’s own success or error message on-publishing resp. an error occuring.
For a Page in my project I have implemented the below receiver that gets activated if the editor uploads a file via a certain field and publishes the site. This file is then processed by a script, though the file itself is not saved. For the editors, I would like to have a custom success message displayed below the default on-publish success message if they uploaded the file successfully, tough only if the pages is published after uplaoding a file. I found the question linked below, I could figure out how to show a custom message but it is not a very satisfying solution.

The wagtail version, I'am using is 4.22

customize-post-publish-flash-message-in-wagtail-cms

@receiver(page_published, sender=Model)
def process_uploaded_file(sender, instance, **kwargs):
    if instance.uploaded_file:
        process_data(instance.uploaded_file.path)
        instance.uploaded_file.delete(save=False)
        instance.uploaded_file = None
        instance.date = (date.today())
        instance.save()

I took the hook from the answer in the question linked above and changed it a bit. If one of the pages of my model gets edited and published it checks for the latest revision and checks within it's contents for the field 'uploaded_file' wether something was uploaded. So far that works, but it's quite unsatisfying to have the receiver for enabling the user uploading a file to refresh data (for a dashboard) and the hook for displaying a message in the admin interface if the editor has indeed done so. Both things should happen at the same place, and checking the json of the revisions seems not a good solutions.

def do_after_page_edit(request, page):
    if isinstance(page, MyPage) :
        latest_revision = revisions.order_by('-created_at').first()
        latest_content = latest_revision.content
        if latest_content['uploaded_file']:
            messages.success(request, f"You have updated the data to Page {page.title}")
        else:
            messages.success(request, f"You have edited and published Page {page.title} but not updated the data")

Solution

  • I think you have run into the fundamental trade-off with hook architectures. Having hooks to allow programmers to add or modify behavior at certain points in a process makes the process flexible - but at the cost of having the code a bit spread out. I agree that sometimes makes it harder to keep all the pieces in your head - but it's better than monkey patching which would sentence you to revisiting that code every time you upgrade.

    It looks like you are using signals and responding to a page_published signal. Does your code become nicer if you use an "after_publish_page" hook?