I touched Wagtail (3). Investigation and implementation of pop-up messages.

Introduction

Hello.

What I want to implement now is the validation setting when saving after editing the page. I want the behavior when the title exceeds 33 characters to be the same as the behavior when the title is 0 characters.

In order to investigate the implementation method, this time I investigated the code of Wagtail. Previous article


Check the Wagtail source code

Wagtail is open source, so if you search for "Wagtail Github" on the net, you will find Wagtail's Github page. I want to know which code is involved when saving the page, so let's search in the code for the time being.

スクリーンショット 2019-12-31 19.59.15.png スクリーンショット 2019-12-31 20.00.25.png

A lot of django.po files are hit. This is a file for converting the words used in the page into another language. I don't think this file is deeply related to the contents of this time, so I will skip it.

wagtail / admin / views / pages.py looks suspicious. Look for the error message in this file as well to find out which function it is used in.

Apparently, the edit function on line 323 looks like that. The create and edit functions are similar. As you can guess from the name, the function that works when the page is created and when it is edited is different. (seems so)

This is where the error message is written.

messages.validation_error(request, _("The page could not be saved due to validation errors"), form)

If you look at the import statement at the beginning of the file, messages will

from wagtail.admin import messages, signals

You can see that it is wagtail / admin / messages.py. Various functions are written, but it seems that the processing shifts to the render function in general.

def render(message, buttons, detail=''):
    return render_to_string('wagtailadmin/shared/messages.html', {
        'message': message,
        'buttons': buttons,
        'detail': detail,
    })

I found that I should use this ** messages ** to display the banner (?) Message that appears at the top of the page with wagtail.


Introduce messages method

Now, let's introduce the messages method. The method we've considered so far is to validate it on save with the save method in models.py. With this technique, you can set whether to save to the database. However, it seems difficult to deal with error messages. This time I would like to use hooks to get the error message.

hooks is a convenient function peculiar to wagtail, and it is possible to execute a program when a specific event occurs. Hooks also support before and after the Page object is edited, before and after the Page object is created, and so on. So, let's try using hooks after the Page object has been edited.

from django.http import HttpResponse
from wagtail.core import hooks

from wagtail.admin import messages

@hooks.register('after_edit_page')
def after_edit_page(request, page):
    is_title_length_valid = True if (len(page.title) > 0 | len(page.title) < 33) else False
    
    if not is_title_length_valid:
        messages.error(request, "Title Count Error")
    

def index(request):
    return HttpReesponse("Hello, world.")

The execution result looks like this.

スクリーンショット 2020-01-01 0.25.09.png

http://localhost:8000/django-admin/wagtailcore/page/

You've implemented both database save validation and error messages.


at the end

It's been a year since I was doing it while watching the kid. ** -_- ** It seems that you can write articles in MarkDown with wagtail, so I think I'll try implementing it.

Recommended Posts

I touched Wagtail (3). Investigation and implementation of pop-up messages.
I touched Wagtail (1) and let's override the save method.
Explanation and implementation of SocialFoceModel
I touched Wagtail (2). Introducing django-extensions.
I touched Tensorflow and keras
Explanation and implementation of PRML Chapter 4
Introduction and Implementation of JoCoR-Loss (CVPR2020)
Explanation and implementation of ESIM algorithm
Introduction and implementation of activation function
[Python] I thoroughly explained the theory and implementation of logistic regression
Explanation and implementation of simple perceptron
[Python] I thoroughly explained the theory and implementation of decision trees
Implementation and experiment of convex clustering method
Explanation and implementation of Decomposable Attention algorithm
I read the implementation of golang channel
[Python] I thoroughly explained the theory and implementation of support vector machine (SVM)
I read the implementation of range (Objects / rangeobject.c)
Implementation of TRIE tree with Python and LOUDS
I touched some of the new features of Python 3.8 ①
I / O related summary of python and fortran
I tried morphological analysis and vectorization of words
I read and implemented the Variants of UKR
Explanation of edit distance and implementation in Python