Skip to content

Commit

Permalink
Merge pull request django-cms#2807 from digi604/fix-2788
Browse files Browse the repository at this point in the history
limit detection of model changes
  • Loading branch information
digi604 committed Mar 5, 2014
2 parents d457b84 + 411d290 commit 19aa151
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 24 deletions.
21 changes: 10 additions & 11 deletions cms/admin/pageadmin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from functools import wraps
import sys
from cms.toolbar_pool import toolbar_pool
from cms.constants import PAGE_TYPES_ID

import django
Expand Down Expand Up @@ -1244,25 +1245,23 @@ def resolve(self, request):
log = LogEntry.objects.get(pk=request.session['cms_log_latest'])
obj = log.get_edited_object()
del request.session['cms_log_latest']
try:
return HttpResponse(force_unicode(obj.get_absolute_url()))
except:
pass
if obj.__class__ in toolbar_pool.get_watch_models() and hasattr(obj, 'get_absolute_url'):
try:
return HttpResponse(force_unicode(obj.get_absolute_url()))
except:
pass
pk = request.REQUEST.get('pk')

try:
app_label, model = request.REQUEST.get('model').split('.')
full_model = request.REQUEST.get('model')
if pk and full_model:
app_label, model = full_model.split('.')
if pk and app_label:
ctype = ContentType.objects.get(app_label=app_label, model=model)
try:
instance = ctype.get_object_for_this_type(pk=pk)
except ctype.model_class().DoesNotExist:
return HttpResponse('/')
return HttpResponse(force_unicode(instance.get_absolute_url()))
except ValueError:
pass

return HttpResponse('/')
return HttpResponse('')

def lookup_allowed(self, key, *args, **kwargs):
if key == 'site__exact':
Expand Down
4 changes: 2 additions & 2 deletions cms/cms_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from cms.compat import user_model_label
from cms.constants import TEMPLATE_INHERITANCE_MAGIC
from cms.exceptions import LanguageError
from cms.models import Title
from cms.models import Title, Page
from cms.toolbar.items import TemplateItem
from cms.toolbar_base import CMSToolbar
from cms.toolbar_pool import toolbar_pool
Expand Down Expand Up @@ -150,7 +150,7 @@ def add_language_menu(self):

@toolbar_pool.register
class PageToolbar(CMSToolbar):
model = Title
watch_models = [Page]

def populate(self):
# always use draft if we have a page
Expand Down
11 changes: 7 additions & 4 deletions cms/static/cms/js/modules/cms.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ $(document).ready(function () {
$.ajax({
'async': false,
'type': 'GET',
'url': CMS.config.reload.url,
'url': CMS.config.request.url,
'data': {
'model': CMS.config.reload.model,
'pk': CMS.config.reload.pk
'model': CMS.config.request.model,
'pk': CMS.config.request.pk
},
'success': function (response) {
if(parent.location.pathname !== response) {
if(response === '' && !url) {
// cancel if response is empty
return false;
} else if(parent.location.pathname !== response) {
// api call to the backend to check if the current path is still the same
that.reloadBrowser(response);
} else if(url === 'REFRESH_PAGE') {
Expand Down
10 changes: 4 additions & 6 deletions cms/templates/cms/toolbar/toolbar_javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
'csrf': '{{ csrf_token }}',
'request': {
'language': '{{ request.GET.language }}',
'page_id': '{{ request.current_page.pk }}'
'model': '{{ request.toolbar.get_object_model }}',
'page_id': '{{ request.current_page.pk }}',
'pk': '{{ request.toolbar.get_object_pk }}',
'url': '{% language request.toolbar.language %}{% url "admin:cms_page_resolve" %}{% endlanguage %}'
},
'lang': {
'debug': '{% blocktrans %}Development version using django CMS {{ cms_version }}{% endblocktrans %}',
Expand All @@ -57,11 +60,6 @@
'id': '{{ request.toolbar.clipboard.pk|unlocalize }}',
'url': '{% if request.toolbar.clipboard.pk %}{% url "admin:cms_page_clear_placeholder" request.toolbar.clipboard.pk %}{% endif %}'
},
'reload': {
'url': '{% url "admin:cms_page_resolve" %}',
'model': '{{ request.toolbar.get_object_model }}',
'pk': '{{ request.toolbar.get_object_pk }}'
},
'messages': '{% if messages %}{% for message in messages %}{{ message }}{% endfor %}{% endif %}',
'error': '{% if request.toolbar.login_form.errors %}{% blocktrans %}<strong>Login failed.</strong> Please check your credentials and try again.{% endblocktrans %}{% endif %}',
'publisher': '{% if not request.current_page.publisher_is_draft and request.current_page.publisher_draft.is_dirty and user.is_authenticated %}{% trans "This page has unpublished changes." %}{% endif %}'
Expand Down
2 changes: 1 addition & 1 deletion cms/tests/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def test_page_create_redirect(self):
resolve_url = reverse('admin:cms_page_resolve')
with self.login_user_context(superuser):
response = self.client.post(resolve_url, {'pk': '', 'model': 'cms.page'})
self.assertEqual(response.content.decode('utf-8'), '/')
self.assertEqual(response.content.decode('utf-8'), '')
page_data = self.get_new_page_data()
self.client.post(URL_CMS_PAGE_ADD, page_data)

Expand Down
8 changes: 8 additions & 0 deletions cms/toolbar_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ def get_toolbars(self):
self.discover_toolbars()
return self.toolbars

def get_watch_models(self):
models = []
for toolbar in self.toolbars.values():
if hasattr(toolbar, 'watch_models'):
models += toolbar.watch_models
return models


toolbar_pool = ToolbarPool()
16 changes: 16 additions & 0 deletions docs/extending_cms/toolbar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,19 @@ Example::



If you want to watch for object creation or editing of models and redirect after they have been added or changed add a
``watch_models`` attribute to your toolbar.

Example::

class PollToolbar(CMSToolbar):

watch_models = [Poll]

def populate(self):
...

After you add this every change to an instance of ``Poll`` via sideframe or modal window will trigger a redirect to
the ``get_absolute_url()`` of the poll instance that was edited.


0 comments on commit 19aa151

Please sign in to comment.