-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin_routes.py
199 lines (171 loc) · 6.15 KB
/
admin_routes.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
from fastapi import APIRouter, Request
from sqlalchemy import (
select,
update,
insert,
delete,
)
from db_utils import engine, Users, validate_user
import hashlib
import pandas as pd
from io import StringIO
import requests
import asyncio
from fastapi.responses import JSONResponse
router = APIRouter()
SALT = "WITHPEPPER"
INTERNAL_API_KEY = "DUMMY_KEY"
@router.post("/admin/add_users")
async def add_user(request: Request):
params = await request.json()
token = params.get("token")
gsheets_url = params.get("gsheets_url")
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
if not gsheets_url:
return {"error": "no google sheets url provided"}
# get the users from the google sheet
user_dets_csv = None
try:
url_to_query = gsheets_url.split("/edit")[0] + "/gviz/tq?tqx=out:csv&sheet=v4"
user_dets_csv = await asyncio.to_thread(requests.get, url_to_query)
user_dets_csv = user_dets_csv.text
except:
return {"error": "could not get the google sheet"}
# get the users from the csv
try:
users = pd.read_csv(StringIO(user_dets_csv)).to_dict(orient="records")
print(users, flush=True)
except:
return {"error": "could not parse the google sheets csv"}
# create a password for each user
userdets = []
for user in users:
dets = {
"username": user.get("username", user.get("user_email")).lower(),
"user_type": user.get("user_type", user.get("user_role")).lower(),
}
userdets.append(dets)
# save the users to postgres
with engine.begin() as conn:
for dets in userdets:
hashed_password = hashlib.sha256(
(dets["username"] + SALT + "defog_" + dets["username"]).encode()
).hexdigest()
# check if user already exists
user_exists = conn.execute(
select(Users).where(Users.username == dets["username"])
).fetchone()
if user_exists:
conn.execute(
update(Users)
.where(Users.username == dets["username"])
.values(
hashed_password=hashed_password, user_type=dets["user_type"]
)
)
else:
conn.execute(
insert(Users).values(
username=dets["username"],
hashed_password=hashed_password,
token=INTERNAL_API_KEY,
user_type=dets["user_type"],
is_premium=True,
)
)
return {"status": "success"}
@router.post("/admin/add_users_csv")
async def add_users_csv(request: Request):
params = await request.json()
token = params.get("token")
users_csv = params.get("users_csv")
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
if not users_csv:
return {"error": "no users provided"}
users = pd.read_csv(StringIO(users_csv)).to_dict(orient="records")
# create a password for each user
userdets = []
for user in users:
dets = {
"username": user.get("username", user.get("user_email")).lower(),
"password": user.get("password", user.get("user_password")),
"user_type": user.get("user_type", user.get("user_role")).lower(),
}
userdets.append(dets)
# save the users to postgres
# save the users to postgres
with engine.begin() as conn:
cur = conn.connection.cursor()
for dets in userdets:
hashed_password = hashlib.sha256(
(dets["username"] + SALT + dets["password"]).encode()
).hexdigest()
# check if user already exists
user_exists = conn.execute(
select(Users).where(Users.username == dets["username"])
).fetchone()
if user_exists:
conn.execute(
update(Users)
.where(Users.username == dets["username"])
.values(
hashed_password=hashed_password, user_type=dets["user_type"]
)
)
else:
conn.execute(
insert(Users).values(
username=dets["username"],
hashed_password=hashed_password,
token=INTERNAL_API_KEY,
user_type=dets["user_type"],
is_premium=True,
)
)
return {"status": "success"}
@router.post("/admin/get_users")
async def get_users(request: Request):
params = await request.json()
token = params.get("token", None)
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
with engine.begin() as conn:
users = conn.execute(select(Users)).fetchall()
users = pd.DataFrame(users)[["username", "user_type"]].to_dict(orient="records")
return {"users": users}
@router.post("/admin/delete_user")
async def delete_user(request: Request):
params = await request.json()
token = params.get("token", None)
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
username = params.get("username", None)
with engine.begin() as conn:
conn.execute(delete(Users).where(Users.username == username))
return {"status": "success"}