-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
175 lines (128 loc) · 5.21 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
import json
import base64
import pandas as pd
from fastapi import FastAPI
from fastapi import Request
from fastapi import Depends
from fastapi_login import LoginManager
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import OAuth2PasswordRequestForm
from fastapi_login.exceptions import InvalidCredentialsException
db = pd.read_pickle("db.pkl")
notifications = dict()
SECRET = 'тигры арр'
manager = LoginManager(SECRET, token_url='/auth/token')
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def check_index(room_name, date, time_id):
contains_room_name = db["room_name"].str.contains(room_name)
contains_date = db["date"].str.contains(date)
contains_time_id = db["time_id"].str.contains(time_id)
return contains_room_name.any() and contains_date.any() and contains_time_id.any(), \
contains_room_name & contains_date & contains_time_id
# TODO store hash
fake_db = {'evgeny': {'password': '1234'},
'marina': {'password': '1234'},
'egor': {'password': '1234'},
'andrey': {'password': '1234'}}
@manager.user_loader()
def load_user(email: str):
user = fake_db.get(email)
return user
@app.post('/auth/token')
def login(data: OAuth2PasswordRequestForm = Depends()):
email = data.username
password = data.password
user = load_user(email)
if not user:
raise InvalidCredentialsException
elif password != user['password']:
raise InvalidCredentialsException
access_token = manager.create_access_token(
data=dict(sub=email)
)
return {'access_token': access_token, 'token_type': 'Bearer'}
# @app.get('/protected')
# def protected_route(user=Depends(manager)):
# return {"user": user}
@app.post("/knopka")
async def knopka_post(request: Request):
time_stamp = dt.now() + timedelta(hours=3)
rounded = time_stamp - (time_stamp - dt.min) % timedelta(minutes=30)
body = await request.json()
body["data"] = json.loads(base64.b64decode(body["data"]).decode('utf8'))
# print(body, request.headers)
room_name = "0"
date = time_stamp.strftime("%Y-%m-%d")
time_id = rounded.strftime("%H:%M")
# print(db)
possible, index = check_index(room_name, date, time_id)
if not possible:
return
# Process buttons
if body["data"]["telemetry"]["firstButton"]["status"] == "click":
db.loc[index, "user"] = None
db.loc[index, "service_col"] = None
db.loc[index, "service_time"] = None
elif body["data"]["telemetry"]["firstButton"]["status"] == "long_press":
notifications[db.loc[index, "user"][0]] = "WARNING! Your room is contested. You are to loose access in 5 minutes"
db.loc[index, "service_col"] = db.loc[index, "user"]
db.loc[index, "user"] = "Anon"
db.loc[index, "service_time"] = pd.to_datetime(time_stamp)
elif body["data"]["telemetry"]["firstButton"]["status"] == "double_click":
if (time_stamp - db.loc[index, "service_time"].dt.to_pydatetime()) > timedelta(minutes=5):
# print(db)
return
db.loc[index, "user"] = db.loc[index, "service_col"]
db.loc[index, "service_col"] = None
db.loc[index, "service_time"] = None
# print(db)
@app.post("/book")
async def book_post(request: Request, user=Depends(manager)):
body = await request.json()
print(type(body), body)
possible, index = check_index(body["room_name"], body["date"], body["time_id"])
if not possible:
return { "success": False, "error_msg": "Incorrect data entry" }
# print(db)
db.loc[index, "user"] = body["user"]
# print((contains_room_name & contains_date & contains_time_id).any())
# print(db)
return { "success": True, "error_msg": "Successfully booked the room" }
@app.post("/unbook")
async def unbook_post(request: Request, user=Depends(manager)):
body = await request.json()
# print(type(body), body)
possible, index = check_index(body["room_name"], body["date"], body["time_id"])
if not possible:
return { "success": False, "error_msg": "Incorrect data entry" }
db.loc[index, "user"] = None
# print((contains_room_name & contains_date & contains_time_id).any())
return { "success": True, "error_msg": "Successfully unbooked the room" }
@app.post("/notify")
async def notify_post(request: Request, user=Depends(manager)):
name = (await request.json())["user"]
if not name in notifications:
return {"notification": None}
ret = {"notification": notifications[name]}
del notifications[name]
return ret
@app.post("/json")
async def json_post(request: Request, user=Depends(manager)):
slc = db[["room_name", "time_id", "user"]]
slc = slc[db["date"] == "2022-04-10"]
# https://stackoverflow.com/questions/55004985/convert-pandas-dataframe-to-json-with-columns-as-key
cols = slc.columns.difference(['room_name'])
return (slc.groupby('room_name')[cols]
.apply(lambda x: x.to_dict('records'))
.reset_index(name='data')
.to_json(orient='records'))
@app.on_event("shutdown")
async def shutdown_event():
db.to_pickle("db.pkl")