Revision 8b01f7f3 api/util.py
b/api/util.py | ||
---|---|---|
6 | 6 |
from functools import wraps |
7 | 7 |
from random import choice |
8 | 8 |
from string import ascii_letters, digits |
9 |
from time import time |
|
9 | 10 |
from traceback import format_exc |
11 |
from wsgiref.handlers import format_date_time |
|
10 | 12 |
|
11 | 13 |
from django.conf import settings |
12 | 14 |
from django.http import HttpResponse |
... | ... | |
122 | 124 |
raise BadRequest('Unsupported Content-Type.') |
123 | 125 |
|
124 | 126 |
|
127 |
def update_response_headers(request, response): |
|
128 |
if request.serialization == 'xml': |
|
129 |
response['Content-Type'] = 'application/xml' |
|
130 |
elif request.serialization == 'atom': |
|
131 |
response['Content-Type'] = 'application/atom+xml' |
|
132 |
else: |
|
133 |
response['Content-Type'] = 'application/json' |
|
134 |
|
|
135 |
response['Date'] = format_date_time(time()) |
|
136 |
|
|
125 | 137 |
def render_metadata(request, metadata, use_values=False, status=200): |
126 | 138 |
if request.serialization == 'xml': |
127 | 139 |
data = render_to_string('metadata.xml', {'metadata': metadata}) |
... | ... | |
148 | 160 |
data = json.dumps(d) |
149 | 161 |
|
150 | 162 |
resp = HttpResponse(data, status=fault.code) |
151 |
|
|
152 |
if request.serialization == 'xml': |
|
153 |
resp['Content-Type'] = 'application/xml' |
|
154 |
elif request.serialization == 'atom': |
|
155 |
resp['Content-Type'] = 'application/atom+xml' |
|
156 |
else: |
|
157 |
resp['Content-Type'] = 'application/json' |
|
158 |
|
|
163 |
update_response_headers(request, resp) |
|
159 | 164 |
return resp |
160 | 165 |
|
161 | 166 |
|
... | ... | |
197 | 202 |
raise BadRequest('Method not allowed.') |
198 | 203 |
|
199 | 204 |
resp = func(request, *args, **kwargs) |
200 |
if request.serialization == 'xml': |
|
201 |
resp['Content-Type'] = 'application/xml' |
|
202 |
elif request.serialization == 'atom': |
|
203 |
resp['Content-Type'] = 'application/atom+xml' |
|
204 |
else: |
|
205 |
resp['Content-Type'] = 'application/json' |
|
206 |
|
|
205 |
update_response_headers(request, resp) |
|
207 | 206 |
return resp |
208 | 207 |
|
209 | 208 |
except Fault, fault: |
Also available in: Unified diff