JeremyG
JeremyG

Reputation: 179

How to update a Wagtail Page

I am using a wagtail_hook to update a Page object and am running into trouble. More specifically, when an end-user hits the "Save draft" button from the browser I want the following code to fire. The purpose of this code is to change the knowledge_base_id dynamically, based on the results of the conditional statements listed below.

def sync_knowledge_base_page_with_zendesk2(request, page):
    if isinstance(page, ContentPage):
        page_instance = ContentPage.objects.get(pk=page.pk)
        pageJsonHolder = page_instance.get_latest_revision().content_json
        content = json.loads(pageJsonHolder)
        print("content at the start = ")
        print(content['knowledge_base_id'])
        kb_id_revision = content['knowledge_base_id']
        kb_active_revision = content['kb_active']
        if kb_active_revision == "T":
            if kb_id_revision == 0:
                print("create the article")
                content['knowledge_base_id'] = 456
                #page_instance.knowledge_base_id = 456 # change this API call
            else:
                print("update the article")
        elif kb_id_revision != 0:
            print("delete the article")
            content['knowledge_base_id'] = 0
            #page_instance.knowledge_base_id = 0
        print("content at the end = ")
        print(content['knowledge_base_id'])
        #page_instance.save_revision().publish

So when the hook code fires, it updates the draft with all the info EXCEPT the knowledge_base_id.


However when I change the knowledge_base_id like this (seen commented out above)

page_instance.knowledge_base_id = 0

And save it like this (also seen commented out above)

page_instance.save_revision().publish()

It saves the updated knowledge_base_id BUT skips over the other revisions. In short, what the heck am I doing wrong. Thanks in advance for the assist. Take care and have a good day.

Upvotes: 1

Views: 1742

Answers (1)

JeremyG
JeremyG

Reputation: 179

So I figured out the problem. Instead of trying to use the Page method save_revisions(), I opted to use revisions.create(). Inside revisions.create(), you pass it a JSON object with your updated values. In addition to that you pass an instance of the user, and values for submitted_for_moderation and approved_go_live_at. Listed below is my updated code sample, with comments. Please let me know if you have any questions for me. I hope this post helps others avoid frustrations with updating revisions. Thanks for reading. Take care and have a good day.

from wagtail.wagtailcore import hooks
from .models import ContentPage
import json


# Allows the latest page revision JSON to be updated based on conditionals
def sync_kb_page_with_zendesk(request, page):

    # Sanity check to ensure page is an instance of ContentPage
    if isinstance(page, ContentPage):

        # this sets the user variable
        user_var = request.user 

        # sets the Content Page
        page_instance = ContentPage.objects.get(pk=page.pk) 

        # this retrieves JSON str w/ latest revisions
        pageJsonHolder = page_instance.get_latest_revision().content_json 

        # this takes the json string and converts it into a json object
        content = json.loads(pageJsonHolder) 

        # this sets the kb id variable for use in the code
        kb_id_revision = content['knowledge_base_id'] 

        # this sets the kb active variable for use in the code
        kb_active_revision = content['kb_active'] 

        # this is the conditional logic 
        if kb_active_revision == "T":
            if kb_id_revision == 0:
                print("create the article")
                # updates the kb id value in the JSON object
                content['knowledge_base_id'] = 456 
            else:
                print("update the article")
        elif kb_id_revision != 0:
            print("delete the article")
            # updates the kb id value in the JSON object
            content['knowledge_base_id'] = 0 

        # this takes the JSON object and coverts it back to a JSON str
        revisionPageJsonHolder = json.dumps(content) 

        # this takes your JSON str and creates the latest revision of Page 
        revision = page_instance.revisions.create(
            content_json=revisionPageJsonHolder,
            user=user_var,
            submitted_for_moderation=False,
            approved_go_live_at=None,
        ) 


# registers the function to fire after page edit
hooks.register('after_edit_page', sync_kb_page_with_zendesk) 

# registers the function to fire after page creation
hooks.register('after_create_page', sync_kb_page_with_zendesk)

Upvotes: 1

Related Questions