【问题标题】:How to update a Wagtail Page如何更新鹡鸰页面
【发布时间】:2018-01-26 01:08:20
【问题描述】:

我正在使用 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

因此,当挂钩代码触发时,它会使用除知识库 ID 之外的所有信息来更新草稿。


但是,当我像这样更改 Knowledge_base_id 时(见上面的注释)

page_instance.knowledge_base_id = 0

并像这样保存(也可以在上面看到)

page_instance.save_revision().publish()

它保存更新的知识库 ID 但跳过其他修订。简而言之,我到底做错了什么。提前感谢您的帮助。保重,祝您有美好的一天。

【问题讨论】:

  • 您将此函数附加到哪个钩子上?
  • hooks.register('after_edit_page', sync_knowledge_base_page_with_zendesk2) hooks.register('after_create_page', sync_knowledge_base_page_with_zendesk2)
  • 恐怕我无法从这段代码 sn-p 中提取 Wagtail 特定的细节 - 看起来这段代码中发生了很多事情(测试 kb_active_revision,提到 API 调用...... ) 这与您的问题没有直接关系。请您将其提炼成一个最小、完整且可验证的示例 (stackoverflow.com/help/mcve),好吗?
  • 我想获取一个 JSON 对象,其中包含来自最新页面修订的信息。幸运的是,这部分已经完成。根据条件语句的结果,我想更改其中一个键的值之一。这部分也完成了。我现在有一个包含所有正确值的 JSON 对象。所以@gasman,我如何将这个更新的 JSON 对象保存为最新版本,所以在页面刷新后,值会显示在浏览器中?我尝试将 JSON 传递给 save_revisions() 方法并遇到问题。使用 save_revisions.publish() 将键值保存到模型而不是最后一个修订版。

标签: python django wagtail


【解决方案1】:

所以我发现了问题所在。我没有尝试使用 Page 方法 save_revisions(),而是选择使用 revision.create()。在 revisions.create() 中,您将一个带有更新值的 JSON 对象传递给它。除此之外,您还传递了一个用户实例,以及提交的_for_moderation 和approved_go_live_at 的值。下面列出的是我更新的代码示例,带有 cmets。如果您对我有任何问题,请告诉我。我希望这篇文章可以帮助其他人避免在更新修订时遇到挫折。谢谢阅读。保重,有一个美好的一天。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多