-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi究极版
114 lines (92 loc) · 3.46 KB
/
api究极版
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from gevent import monkey
monkey.patch_all()
import sys
from flask import Flask, request, jsonify
from flask_restful import Api, Resource
import json
import pymongo
from flask_cors import CORS
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
app = Flask(__name__)
CORS(app, supports_credentials=True)
api = Api(app)
db_name = "SUMSC_file"
db_address = "mongodb://118.126.66.3:27017"
col_name = "application"
code_json = "./code.json"
mydict = {}
names = []
class Id_write(Resource):
def get(self):
print("get")
return "hi"
def option(self):
return "success"
def post(self):
print("post")
names.clear()
args = json.loads(request.get_data())
if mydict == {}:
user_id = 1
else:
user_id = len(mydict) + 1
user_id = 'user{}'.format(user_id)
mydict[user_id] = {"actname": args["actname"],
"code": args["code"],
"cemail": args["cemail"],
"cgender": args["cgender"],
"cgrade": args["cgrade"],
"cid": args["cid"],
"cmajor": args["cmajor"],
"cname": args["cname"],
"cphone": args["cphone"],
"teamname": args["teamname"],
"teamsize": args["teamsize"]}
names.append(mydict[user_id]["cname"])
with open(code_json, encoding='utf-8') as f:
new = json.load(f)
actname0 = mydict[user_id]["actname"]
'''比对验证码'''
if mydict[user_id]["code"] == new[actname0]:
teamsize = int(args["teamsize"])
if teamsize > 1:
teamsize0 = teamsize - 1
for n in range(teamsize0):
mydict_add = {
"email{}".format(n): args["email{}".format(n)],
"gender{}".format(n): args["gender{}".format(n)],
"grade{}".format(n): args["grade{}".format(n)],
"id{}".format(n): args["id{}".format(n)],
"major{}".format(n): args["major{}".format(n)],
"name{}".format(n): args["name{}".format(n)],
"phone{}".format(n): args["phone{}".format(n)]
}
mydict[user_id].update(mydict_add)
names.append(mydict[user_id]["name{}".format(n)])
'''数据库的写入和查重'''
myclient = pymongo.MongoClient(db_address)
mydb = myclient[db_name]
mycol = mydb[col_name]
if not mycol.find_one({"cid":mydict[user_id]["cid"]}):
mycol.insert_one(mydict[user_id])
# 把mydict[user_id]写入数据库
if mydict[user_id] in mycol.find():
'''若此处真正写入数据库'''
res = 'SUMSC200'
else:
res = 'SUMSC500'
else:
'''重复报名'''
res = 'SUMSC424'
else:
'''验证码错误'''
res = 'SUMSC403'
a = {'status_code': res,
'names': names}
return jsonify(a)
api.add_resource(Id_write, '/')
if __name__ == '__main__':
app.run(host='0.0.0.0', port='10000', debug=True)