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

fix prefetch error when current model has a m2m relation to grouper f… #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion djangocms_versioning/monkeypatch/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def _copy_title_extensions(self, source_page, target_page, language, clone=False):
"""
djangocms-cms/extensions/admin.py, last changed in: divio/django-cms@2894ae8
djangocms-cms/extensions/admin.py, last changed in: django-cms/django-cms@2894ae8

The existing method ExtensionPool._copy_title_extensions will only ever
get published versions, we change the queries to get the latest draft version
Expand Down
32 changes: 28 additions & 4 deletions djangocms_versioning/plugin_rendering.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.db import models

from cms.plugin_rendering import ContentRenderer, StructureRenderer
from cms.utils.placeholder import rescan_placeholders_for_obj

Expand All @@ -11,6 +13,7 @@ def prefetch_versioned_related_objects(instance, toolbar):
candidate_fields = [
f for f in instance._meta.get_fields() if f.is_relation and not f.auto_created
]

for field in candidate_fields:
try:
versionable = versionables.for_grouper(field.remote_field.model)
Expand All @@ -24,14 +27,35 @@ def prefetch_versioned_related_objects(instance, toolbar):
qs = versionable.content_model.objects.all()
related_field = getattr(instance, field.name)
if related_field:
filters = {versionable.grouper_field_name: related_field}
is_related_manager = isinstance(related_field, models.Manager)

if is_related_manager:
filter_key = '{}__in'.format(versionable.grouper_field_name)
filter_values = related_field.get_queryset()
filters = {filter_key: filter_values}
else:
filters = {versionable.grouper_field_name: related_field}
# TODO Figure out grouping values-awareness
# for extra fields other than hardcoded 'language'
if "language" in versionable.extra_grouping_fields:
filters["language"] = toolbar.request_language
qs = qs.filter(**filters)
prefetch_cache = {versionable.grouper_field.remote_field.name: qs}
related_field._prefetched_objects_cache = prefetch_cache
try:
qs = qs.filter(**filters).order_by(versionable.grouper_field_name).distinct(versionable.grouper_field_name)
except:
# FIXME: there will be error caused by djangocms-internalsearch
# Cannot use QuerySet for "content model": Use a QuerySet for "grouper model"
# will catch the error here to avoid page corruption.
pass

# TODO refine it after understand prefetch in many2many field.
# because if `related_field` is ManyRelatedManager, it is temporary,
# we can't store `_prefetched_objectes_cache` to it, so I decide to store the
# prefetched value to model instance.
if is_related_manager:
instance._prefetched_objects_cache = getattr(instance, '_prefetched_objects_cache', {})
instance._prefetched_objects_cache[field.name] = qs
else:
related_field._prefetched_objects_cache = {versionable.grouper_field.remote_field.name: qs}


class VersionContentRenderer(ContentRenderer):
Expand Down
10 changes: 9 additions & 1 deletion djangocms_versioning/test_utils/polls/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool

from .models import PollPlugin as Poll
from .models import PollManyPlugin as PollMany, PollPlugin as Poll


@plugin_pool.register_plugin
Expand All @@ -10,3 +10,11 @@ class PollPlugin(CMSPluginBase):
name = "Poll"
allow_children = True
render_template = "polls/poll.html"


@plugin_pool.register_plugin
class PollManyPlugin(CMSPluginBase):
model = PollMany
name = "PollMany"
allow_children = True
render_template = "polls/polls.html"
7 changes: 7 additions & 0 deletions djangocms_versioning/test_utils/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ class PollPlugin(CMSPlugin):

def __str__(self):
return str(self.poll)


class PollManyPlugin(CMSPlugin):
polls = models.ManyToManyField(Poll, blank=True)

def __str__(self):
return ''
Empty file.
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

INSTALL_REQUIREMENTS = [
"Django>=1.11,<3.0",
"django-cms",
"django-cms<4.1.0",
"django-fsm>=2.6,<2.7"
]

Expand Down Expand Up @@ -38,12 +38,12 @@
author="Divio AG",
test_suite="test_settings.run",
author_email="[email protected]",
url="http://github.com/divio/djangocms-versioning",
url="http://github.com/django-cms/djangocms-versioning",
license="BSD",
zip_safe=False,
tests_require=TEST_REQUIREMENTS,
dependency_links=[
"http://github.com/divio/django-cms/tarball/release/4.0.x#egg=django-cms-4.0.0",
"https://github.com/divio/djangocms-text-ckeditor/tarball/support/4.0.x#egg=djangocms-text-ckeditor-4.0.x",
]
# dependency_links=[
# "https://github.com/django-cms/django-cms/tarball/release/4.0.x#egg=django-cms",
# "https://github.com/django-cms/djangocms-text-ckeditor/tarball/support/4.0.x#egg=djangocms-text-ckeditor",
# ]
)
3 changes: 2 additions & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ beautifulsoup4
django-classy-tags<2.0.0
django-sekizai<2.0.0

https://github.com/django-cms/django-cms/tarball/release/4.0.x#egg=django-cms
# Get the lastest cms v4 compatible djangocms-text-ckeditor
https://github.com/divio/djangocms-text-ckeditor/tarball/support/4.0.x#egg=djangocms-text-ckeditor
https://github.com/django-cms/djangocms-text-ckeditor/tarball/support/4.0.x#egg=djangocms-text-ckeditor
2 changes: 1 addition & 1 deletion tests/test_monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_copy_extensions(self):
)
# No asserts, this test originally failed because the versioned manager was called
# in copy_extensions, now we call the original manager instead
# https://github.com/divio/djangocms-versioning/pull/201/files#diff-fc33dd7b5aa9b1645545cf48dfc9b4ecR19
# https://github.com/django-cms/djangocms-versioning/pull/201/files#diff-fc33dd7b5aa9b1645545cf48dfc9b4ecR19


class MonkeypatchTestCase(CMSTestCase):
Expand Down
70 changes: 70 additions & 0 deletions tests/test_plugin_rendering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from cms.api import add_plugin
from cms.test_utils.testcases import CMSTestCase
from cms.toolbar.toolbar import CMSToolbar

from djangocms_versioning.plugin_rendering import VersionContentRenderer
from djangocms_versioning.test_utils.factories import (
PageVersionFactory,
PlaceholderFactory,
PollVersionFactory,
)


class MonkeypatchTestCase(CMSTestCase):
def test_prefetch_versioned_m2m_objects(self):
'''
test model with manytomany field to grouper model
'''
request = self.get_request('/')
toolbar = CMSToolbar(request)
toolbar.edit_mode_active = True
request.toolbar = toolbar
context = {"request": request}
contentRenderer = VersionContentRenderer(request)

version = PageVersionFactory()
placeholder = PlaceholderFactory(source=version.content)
poll_version = PollVersionFactory(content__language='en')
poll = poll_version.content.poll

plugin = add_plugin(
placeholder, "PollManyPlugin", version.content.language
)

plugin.polls.add(poll)
plugin._prefetched_objects_cache = {}

with self.assertNumQueries(1):
contentRenderer.render_plugin(plugin, context)
for i in range(1000):
self.assertEqual(1, len(plugin.polls.all()))

with self.assertNumQueries(0):
self.assertEqual(1, len(plugin.polls.all()))

self.assertIsNotNone(plugin._prefetched_objects_cache)
self.assertIsNotNone(plugin._prefetched_objects_cache['polls'])
self.assertEqual(len(plugin._prefetched_objects_cache['polls']), 1)

def test_prefetch_versioned_m2o_objects(self):
'''
#test model with one foreign key to grouper model
'''
request = self.get_request('/')
toolbar = CMSToolbar(request)
toolbar.edit_mode_active = True
request.toolbar = toolbar
context = {"request": request}
contentRenderer = VersionContentRenderer(request)

version = PageVersionFactory()
placeholder = PlaceholderFactory(source=version.content)
poll_version = PollVersionFactory(content__language='en')
poll = poll_version.content.poll

plugin = add_plugin(
placeholder, "PollPlugin", version.content.language, poll=poll
)

contentRenderer.render_plugin(plugin, context)
self.assertIsNotNone(plugin.poll._prefetched_objects_cache)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ deps =
dj21: Django>=2.1,<2.2
dj22: Django>=2.2,<3.0

cms40: https://github.com/divio/django-cms/archive/release/4.0.x.zip
cms40: https://github.com/django-cms/django-cms/archive/release/4.0.x.zip

basepython =
py35: python3.5
Expand Down