Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] create_page_content API adding two cms_pagecontent records instead of one #7796

Open
1 task
jamesfogg opened this issue Jan 29, 2024 · 3 comments
Open
1 task
Assignees

Comments

@jamesfogg
Copy link

jamesfogg commented Jan 29, 2024

For a page created manually from Admin, can click into page settings.

For any pages that have been created using the API, getting this error when trying to then click into settings.

MultipleObjectsReturned at /en/admin/cms/pagecontent/158/change/
get() returned more than one PageUrl -- it returned 2!

Looking at the pagecontent table in the dB.
When a page is created manually - one pageholder record is created with template type = INHERIT
When a page, and then pagecontent is created using the API. There are two records -
One which has the template as specified in the pagecontent create call.
The second record has the template specified as template = INHERIT

Essentially looks like the API is firing the creation of the requested record and then an additional default placeholder record is also being created which causes the above settings issue. Also looks like 2x dupe cms_pageurl records are being create as a consequence

Steps to reproduce

Manually create a page from admin screen, click on settings - all ok.

Vs.

Create a page/page content using the API as follows:

parent_page = self.get_parent_page(parent_path)
node_parent = parent_page.node if parent_page else None
draft_page = Page.objects.filter(pagecontent_set__title=page_title, node__parent=node_parent).first()

    main_page = draft_page if draft_page else create_page(page_title, template, language, parent=parent_page, created_by=james_user)

    page_content = create_page_content(
        language=language,
        title=page_title,
        page=main_page,
        slug=main_page.get_slug(language),
        created_by=james_user
    )

    placeholders = Placeholder.objects.get_for_obj(page_content)
    
    header_content = placeholders.filter(slot="header_content").get()
    body_content = placeholders.filter(slot="body_content").get()
    scripts_content = placeholders.filter(slot="scripts_content").get()

    add_plugin(
        placeholder = header_content,
        plugin_type = GetSetJSON,
        language = language,
        content = json.dumps(plugin_data),
        action = 'set_dictionary'
    )

    add_plugin(
        placeholder = body_content,
        plugin_type = MasterPlugin,
        language = language
    )

    if page_content.versions.first():
        page_content.versions.first().publish(james_user)

Expected behaviour

One cms_pagecontent record to be created when page created via API

Actual behaviour

Two cms_pagecontent records created - one with template specified in the API call, the other with template type = INHERIT

Screenshots

N/A

311 / 4.1

Do you want to help fix this issue?

  • Yes, I want to help fix this issue and I will join #workgroup-pr-review on Slack to confirm with the community that a PR is welcome.
  • [ X] No, I only want to report the issue.
@fsbraun fsbraun self-assigned this Jan 29, 2024
@fsbraun
Copy link
Sponsor Member

fsbraun commented Jan 29, 2024

Two PageContent objects AND two PageURL objects?

@jamesfogg
Copy link
Author

Yes extra records in both, a work around to get to the settings after page creation is to delete one instance of the PageURL instance - as this is what is causing the error (and records are 100% identical).

dupe_pageurl_record = PageUrl.objects.filter(page_id=main_page.pk,language=language).first()
dupe_pageurl_record.delete()

When deleted like this, can access the settings page as expected. There are differences on the two different PageContnet records though.

@fsbraun
Copy link
Sponsor Member

fsbraun commented Jan 30, 2024

@jamesfogg OK, after looking at your code, I think I know the reason:

  • create_page already creates a PageContent object
  • You need to create or get the page. Important: do not forget to provide a created_by argument.
  • Instead of create_pagecontent you'll have to get the existing content by using page.pagecontent_set(manager="admin_manager").latest_content() Only if this should be empty, create a page content object manually.
  • From there you get the placeholder.

I expect, if you follow this path, you will only have one page url and only one page content object.

My take-away is that create_pagecontent needs to check if there already is a content object before creating a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants