-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
82 lines (72 loc) · 2.21 KB
/
Copy pathviews.py
File metadata and controls
82 lines (72 loc) · 2.21 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from django.shortcuts import render
from django.http import HttpResponse
import re, json
from . import apiList as apiList
def indexGET(request):
# resStr = """
# request.path: {}
# request.body: {}
# request.content_type: {}
# request.method: {}
# request.META['HTTP_HOST']: {}
# """.format(
# request.path,
# request.body,
# request.content_type,
# request.method,
# request.META['HTTP_HOST']
# )
rtnRes = HttpResponse()
# #------------------------------------
# # force status
# rtnRes.__init__(status= 405)
# #-------------------------------------
# # write header
# rtnRes.__setitem__('memo', 'Go home kid')
#-------------------------------------
# deal with GET
if request.method == 'GET':
# 刪掉網址中的 /api/
preRe= re.compile('^/api/')
apiName = preRe.sub('', request.path)
#playData
parameterGET = dict(request.GET)
# http://localhost:8000/api/index?aaa=b;hhh=t
# {'aaa': ['b'], 'hhh': ['t']}
for eachP in parameterGET:
if len(parameterGET[eachP])==1:
parameterGET[eachP] = parameterGET[eachP][0]
data = ''
try:
data = getattr(apiList, apiName)(parameterGET)
if data['status']:
data = json.dumps(data['data'])
else:
data = data['err']
except:
data = '沒有這支 api'
# http://localhost:8000/api/playData?name=Charmander
else:
data='你只能用GET'
rtnRes.write(data)
return rtnRes
def indexPOST(request):
rtnRes = HttpResponse()
# #-------------------------------------
# # deal with POST NEED TO DISABLE CSRF
if request.method == 'POST':
if request.content_type == 'application/json':
parameterPOST = json.loads( str(request.body, encoding='utf8') )
try:
apiName = parameterPOST['api']
data = getattr(apiList, apiName)(parameterPOST)
except:
data = "api 為必要欄位"
else:
data = "如用 GET 請加上 api 名稱"
rtnRes.write(data)
return rtnRes
# {
# "api": "playData",
# "name": "Charmander"
# }