-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
198 lines (159 loc) · 5.33 KB
/
app.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from flask import Flask, jsonify, request
from pymongo import MongoClient
from flask_jwt_extended import (
JWTManager, jwt_required, create_access_token, create_refresh_token, get_jwt_identity, jwt_refresh_token_required
)
import config
import encryption as crypt
import license
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = config.key
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = config.access
app.config['JWT_REFRESH_TOKEN_EXPIRES'] = config.refresh
jwt = JWTManager(app)
@app.route('/join', methods=['POST'])
def join():
req = request.get_json()
req['user_pw'] = crypt.encryption(req['user_pw'])
conn = MongoClient(config.ip)
db = conn.main_server
mem_list = db.member
id_check = mem_list.find({'user_id': req['user_id']}).count()
if id_check != 0:
return jsonify({"code": 1, "msg": "Duplicate ID exists"}), 401
if req['type'] == '1':
mem_list.insert({
"user_id": req['user_id'],
"user_pw": req['user_pw'],
"user_name": req['user_name'],
"phone": req['phone'],
"birth": req['birth'],
"gender": req['gender'],
"type": req['type']
})
elif req['type'] == '2':
store = license.getData(req['license'])
if store['code'] == '1':
return jsonify(store)
mem_list.insert({
"user_id": req['user_id'],
"user_pw": req['user_pw'],
"user_name": req['user_name'],
"phone": req['phone'],
"birth": req['birth'],
"gender": req['gender'],
"type": req['type'],
"license": req['license'],
"store_name": store['store_name'],
"address": store['address']
})
elif req['type'] == '3':
mem_list.insert({
"user_id": req['user_id'],
"user_pw": req['user_pw'],
"type": req['type'],
"grant": "False"
})
return jsonify({"code": 0, "msg": "Join success"})
@app.route('/auth', methods=['POST'])
def auth():
req = request.get_json()
conn = MongoClient(config.ip)
db = conn.main_server
mem_list = db.member
result = mem_list.find_one({'user_id': req['user_id']})
if result is None:
return jsonify({"code": "1", "msg": "No matching ID or PW exists"}), 401
if not crypt.compare(req['user_pw'], result['user_pw']):
return jsonify({"code": "1", "msg": "No matching ID or PW exists"}), 401
del result['_id']
access_token = create_access_token(identity=req['user_id'])
refresh_token = create_refresh_token(identity=req['user_id'])
return jsonify(
code=0,
msg='login success',
user_name=result['user_name'],
type=result['type'],
access_token=access_token,
refresh_token=refresh_token
), 200
@app.route('/refresh', methods=['GET'])
@jwt_refresh_token_required
def refresh():
access_token = create_access_token(identity=get_jwt_identity())
return jsonify(access_token=access_token, user_id=get_jwt_identity())
def isManager(user_id):
conn = MongoClient(config.ip)
db = conn.main_server
member = db.member
manager = member.find_one({'user_id': user_id})
if manager['type'] == '3':
if manager['grant'] == 'True':
return True
return False
@app.route('/get_info', methods=['GET'])
@jwt_required
def getInfo():
try:
if not isManager(user_id=get_jwt_identity()):
return jsonify(code=1, msg="This user is not authorized."), 401
except Exception as e:
return jsonify(msg="Unregistered Manager"), 401
conn = MongoClient(config.ip)
db = conn.main_server
mem_list = db.member
result = mem_list.find_one({'user_id': request.args['type']})
if result['type'] == '1':
return jsonify(
user_id=result['user_id'],
user_name=result['user_name'],
phone=result['phone'],
birth=result['birth'],
gender=result['gender'],
type=result['type']
)
elif result['type'] == '2':
return jsonify(
user_id=result['user_id'],
user_name=result['user_name'],
phone=result['phone'],
birth=result['birth'],
gender=result['gender'],
type=result['type'],
license=result['license'],
store_name=result['store_name'],
address=result['address']
)
else:
return jsonify(
user_id=result['user_id'],
type=result['type']
)
@app.route('/alert-add', methods=['POST'])
def alertAdd():
req = request.get_json()
conn = MongoClient(config.ip)
db = conn.main_server
alert = db.alert
to_insert = []
for member in req['data']:
to_insert.append({"user_id": member['user_id']})
try:
alert.insert_many(to_insert, ordered=False)
except:
pass
finally:
return jsonify(msg="Done")
@app.route('/alert-check', methods=['GET'])
@jwt_required
def alertCheck():
conn = MongoClient(config.ip)
db = conn.main_server
alert = db.alert
user_id = request.args["user_id"]
result = alert.remove({'user_id': get_jwt_identity()})['n']
if result == 0:
return jsonify(code=0)
return jsonify(code=1)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=config.port)