2015-03-31 05:53:16 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
# python
|
2014-04-02 13:55:24 +00:00
|
|
|
import json
|
2015-03-31 05:53:16 +00:00
|
|
|
|
|
|
|
# django
|
2014-03-28 14:48:05 +00:00
|
|
|
from django.views.generic import View as DjangoView
|
2014-03-31 14:39:37 +00:00
|
|
|
from django.template import RequestContext
|
|
|
|
from django.shortcuts import render_to_response
|
2014-04-02 13:55:24 +00:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.contrib import messages
|
2015-03-31 05:53:16 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2014-03-28 14:48:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class View(DjangoView):
|
|
|
|
section = None
|
|
|
|
|
|
|
|
def get_context(self, context):
|
|
|
|
if self.section:
|
|
|
|
context['navigation'] = {
|
|
|
|
'section': self.section
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
2014-03-31 14:39:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MessagesView(View):
|
|
|
|
template = 'contrib/messages.html'
|
2014-04-02 13:55:24 +00:00
|
|
|
formats = ['html', 'json']
|
2014-03-31 14:39:37 +00:00
|
|
|
|
|
|
|
def get(self, request):
|
2014-04-02 13:55:24 +00:00
|
|
|
format = 'html'
|
|
|
|
if 'format' in request.GET and request.GET['format'] in self.formats:
|
|
|
|
format = request.GET['format']
|
|
|
|
|
|
|
|
if format == 'html':
|
|
|
|
ctx = RequestContext(request, {})
|
|
|
|
result = render_to_response(self.template, context_instance=ctx)
|
|
|
|
elif format == 'json':
|
|
|
|
messages_json = []
|
|
|
|
for message in messages.get_messages(request):
|
|
|
|
messages_json.append(
|
|
|
|
{
|
|
|
|
"level": message.level,
|
|
|
|
"message": message.message,
|
|
|
|
"extra_tags": message.tags,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
result = HttpResponse(
|
|
|
|
json.dumps(messages_json),
|
|
|
|
content_type='application/json'
|
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
2015-03-31 05:53:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BlockedView(View):
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
raise PermissionDenied
|