Skip to content

Commit 9d57d9c

Browse files
committed
Add method that allows to monkeypatch how models are filtered
1 parent ed696b3 commit 9d57d9c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

cms/utils/helpers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,19 @@ def get_admin_model_object_by_id(model_class, obj_id):
127127
should be visible to the admin.
128128
"""
129129
return model_class.objects.get(pk=obj_id)
130+
131+
132+
def filter_admin_model(model_class, **kwargs):
133+
"""
134+
A method to filter a Model with keyword arguments.
135+
136+
3rd party applications may change the queryset by the use of monkey-patching.
137+
djangocms-versioning is a prime example of this. By having this helper 3rd
138+
party applications can monkeypatch this helper to ensure correct results
139+
without monkeypatching entire functions of django-cms or through adding
140+
their logic directly to django-cms.
141+
142+
This helper can be reused by applications that wish to get a model that
143+
should be visible to the admin.
144+
"""
145+
return model_class.filter(**kwargs)

cms/views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
from cms.cache.page import get_page_cache
1919
from cms.exceptions import LanguageError
2020
from cms.forms.login import CMSToolbarLoginForm
21-
from cms.models.pagemodel import TreeNode
21+
from cms.models import PageContent, TreeNode
2222
from cms.page_rendering import _handle_no_page, _render_welcome_page, render_pagecontent
2323
from cms.toolbar.utils import get_toolbar_from_request
2424
from cms.utils import get_current_site
2525
from cms.utils.conf import get_cms_setting
26+
from cms.utils import helpers
2627
from cms.utils.helpers import is_editable_model
2728
from cms.utils.i18n import (get_fallback_languages, get_public_languages,
2829
get_redirect_on_fallback, get_language_list,
@@ -168,6 +169,9 @@ def details(request, slug):
168169
# use the page object with populated cache
169170
content.page = page
170171
if hasattr(request, 'toolbar'):
172+
content = helpers.filter_admin_model(
173+
PageContent, page_id=page.pk, language=request_language
174+
).first()
171175
request.toolbar.set_object(content)
172176

173177
return render_pagecontent(request, content)

0 commit comments

Comments
 (0)