error exceptions and columns of time
This commit is contained in:
@@ -12,7 +12,6 @@ from datetime import datetime, timedelta
|
||||
import asyncio
|
||||
|
||||
api = FastAPI()
|
||||
|
||||
from dotenv import load_dotenv #Работа с env для CORS
|
||||
import os
|
||||
load_dotenv()
|
||||
@@ -28,16 +27,6 @@ api.add_middleware(
|
||||
allow_headers=headers,
|
||||
)
|
||||
|
||||
# @api.middleware("http") #Логирование заходов перед всеми endpoints / возможно не нужен, так как то же самое делает uvicorn
|
||||
# async def log_requests(request: Request, call_next):
|
||||
# ip = request.client.host #ip
|
||||
# ua = request.headers.get("user-agent") #browser
|
||||
# method = request.method #method
|
||||
# url = str(request.url) #url
|
||||
# print(f"[{method}] {url} from {ip} ({ua})")
|
||||
# response = await call_next(request)
|
||||
# return response
|
||||
|
||||
@api.get("/protected") #test
|
||||
async def protected(current_user: str = Depends(JWT.current_user)):
|
||||
return {"msg": f"Hello, {current_user}"}
|
||||
@@ -45,8 +34,8 @@ async def protected(current_user: str = Depends(JWT.current_user)):
|
||||
@api.get("/", response_model=list[pydentic.UserOut]) #список!
|
||||
async def get_all_rows(current_user: str = Depends(JWT.current_user)):
|
||||
users = await db.get_all_rows()
|
||||
if not users:
|
||||
raise HTTPException(status_code=404, detail="No users found")
|
||||
if not user:
|
||||
raise HTTPException(status_code=401, detail="The user isn't found")
|
||||
return users
|
||||
@api.get("/get_user_by_email/{email}", response_model=pydentic.UserOut)
|
||||
async def get_user_by_email(email:str, current_user: str = Depends(JWT.current_user)):
|
||||
@@ -54,24 +43,27 @@ async def get_user_by_email(email:str, current_user: str = Depends(JWT.current_u
|
||||
if user:
|
||||
return user
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
raise HTTPException(status_code=401, detail="The user isn't found")
|
||||
@api.post("/user_create", response_model=pydentic.UserOut)
|
||||
async def create_user(row:pydentic.CreateUser):
|
||||
new_row = pydentic.CreateUser(email=row.email, description=row.description, activated = row.activated, password = row.password)
|
||||
await db.create_user(new_row)
|
||||
try:
|
||||
await db.create_user(new_row)
|
||||
except:
|
||||
raise HTTPException(status_code=409, detail="User with this email already exists")
|
||||
return new_row
|
||||
@api.delete("/user_delete/{email}", response_model=pydentic.UserOut)
|
||||
async def delete_user(email:str,current_user: str = Depends(JWT.current_user)):
|
||||
user = await db.get_user_by_email(email)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
raise HTTPException(status_code=401, detail="The user isn't found")
|
||||
await db.delete_user(email)
|
||||
return user
|
||||
@api.put("/user_update/{email}", response_model=pydentic.UserOut)
|
||||
async def update_user(email:str, updated_row: pydentic.UserUpdate, current_user: str = Depends(JWT.current_user)):
|
||||
user = await db.get_user_by_email(email)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
raise HTTPException(status_code=401, detail="The user isn't found")
|
||||
changed = False
|
||||
if updated_row.email is not None and updated_row.email != user.email:
|
||||
user.email = updated_row.email
|
||||
@@ -92,7 +84,10 @@ async def update_user(email:str, updated_row: pydentic.UserUpdate, current_user:
|
||||
return user
|
||||
@api.post("/login")
|
||||
async def login_user(form_data: OAuth2PasswordRequestForm = Depends()):
|
||||
creds = pydentic.UserLogin(email=form_data.username, password=form_data.password)
|
||||
try:
|
||||
creds = pydentic.UserLogin(email=form_data.username, password=form_data.password)
|
||||
except:
|
||||
raise HTTPException(status_code=422, detail="Email is not a valid email address")
|
||||
user = await db.login_user(creds)
|
||||
if not user:
|
||||
raise HTTPException(status_code=401, detail="The user isn't found")
|
||||
@@ -103,7 +98,6 @@ async def login_user(form_data: OAuth2PasswordRequestForm = Depends()):
|
||||
return {"access_token": access_token, "token_type": "bearer"}
|
||||
@api.post("/reset", response_model=pydentic.UserOut)
|
||||
async def reset_user(row:pydentic.UserReset):
|
||||
|
||||
user = await db.get_user_by_email(row.email)
|
||||
if not user:
|
||||
raise HTTPException(status_code=401, detail="The user isn't found")
|
||||
|
||||
Reference in New Issue
Block a user