This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
357 lines (311 loc) · 7.79 KB
/
server.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import jwt
import sys
import json
import logging
import requests
from fastapi.responses import ORJSONResponse
from fastapi import FastAPI, status
from datetime import date
import sql_select as sql
import js_models as js
logging.basicConfig(filename="app.log", filemode="w", format="%(name)s - %(levelname)s - %(message)s")
key = "s5be63t1b3sd5rts54rg1fvds1v2sdv"
FILE_SERVER = "http://100.100.100.100"
FILE_REQUEST = f"{FILE_SERVER}/auth/"
app = FastAPI()
@app.post("/client/auth/", response_class=ORJSONResponse)
async def auth(msg: js.auth):
"""
CLIENT: Authorization
request:
phone - "77017777777"
jwt
response:
key
id - organization id
name - organization name
"""
cn = None
try:
m = RedisDB()
obj = m.read(msg.phone)
jwt.decode(msg.jwt, obj["sk"], algorithms=["HS256"])
except jwt.exceptions.InvalidSignatureError or jwt.exceptions.ExpiredSignatureError:
print("ERROR jwt")
return {
"status": status.HTTP_400_BAD_REQUEST,
"error": "Ошибка авторизации"
}
try:
cn = sql.cn_db()
rq = sql.auth(cn, obj["id"])
except:
return {
"status": status.HTTP_400_BAD_REQUEST,
"error": "Произошла внутренняя ошибка сервера. Попробуйте позже"
}
finally:
if cn:
sql.dscn_db(cn)
if rq:
return {
"status": status.HTTP_200_OK,
"dat": {
"key": key,
"id": rq[0],
"name": rq[1],
}
}
else:
return {
"status": status.HTTP_400_BAD_REQUEST,
"error": "Ваш номер не был зарегистрирован"
}
@app.post("/client/info/", response_class=ORJSONResponse)
async def info(msg: js.info):
"""
CLIENT: Show information to client
request:
id
key
response:
id
name
phone
team - [names]
"""
cn = None
try:
cn = sql.cn_db()
rq = sql.info(cn, msg.id)
except:
return {
"status": status.HTTP_400_BAD_REQUEST,
"error": "Произошла внутренняя ошибка сервера. Попробуйте позже",
}
else:
return {
"status": status.HTTP_200_OK,
"dat": rq,
"key": key
}
finally:
if cn:
sql.dscn_db(cn)
@app.post("/client/info/upload/", response_class=ORJSONResponse)
async def new_info(msg: js.new_info):
"""
CLIENT: Insert information about members
request:
name - Name of new member
id - id of new member
phone - 77017777777
key
"""
cn = None
uid = None
js_rq = {
"name": msg.name,
"id": msg.id,
"phone": msg.phone
}
try:
r = requests.post(FILE_REQUEST, data=json.dumps(js_rq))
rd = json.loads(r.content)
except Exception as e:
err = sys.exc_info()
print("ERROR", err, err[-1].tb_lineno)
logging.exception(e)
return {
"status": status.HTTP_400_BAD_REQUEST,
"error": "Не удалось сохранить новый сектор. Попробуйте позже",
}
try:
cn = sql.cn_db()
sql.new_info(cn, msg.name, uid, msg.id, msg.phone)
except:
return {
"status": status.HTTP_400_BAD_REQUEST,
"error": "Не удалось сохранить новый сектор. Попробуйте позже",
}
else:
return {
"status": status.HTTP_200_OK,
"key": key
}
finally:
if cn:
sql.dscn_db(cn)
@app.post("/client/news/upload/", response_class=ORJSONResponse)
async def news(msg: js.news):
"""
CLIENT: Insert news in web-site
request:
id - id of CLIENT
start_date - start date
end_date - end date
content - content of news in russian
"""
cn = None
try:
cn = sql.cn_db()
sql.insert_news(
cn, msg.id,
msg.start_date,
msg.end_date,
msg.content
)
except:
return {
"status": status.HTTP_400_BAD_REQUEST,
"error": "Не удалось создать оповещение. Попробуйте позже",
}
else:
return {
"status": status.HTTP_200_OK,
"key": key
}
finally:
if cn:
sql.dscn_db(cn)
@app.get("/client/news/", response_class=ORJSONResponse)
async def news():
"""
CLIENT: Show news to CLIENT
response:
content - content of news
start_date - start date
end_date - end date
"""
cn = sql.cn_db()
rq = sql.news(cn)
sql.dscn_db(cn)
if rq is None:
return {
"status": status.HTTP_400_BAD_REQUEST
}
else:
return {
"status": status.HTTP_200_OK,
"dat": rq
}
@app.get("/client/number/")
async def number():
"""
CLIENT: Show number of processed, submitted, completed project to CLIENT
response:
submitted
processed
completed
"""
cn = None
try:
cn = sql.cn_db()
rq = sql.number(cn)
except:
return {"status": status.HTTP_400_BAD_REQUEST}
else:
return {
"status": status.HTTP_200_OK,
"dat": rq
}
finally:
if cn:
sql.dscn_db(cn)
@app.post("/client/request/upload/")
async def request(msg: js.request):
"""
CLIENT: Insert information about project
request:
project_name
last_name
first_name
deadline, for example "2020-08-20"
status
description
phone
comment
address
response:
pj_id
"""
cn = None
try:
cn = sql.cn_db()
rq = sql.request(
cn,
msg.project_name,
msg.last_name,
msg.first_name,
msg.deadline,
msg.status,
msg.description,
msg.phone,
msg.comment,
msg.address
)
except:
return {"status": status.HTTP_400_BAD_REQUEST}
else:
return {
"status": status.HTTP_200_OK,
"dat": rq
}
finally:
if cn:
sql.dscn_db(cn)
@app.post("/client/request/", response_class=ORJSONResponse)
async def request(msg: js.request):
"""
CLIENT: Show information about project to CLIENT
request:
id
key
response:
id
name
member_id
deadline
status
created_date
description
comment
address
"""
cn = None
try:
cn = sql.cn_db()
rq = sql.s_request(cn, msg.id)
except:
return {"status": status.HTTP_400_BAD_REQUEST,
"error": "Произошла внутренняя ошибка сервера. Попробуйте позже",
}
else:
return {
"status": status.HTTP_200_OK,
"dat": rq,
"key": key
}
finally:
if cn:
sql.dscn_db(cn)
@app.post("/client/get_rate/")
async def rate(msg: js.rate):
"""
CLIENT: Update RATE from CLIENT
request:
project_id
comment
rate
"""
cn = None
try:
cn = sql.cn_db()
sql.rate(cn, msg.project_id, msg.rate, msg.comment)
except:
return {"status": status.HTTP_400_BAD_REQUEST}
else:
return {"status": status.HTTP_200_OK}
finally:
if cn:
sql.dscn_db(cn)