-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.py
252 lines (224 loc) · 7.78 KB
/
services.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from datetime import datetime
from parser import parserId, parserLogin, parserRegister, parserUpdate
from flask import jsonify
from flask_jwt_extended import (
create_access_token,
create_refresh_token,
decode_token,
get_jwt_identity,
jwt_required,
)
from flask_jwt_extended.utils import get_jwt
from sqlalchemy.sql.functions import count
from main import app, db
from orm import Customer, CustomersMeets, MeetModel, RevokedTokenModel, UserModel
@app.route("/customers-meetings", methods=["POST"])
def customers_meetings():
def to_json(x):
return {
"customer_id": x.CustomersMeets.customers_id,
"firstName": x.Customer.firstname,
"lastName": x.Customer.lastname,
"id": x.CustomersMeets.id,
"meet_id": x.CustomersMeets.meet_id,
"address": x.MeetModel.address,
"description": x.MeetModel.description,
}
query = (
db.session.query(CustomersMeets, Customer, MeetModel)
.filter(
CustomersMeets.customers_id == Customer.customer_id,
)
.filter(CustomersMeets.meet_id == MeetModel.meet_id)
)
return {"meetings_customers": list(map(lambda x: to_json(x), query.all()))}
@app.route("/group_by-adress", methods=["GET"])
def group_by_adress():
querry = (
db.session.query(
count(CustomersMeets.id),
MeetModel.address,
)
.filter(CustomersMeets.meet_id == MeetModel.meet_id)
.group_by(MeetModel.meet_id)
)
# grouped_querry = query.group_by(MeetModel.address).all()
def to_json(x):
return {
"count": x[0],
"address": x[1],
}
return {"ga": list(map(lambda x: to_json(x), querry.all()))}
@app.route("/users", methods=["POST"])
@jwt_required()
def users():
return UserModel.return_all()
@app.route("/update-user", methods=["POST"])
@jwt_required()
def updateUser():
data = parserUpdate.parse_args()
user = UserModel.findById(data["id"])
if user:
user.username = data["username"] if data["username"] else user.username
user.password = (
UserModel.generate_hash(data["password"])
if data["password"]
else user.password
)
user.firstName = data["firstName"] if data["firstName"] else user.firsNname
user.lastName = data["lastName"] if data["lastName"] else user.lastName
user.role = data["role"] if data["role"] else user.role
user.updateToDb()
else:
return jsonify({"message": "User not found"}), 404
return {"message": "User updated"}
@app.route("/register", methods=["POST"])
def register():
data = parserRegister.parse_args()
id = parserId.parse_args()
if UserModel.find_by_username(data["username"]):
return jsonify({"message": f'User {data["username"]} already exists'}), 400
new_user = UserModel(
username=data["username"],
password=UserModel.generate_hash(data["password"]),
role=data["role"],
firstName=data["firstName"],
lastName=data["lastName"],
)
try:
new_user.save_to_db()
access_token = create_access_token(identity=data["username"])
dec_acc = decode_token(access_token)
refresh_token = create_refresh_token(identity=data["username"])
dec_ref = decode_token(refresh_token)
RevokedTokenModel(
dec_acc["jti"],
datetime.fromtimestamp(dec_acc["exp"]),
id["uid"],
id["system_string"],
).add()
RevokedTokenModel(
dec_ref["jti"],
datetime.fromtimestamp(dec_ref["exp"]),
id["uid"],
id["system_string"],
).add()
return {
# "message": "User created successfully",
"id": new_user.id,
"username": new_user.username,
"firstName": new_user.firstName,
"lastName": new_user.lastName,
"role": new_user.role,
"access_token": access_token,
"refresh_token": refresh_token,
}, 201
except Exception as e:
return {"message": str(e)}, 500
@app.route("/login", methods=["POST"])
def login():
data = parserLogin.parse_args()
id = parserId.parse_args()
user = UserModel.find_by_username(data["username"])
if user and UserModel.verify_hash(data["password"], user.password):
access_token = create_access_token(identity=data["username"])
dec_acc = decode_token(access_token)
refresh_token = create_refresh_token(identity=data["username"])
dec_ref = decode_token(refresh_token)
RevokedTokenModel(
dec_acc["jti"],
datetime.fromtimestamp(dec_acc["exp"]),
id["uid"],
id["system_string"],
).add()
RevokedTokenModel(
dec_ref["jti"],
datetime.fromtimestamp(dec_ref["exp"]),
id["uid"],
id["system_string"],
).add()
return {
"id": user.id,
"username": user.username,
"firstName": user.firstName,
"lastName": user.lastName,
"role": user.role,
"access_token": access_token,
"refresh_token": refresh_token,
}, 200
else:
return {"message": "Wrong credentials"}, 401
# TODO: Теперь надо сделать очищение токенов при запросах с Angular
@app.route("/logout", methods=["POST"])
@jwt_required(refresh=True)
def logout():
refresh_token = get_jwt()["jti"]
id = parserId.parse_args()
RevokedTokenModel.find_and_revoke(refresh_token, id["uid"], id["system_string"])
RevokedTokenModel.find_and_revoke(
decode_token(id["access_token"], allow_expired=True)["jti"],
id["uid"],
id["system_string"],
)
return {"message": "Successfully logged out"}, 200
@app.route("/protected-refresh", methods=["GET", "POST"])
@jwt_required(refresh=True)
# @not_blacklisted_token
def protected():
return {"message": "protected_refresh"}, 200
@app.route("/protected-access", methods=["GET", "POST"])
@jwt_required()
# @not_blacklisted_token
def protected_access():
return {"message": "protected_access"}, 200
@app.route("/refresh-access", methods=["POST"])
@jwt_required(
refresh=True,
)
def refresh_access():
id = parserId.parse_args()
RevokedTokenModel.find_and_revoke(
decode_token(id["access_token"], allow_expired=True)["jti"],
id["uid"],
id["system_string"],
)
username = get_jwt_identity()
access_token = create_access_token(identity=username)
dec_acc = decode_token(access_token)
RevokedTokenModel(
dec_acc["jti"],
datetime.fromtimestamp(dec_acc["exp"]),
id["uid"],
id["system_string"],
).add()
return {"access_token": access_token}, 200
@app.route("/refresh-refresh", methods=["POST"])
@jwt_required(
refresh=True,
)
def refresh_refresh():
username = get_jwt_identity()
id = parserId.parse_args()
RevokedTokenModel.find_and_revoke(get_jwt()["jti"], id["uid"], id["system_string"])
RevokedTokenModel.find_and_revoke(
decode_token(id["access_token"], allow_expired=True)["jti"],
id["uid"],
id["system_string"],
)
refresh_token = create_refresh_token(identity=username)
dec_ref = decode_token(refresh_token)
RevokedTokenModel(
dec_ref["jti"],
datetime.fromtimestamp(dec_ref["exp"]),
id["uid"],
id["system_string"],
).add()
access_token = create_access_token(identity=username)
dec_acc = decode_token(access_token)
RevokedTokenModel(
dec_acc["jti"],
datetime.fromtimestamp(dec_acc["exp"]),
id["uid"],
id["system_string"],
).add()
return {"refresh_token": refresh_token, "access_token": access_token}, 200