-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_util.py
54 lines (37 loc) · 1.38 KB
/
api_util.py
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
# coding=utf8
"""
api util. rely on Django.
"""
__author__ = 'didicout <[email protected]>'
from django.http import HttpResponse
from django.http import HttpResponseBadRequest
import json
def render_json_success(data, sort_keys=False):
result = dict()
result['success'] = True
result['code'] = 0
result['message'] = 'success'
result['data'] = data
return HttpResponse(json.dumps(result, sort_keys=sort_keys, indent=4),
content_type='application/json')
def render_json_p(data, call_back, sort_keys=False):
result = dict()
result['success'] = True
result['code'] = 0
result['message'] = 'success'
result['data'] = data
json_str = json.dumps(result, sort_keys=sort_keys, indent=4)
return HttpResponse("%s(%s)" % (call_back, json_str), content_type='text/javascript; charset=utf-8')
def render_json_failure(message, code=1):
result = dict()
result['success'] = False
result['code'] = code
result['message'] = message
return HttpResponseBadRequest(json.dumps(result, sort_keys=False),
content_type='application/json')
def render_text(message):
return HttpResponse(message, content_type='text/plain')
def render_text_400(message):
return HttpResponseBadRequest(message, content_type='text/plain')
def render_html(html):
return HttpResponse(html, content_type='text/html')