-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
66 lines (47 loc) · 1.86 KB
/
utils.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
from datetime import timedelta, datetime
from passlib.context import CryptContext
from jose import jwt
import os
import json
from schemas.auth import UserInfo
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password):
return pwd_context.hash(password)
def create_access_token(data: dict):
expires_delta = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode = data.copy()
expire = datetime.utcnow() + expires_delta
to_encode.update({"exp": expire})
encode_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encode_jwt
def user_entity(user) -> dict:
return {
"id": str(user["_id"]),
"full_name": user["full_name"],
"email": user["email"],
"role": user["role"],
"is_verified": user["is_verified"],
"password": user["password"],
"phone": user["phone"],
"created_at": user["created_at"],
"updated_at": user["updated_at"]
}
def get_email_from_token(token) -> str:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email = payload.get("sub")
return email
ALLOWED_EXTENSIONS = {'pdf', 'txt'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def settings_check(num: str):
if not os.path.exists(f"./settings/{num}"):
os.makedirs(f"./settings/{num}")
with open(f"./settings/{num}/settings.json", "w") as f:
data = {"title": f"Bot {num}", "header": "",
"bot": "", "user": "", "model": "gpt-3.5-turbo"}
json.dump(data, f)