-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.py
88 lines (72 loc) · 2.57 KB
/
users.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
from fastapi import Query, APIRouter
from utils import *
router = APIRouter()
@router.get("/", response_model=list[UserPublic])
def read_users(
current_user: CurrentUser,
session: SessionDep,
offset: int = 0,
limit: Annotated[int, Query(le=100)] = 100,
):
if not current_user.is_admin:
raise HTTPException(status_code=401, detail="You're not an admin")
users = session.exec(select(User).offset(offset).limit(limit)).all()
return users
@router.post("/", response_model=UserPublic)
def create_user(user_create: UserCreate, session: SessionDep):
user_exists = get_user(session, user_create.username)
if user_exists:
raise HTTPException(status_code=400, detail="username already exists")
email_exists = get_user_by_email(session, user_create.email)
if email_exists:
raise HTTPException(status_code=400, detail="email already exists")
db_user = User.model_validate(
user_create, update={"hashed_password": get_password_hash(user_create.password)}
)
session.add(db_user)
session.commit()
session.refresh(db_user)
return db_user
@router.get("/me", response_model=UserPublic)
def read_user_me(current_user: CurrentUser):
"""
Get current user.
"""
return current_user
@router.get("/{user_id}", response_model=UserPublic)
def read_user(user_id: int, session: SessionDep):
user = session.get(User, user_id)
if not user:
raise HTTPException(status_code=404, detail="User not found")
return user
@router.delete("/{user_id}")
def delete_user(user_id: int, current_user: CurrentUser, session: SessionDep):
if current_user.id != user_id and not current_user.is_admin:
raise HTTPException(
status_code=401, detail="You are not this user and you are not an admin"
)
user = session.get(User, user_id)
if user:
session.delete(user)
session.commit()
return {"ok": True}
@router.patch("/{user_id}", response_model=UserPublic)
def update_user(
current_user: CurrentUser,
user_id: int,
user_update: UserUpdate,
session: SessionDep,
):
if current_user.id != user_id and not current_user.is_admin:
raise HTTPException(
status_code=401, detail="You are not this user and you are not an admin"
)
user = session.get(User, user_id)
if not user:
raise HTTPException(status_code=404, detail="User not found")
user_data = user_update.model_dump(exclude_unset=True)
user.sqlmodel_update(user_data)
session.add(user)
session.commit()
session.refresh(user)
return user