Created
July 26, 2016 20:36
-
-
Save victorono/d904f116e8a63cc9c010c85d01bc1967 to your computer and use it in GitHub Desktop.
messages app django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
import json | |
from django.contrib import messages | |
class AjaxMessaging(object): | |
def process_response(self, request, response): | |
if request.is_ajax(): | |
if response['Content-Type'] in ["application/javascript", "application/json"]: | |
try: | |
content = json.loads(response.content) | |
except ValueError: | |
return response | |
django_messages = [] | |
for message in messages.get_messages(request): | |
django_messages.append({ | |
"level": message.level, | |
"message": message.message, | |
"extra_tags": message.tags, | |
}) | |
content['messages'] = django_messages | |
response.content = json.dumps(content) | |
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (context) { | |
$.ajaxSetup({ | |
complete: function (xhr) { | |
var contentType = xhr.getResponseHeader("Content-Type"), | |
json = null; | |
if (contentType === "application/json" || contentType === "text/json") { | |
try { | |
json = $.parseJSON(xhr.responseText); | |
$.each(json.messages, function (index) { | |
if (json.messages[index].extra_tags === "success") { | |
alertify.success(json.messages[index].message); | |
} else if (json.messages[index].extra_tags === "error") { | |
alertify.error(json.messages[index].message); | |
} | |
}); | |
} catch (err) { | |
return true; | |
} | |
} | |
}, | |
error: function () { | |
alertify.error(gettext("Ha ocurrido un error"), "", 6); | |
} | |
}); | |
})(window || this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment