如何更新Wagtail页面

时间:2018-01-24 21:37:07

标签: python django wagtail

我正在使用wagtail_hook更新Page对象并遇到麻烦。更具体地说,当最终用户点击"保存草稿"浏览器中的按钮我希望以下代码触发。此代码的目的是根据下面列出的条件语句的结果动态更改knowledge_base_id。

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

因此,当钩子代码触发时,它会使用除了knowledge_base_id之外的所有信息更新草稿。

然而,当我像这样更改knowledge_base_id时(见上文已注释)

page_instance.knowledge_base_id = 0

并保存(如上所述)

page_instance.save_revision().publish()

它会保存更新后的knowledge_base_id,但会跳过其他修订版。简而言之,我做错了什么。在此先感谢您的帮助。小心,祝你有个美好的一天。

1 个答案:

答案 0 :(得分:0)

所以我想出了问题所在。我没有尝试使用Page方法save_revisions(),而是选择使用revisions.create()。在revisions.create()中,您使用更新的值传递JSON对象。除此之外,您还传递了用户的实例,以及submitted_for_moderation和approved_go_live_at的值。下面列出的是我更新的代码示例,带有注释。如果您对我有任何疑问,请与我们联系。我希望这篇文章可以帮助其他人避免更新修订版本的挫败感。谢谢阅读。保重,祝你有个美好的一天。

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)