|
1 | | -from typing import Annotated, Any, List |
| 1 | +from typing import Any, List |
2 | 2 |
|
3 | | -from fastapi import APIRouter, Body, Depends, HTTPException |
4 | | -from fastapi.encoders import jsonable_encoder |
5 | | -from pydantic.networks import EmailStr |
| 3 | +from fastapi import APIRouter, Depends, HTTPException |
6 | 4 | from sqlmodel import select |
7 | 5 |
|
8 | 6 | from app import crud |
|
12 | 10 | get_current_active_superuser, |
13 | 11 | ) |
14 | 12 | from app.core.config import settings |
15 | | -from app.models import User, UserCreate, UserCreateOpen, UserOut, UserUpdate |
| 13 | +from app.models import ( |
| 14 | + User, |
| 15 | + UserCreate, |
| 16 | + UserCreateOpen, |
| 17 | + UserOut, |
| 18 | + UserUpdate, |
| 19 | + UserUpdateMe, |
| 20 | +) |
16 | 21 | from app.utils import send_new_account_email |
17 | 22 |
|
18 | 23 | router = APIRouter() |
@@ -54,29 +59,24 @@ def create_user(*, session: SessionDep, user_in: UserCreate) -> Any: |
54 | 59 | return user |
55 | 60 |
|
56 | 61 |
|
57 | | -# TODO: Refactor when SQLModel has update |
58 | | -# @router.put("/me") |
59 | | -# def update_user_me( |
60 | | -# *, |
61 | | -# session: SessionDep, |
62 | | -# password: Annotated[str, Body(None)], |
63 | | -# full_name: Annotated[str, Body(None)], |
64 | | -# email: Annotated[EmailStr, Body(None)], |
65 | | -# current_user: CurrentUser, |
66 | | -# ) -> UserOut: |
67 | | -# """ |
68 | | -# Update own user. |
69 | | -# """ |
70 | | -# current_user_data = jsonable_encoder(current_user) |
71 | | -# user_in = UserUpdate(**current_user_data) |
72 | | -# if password is not None: |
73 | | -# user_in.password = password |
74 | | -# if full_name is not None: |
75 | | -# user_in.full_name = full_name |
76 | | -# if email is not None: |
77 | | -# user_in.email = email |
78 | | -# user = crud.user.update(session, session_obj=current_user, obj_in=user_in) |
79 | | -# return user |
| 62 | +@router.put("/me", response_model=UserOut) |
| 63 | +def update_user_me( |
| 64 | + *, session: SessionDep, body: UserUpdateMe, current_user: CurrentUser |
| 65 | +) -> Any: |
| 66 | + """ |
| 67 | + Update own user. |
| 68 | + """ |
| 69 | + # TODO: Refactor when SQLModel has update |
| 70 | + # current_user_data = jsonable_encoder(current_user) |
| 71 | + # user_in = UserUpdate(**current_user_data) |
| 72 | + # if password is not None: |
| 73 | + # user_in.password = password |
| 74 | + # if full_name is not None: |
| 75 | + # user_in.full_name = full_name |
| 76 | + # if email is not None: |
| 77 | + # user_in.email = email |
| 78 | + # user = crud.user.update(session, session_obj=current_user, obj_in=user_in) |
| 79 | + # return user |
80 | 80 |
|
81 | 81 |
|
82 | 82 | @router.get("/me", response_model=UserOut) |
@@ -127,22 +127,27 @@ def read_user_by_id( |
127 | 127 | return user |
128 | 128 |
|
129 | 129 |
|
130 | | -# TODO: Refactor when SQLModel has update |
131 | | -# @router.put("/{user_id}", dependencies=[Depends(get_current_active_superuser)]) |
132 | | -# def update_user( |
133 | | -# *, |
134 | | -# session: SessionDep, |
135 | | -# user_id: int, |
136 | | -# user_in: UserUpdate, |
137 | | -# ) -> UserOut: |
138 | | -# """ |
139 | | -# Update a user. |
140 | | -# """ |
141 | | -# user = session.get(User, user_id) |
142 | | -# if not user: |
143 | | -# raise HTTPException( |
144 | | -# status_code=404, |
145 | | -# detail="The user with this username does not exist in the system", |
146 | | -# ) |
147 | | -# user = crud.user.update(session, db_obj=user, obj_in=user_in) |
148 | | -# return user # type: ignore |
| 130 | +@router.put( |
| 131 | + "/{user_id}", |
| 132 | + dependencies=[Depends(get_current_active_superuser)], |
| 133 | + response_model=UserOut, |
| 134 | +) |
| 135 | +def update_user( |
| 136 | + *, |
| 137 | + session: SessionDep, |
| 138 | + user_id: int, |
| 139 | + user_in: UserUpdate, |
| 140 | +) -> Any: |
| 141 | + """ |
| 142 | + Update a user. |
| 143 | + """ |
| 144 | + |
| 145 | + # TODO: Refactor when SQLModel has update |
| 146 | + # user = session.get(User, user_id) |
| 147 | + # if not user: |
| 148 | + # raise HTTPException( |
| 149 | + # status_code=404, |
| 150 | + # detail="The user with this username does not exist in the system", |
| 151 | + # ) |
| 152 | + # user = crud.user.update(session, db_obj=user, obj_in=user_in) |
| 153 | + # return user |
0 commit comments