1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 | # Author: limodou@gmail.com
# version: 0.2
# Format the request and response from/to json and other format
#
# Update:
# 0.1 support json format
# 0.2 support xmlrpc, html format
#
from django.http import HttpResponse
from utils.ajax import json_response
from django.conf import settings
class FormatMiddleware(object):
def process_request(self, request):
format_string = getattr(settings, 'FORMAT_STRING', 'format')
format = request.GET.get(format_string, '')
if format:
request.format = format.lower() #could be "json", "xmlrpc", etc
else:
request.format = getattr(settings, 'DEFAULT_FORMAT', 'json')
if request.format == 'xmlrpc':
import xmlrpclib
p, u = xmlrpclib.getparser()
p.feed(request.raw_post_data)
p.close()
args = u.close()
if len(args) > 0:
args = args[0]
if not isinstance(args, dict):
xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % 'Arguments should be a dict'), methodresponse=1)
return HttpResponse(xml, mimetype='text/xml; charset=utf-8')
old = request.POST._mutable
request.POST._mutable = True
for k, v in args.items():
request.POST[k] = v
request.POST._mutable = old
def process_response(self, request, response):
if isinstance(response, HttpResponse):
return response
elif request.format == 'json':
return json_response(response)
elif request.format == 'xmlrpc':
import xmlrpclib
try:
xml = xmlrpclib.dumps((response,), methodresponse=1)
except Exception, e:
xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % e), methodresponse=1)
return HttpResponse(xml, mimetype='text/xml; charset=utf-8')
elif request.format == 'html':
if hasattr(request, 'format_processor'):
return request.format_processor(response)
else:
return HttpResponse(response)
raise Exception, 'Not support this format [%s]' % request.format
|
Comments