8000 Moved placeholders from Page to Title model by Aiky30 · Pull Request #6442 · django-cms/django-cms · GitHub
[go: up one dir, main page]

Skip to content

Moved placeholders from Page to Title model #6442

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

Merged
merged 21 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
Prev Previous commit
Next Next commit
Update the failures on the page tests
  • Loading branch information
Andrew Aikman authored and czpython committed Jul 11, 2018
commit c2fa4748c5964f2f28d963b26bdbc90aa1ff104d
1 change: 0 additions & 1 deletion cms/models/pagemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ def _copy_contents(self, target, language):
Copy all the plugins to a new page.
:param target: The page where the new content should be stored
"""

for title in target.title_set.all():
title.copy_placeholders(title, language)

Expand Down
22 changes: 12 additions & 10 deletions cms/plugin_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,12 @@ def render_page_placeholder(self, slot, context, inherit,
current_page = page or self.current_page
placeholder_cache = self._placeholders_by_page_cache

if current_page.pk not in placeholder_cache:
title = current_page.get_title_obj(language=self.request_language)

if title.pk not in placeholder_cache:
# Instead of loading plugins for this one placeholder
# try and load them for all placeholders on the page.
self._preload_placeholders_for_page(current_page)
self._preload_placeholders_for_page(title)

try:
placeholder = placeholder_cache[current_page.pk][slot]
Expand Down Expand Up @@ -482,7 +484,7 @@ def _get_cached_placeholder_content(self, placeholder, language):
language_cache[placeholder.pk] = cached_value
return language_cache.get(placeholder.pk)

def _preload_placeholders_for_page(self, page, slots=None, inherit=False):
def _preload_placeholders_for_page(self, title, slots=None, inherit=False):
"""
Populates the internal plugin cache of each placeholder
in the given page if the placeholder has not been
Expand All @@ -491,10 +493,10 @@ def _preload_placeholders_for_page(self, page, slots=None, inherit=False):
from cms.utils.plugins import assign_plugins

if slots:
placeholders = page.get_placeholders().filter(slot__in=slots)
placeholders = title.get_placeholders().filter(slot__in=slots)
else:
# Creates any placeholders missing on the page
placeholders = page.rescan_placeholders().values()
placeholders = title.rescan_placeholders().values()

if inherit:
# When the inherit flag is True,
Expand All @@ -503,7 +505,7 @@ def _preload_placeholders_for_page(self, page, slots=None, inherit=False):
elif not self.toolbar.edit_mode_active:
# Scan through the page template to find all placeholders
# that have inheritance turned on.
slots_w_inheritance = [pl.slot for pl in page.get_declared_placeholders() if pl.inherit]
slots_w_inheritance = [pl.slot for pl in title.get_declared_placeholders() if pl.inherit]
else:
# Inheritance is turned off on edit-mode
slots_w_inheritance = []
Expand All @@ -524,12 +526,12 @@ def _preload_placeholders_for_page(self, page, slots=None, inherit=False):
assign_plugins(
request=self.request,
placeholders=placeholders_to_fetch,
template=page.get_template(),
template=title.page.get_template(),
lang=self.request_language,
is_fallback=inherit,
)

parent_page = page.parent_page
parent_page = title.page.parent_page
# Inherit only placeholders that have no plugins
# or are not cached.
placeholders_to_inherit = [
Expand All @@ -550,10 +552,10 @@ def _preload_placeholders_for_page(self, page, slots=None, inherit=False):

for placeholder in placeholders:
# Save a query when the placeholder toolbar is rendered.
placeholder.page = page
placeholder.page = title.page
page_placeholder_cache[placeholder.slot] = placeholder

self._placeholders_by_page_cache[page.pk] = page_placeholder_cache
self._placeholders_by_page_cache[title.pk] = page_placeholder_cache


class StructureRenderer(BaseRenderer):
Expand Down
2 changes: 1 addition & 1 deletion cms/sitemaps/cms_sitemap.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def items(self):
def lastmod(self, title):
modification_dates = [title.page.changed_date, title.page.publication_date]
plugins_for_placeholder = lambda placeholder: placeholder.get_plugins()
plugins = from_iterable(map(plugins_for_placeholder, title.page.placeholders.all()))
plugins = from_iterable(map(plugins_for_placeholder, title.placeholders.all()))
plugin_modification_dates = map(lambda plugin: plugin.changed_date, plugins)
modification_dates.extend(plugin_modification_dates)
return max(modification_dates)
Expand Down
23 changes: 14 additions & 9 deletions cms/tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,10 @@ def test_delete_with_plugins(self):
Text = self.get_plugin_model('TextPlugin')
home = create_page("home", "nav_playground.html", "en")
page = create_page("page", "nav_playground.html", "en")
page.rescan_placeholders() # create placeholders
placeholder = page.placeholders.all()[0]
home_title = self.get_page_title(page=home, language="en")
page_title = self.get_page_title(page=page, language="en")
page_title.rescan_placeholders() # create placeholders
placeholder = page_title.placeholders.all()[0]
plugin_base = CMSPlugin(
plugin_type='TextPlugin',
placeholder=placeholder,
Expand All @@ -634,8 +636,8 @@ def test_delete_with_plugins(self):
self.assertTrue(Placeholder.objects.count() > 2)

superuser = self.get_superuser()
home_pl_count = home.get_placeholders().count()
page_pl_count = page.get_placeholders().count()
home_pl_count = home_title.get_placeholders().count()
page_pl_count = page_title.get_placeholders().count()
expected_pl_count = Placeholder.objects.count() - (home_pl_count + page_pl_count)

with self.login_user_context(superuser):
Expand Down Expand Up @@ -889,7 +891,8 @@ def test_plugin_loading_queries(self):
CMS_TEMPLATES=(('placeholder_tests/base.html', 'tpl'), ),
):
page = create_page('home', 'placeholder_tests/base.html', 'en', published=True, slug='home')
placeholders = list(page.placeholders.all())
title = self.get_page_title(page=page, language="en")
placeholders = list(title.placeholders.all())
for i, placeholder in enumerate(placeholders):
for j in range(5):
add_plugin(placeholder, 'TextPlugin', 'en', body='text-%d-%d' % (i, j))
Expand Down Expand Up @@ -1066,8 +1069,9 @@ def test_page_used_on_request(self):
slug='home',
xframe_options=Page.X_FRAME_OPTIONS_DENY
)
placeholder = cms_page.placeholders.all()[0]
add_plugin(cms_page.placeholders.all()[0], 'TextPlugin', 'en', body=public_text)
title = self.get_page_title(page=cms_page, language="en")
placeholder = title.placeholders.all()[0]
add_plugin(title.placeholders.all()[0], 'TextPlugin', 'en', body=public_text)
cms_page.publish('en')
add_plugin(placeholder, 'TextPlugin', 'en', body=draft_text)
endpoint = cms_page.get_absolute_url('en')
Expand Down Expand Up @@ -1130,8 +1134,9 @@ def test_page_preview_persists(self):
slug='home',
xframe_options=Page.X_FRAME_OPTIONS_DENY
)
placeholder = cms_page.placeholders.all()[0]
add_plugin(cms_page.placeholders.all()[0], 'TextPlugin', 'en', body=public_text)
title = self.get_page_title(page=cms_page, language="en")
placeholder = title.placeholders.all()[0]
add_plugin(title.placeholders.all()[0], 'TextPlugin', 'en', body=public_text)
cms_page.publish('en')
add_plugin(placeholder, 'TextPlugin', 'en', body=draft_text)
endpoint = cms_page.get_absolute_url('en')
Expand Down
0