Skip to content

Commit d353a80

Browse files
committed
Added javascript handling of error messages on PageAdmin.change_status
1 parent 73e903c commit d353a80

File tree

3 files changed

+70
-21
lines changed

3 files changed

+70
-21
lines changed

cms/admin/pageadmin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from cms.utils import (copy_plugins, helpers, moderator, permissions, plugins,
1919
get_template_from_request, get_language_from_request,
2020
placeholder as placeholder_utils, admin as admin_utils, cms_static_url)
21+
from cms.utils.admin import HttpJsResponse
22+
from cms.utils.permissions import has_plugin_permission
2123
from copy import deepcopy
2224
from distutils.version import LooseVersion
2325
from django import template
@@ -1063,11 +1065,9 @@ def change_status(self, request, page_id):
10631065
if page.published or is_valid_overwrite_url(page.get_absolute_url(),page,False):
10641066
page.published = not page.published
10651067
page.save()
1066-
return admin_utils.render_admin_menu_item(request, page)
1068+
return HttpJsResponse(admin_utils.render_admin_menu_item(request, page).content,200)
10671069
except ValidationError,e:
1068-
return HttpResponseServerError(json.dumps(e.messages))
1069-
except Exception,e:
1070-
return HttpResponseServerError(json.dumps(e.messages))
1070+
return HttpJsResponse(e.messages,500)
10711071
else:
10721072
return HttpResponseForbidden(unicode(_("You do not have permission to publish this page")))
10731073

cms/static/cms/js/change_list.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,38 @@
223223
if(jtarget.hasClass("publish-checkbox")) {
224224
pageId = jtarget.attr("name").split("status-")[1];
225225
// if I don't put data in the post, django doesn't get it
226-
reloadItem(jtarget, admin_base_url + "cms/page/" + pageId + "/change-status/", { 1:1 });
226+
reloadItem(
227+
jtarget, admin_base_url + "cms/page/" + pageId + "/change-status/",
228+
{ 1:1 },
229+
// on success
230+
function(encoded_response,textStatus){
231+
decoded = $.parseJSON(encoded_response)
232+
response = decoded['body'];
233+
status = decoded['status'];
234+
if(status==200) {
235+
if (/page_\d+/.test($(jtarget).attr('id'))) {
236+
// one level higher
237+
target = $(jtarget).find('div.cont:first');
238+
} else {
239+
target = $(jtarget).parents('div.cont:first');
240+
}
241+
242+
var parent = target.parent();
243+
if (response == "NotFound") {
244+
return parent.remove();
245+
}
246+
target.replace(response);
247+
parent.find('div.cont:first').yft();
248+
249+
return true;
250+
}
251+
else {
252+
$(jtarget).attr("checked",false)
253+
alert(response);
254+
return false;
255+
}
256+
}
257+
);
227258
e.stopPropagation();
228259
return true;
229260
}
@@ -438,21 +469,24 @@
438469
}
439470

440471
function onSuccess(response, textStatus) {
441-
if (callback) callback(response, textStatus);
442-
443-
if (/page_\d+/.test($(el).attr('id'))) {
444-
// one level higher
445-
target = $(el).find('div.cont:first');
446-
} else {
447-
target = $(el).parents('div.cont:first');
448-
}
449-
450-
var parent = target.parent();
451-
if (response == "NotFound") {
452-
return parent.remove();
453-
}
454-
target.replace(response);
455-
parent.find('div.cont:first').yft();
472+
status = true;
473+
if (callback) status = callback(response, textStatus);
474+
475+
if(status==true) {
476+
if (/page_\d+/.test($(el).attr('id'))) {
477+
// one level higher
478+
target = $(el).find('div.cont:first');
479+
} else {
480+
target = $(el).parents('div.cont:first');
481+
}
482+
483+
var parent = target.parent();
484+
if (response == "NotFound") {
485+
return parent.remove();
486+
}
487+
target.replace(response);
488+
parent.find('div.cont:first').yft();
489+
}
456490

457491
return true;
458492
}
@@ -486,11 +520,13 @@
486520
} else {
487521
moveSuccess($('#page_'+item_id + " div.col1:eq(0)"));
488522
}
523+
return true;
489524
},
490525

491526
// on error
492527
function(){
493528
moveError($('#page_'+item_id + " div.col1:eq(0)"));
529+
return false;
494530
}
495531
);
496532
}

cms/utils/admin.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.http import HttpResponse
44
from django.shortcuts import render_to_response
55
from django.template.context import RequestContext
6-
6+
import json
77
from django.contrib.sites.models import Site
88

99
from cms.models import Page
@@ -12,6 +12,19 @@
1212

1313
NOT_FOUND_RESPONSE = "NotFound"
1414

15+
class HttpJsResponse(HttpResponse):
16+
""" Extended HttpResponse type for django-javascript communication.
17+
18+
It wraps text messages returned to javascript functions with a status code for message handling in JS.
19+
Response content is automatically serialized to json.
20+
"""
21+
# this is the status code used by javascript to detect error semantic. Based on HTTP error statuses
22+
js_dj_status = 200
23+
24+
def __init__(self, content, js_dj_status):
25+
content = json.dumps({'status':js_dj_status,'body':content})
26+
super(HttpJsResponse,self).__init__(content,content_type="application/json")
27+
1528

1629
def get_admin_menu_item_context(request, page, filtered=False):
1730
"""

0 commit comments

Comments
 (0)