|
1 | | -from app.schemas.user import UserPydanticList |
2 | | -from typing import Any, List |
3 | | -from fastapi import APIRouter, Depends, HTTPException |
| 1 | +from fastapi import APIRouter, Depends, HTTPException, status |
4 | 2 | from app import crud, models, schemas |
5 | | -from app.schemas import UserPydantic |
6 | 3 | from app.api import deps |
7 | 4 |
|
8 | 5 | router = APIRouter() |
9 | 6 |
|
10 | 7 |
|
11 | | -@router.get("/", response_model=UserPydanticList) |
| 8 | +@router.get("/", response_model=schemas.UserPydanticList) |
12 | 9 | async def read_users( |
13 | 10 | skip: int = 0, |
14 | 11 | limit: int = 100, |
15 | 12 | current_user: models.User = Depends(deps.get_current_active_superuser), |
16 | | -) -> Any: |
| 13 | +): |
17 | 14 | """ |
18 | 15 | Retrieve users. |
19 | 16 | """ |
20 | 17 | users = await crud.user.get_multi(skip=skip, limit=limit) |
21 | | - return await UserPydanticList.from_queryset(users) |
| 18 | + return await schemas.UserPydanticList.from_queryset(users) |
22 | 19 |
|
23 | 20 |
|
24 | | -@router.post("/", response_model=UserPydantic) |
| 21 | +@router.post("/", response_model=schemas.UserPydantic) |
25 | 22 | async def create_user( |
26 | 23 | user_in: schemas.UserCreateBySuperuser, |
27 | 24 | current_user: models.User = Depends(deps.get_current_active_superuser), |
28 | | -) -> Any: |
| 25 | +): |
29 | 26 | """ |
30 | 27 | Create new user. |
31 | 28 | """ |
32 | 29 | user = await crud.user.get_by_email(email=user_in.email) |
33 | 30 | if user: |
34 | 31 | raise HTTPException( |
35 | | - status_code=400, |
| 32 | + status_code=status.HTTP_400_BAD_REQUEST, |
36 | 33 | detail="The user with this username already exists in the system.", |
37 | 34 | ) |
38 | 35 | user = await crud.user.create_by_superuser(obj_in=user_in) |
39 | | - return UserPydantic.from_orm(user) |
| 36 | + return await schemas.UserPydantic.from_tortoise_orm(user) |
40 | 37 |
|
41 | 38 |
|
42 | | -@router.put("/me", response_model=UserPydantic) |
| 39 | +@router.put("/me", response_model=schemas.UserPydantic) |
43 | 40 | async def update_user_me( |
44 | 41 | user_in: schemas.UserUpdateMe, |
45 | 42 | current_user: models.User = Depends(deps.get_current_active_user), |
46 | | -) -> Any: |
| 43 | +): |
47 | 44 | """ |
48 | 45 | Update own user. |
49 | 46 | """ |
50 | 47 | user = await crud.user.update_me(db_obj=current_user, obj_in=user_in) |
51 | | - return schemas.UserPydantic.from_orm(user) |
| 48 | + return await schemas.UserPydantic.from_tortoise_orm(user) |
52 | 49 |
|
53 | 50 |
|
54 | | -@router.get("/me", response_model=UserPydantic) |
| 51 | +@router.get("/me", response_model=schemas.UserPydantic) |
55 | 52 | async def read_user_me( |
56 | 53 | current_user: models.User = Depends(deps.get_current_active_user), |
57 | | -) -> Any: |
| 54 | +): |
58 | 55 | """ |
59 | 56 | Get current user. |
60 | 57 | """ |
61 | | - return UserPydantic.from_orm(current_user) |
| 58 | + return await schemas.UserPydantic.from_tortoise_orm(current_user) |
62 | 59 |
|
63 | 60 |
|
64 | | -@router.post("/open", response_model=UserPydantic) |
| 61 | +@router.post("/open", response_model=schemas.UserPydantic) |
65 | 62 | async def create_user_open(user_in: schemas.UserCreateMe): |
66 | 63 | """ |
67 | 64 | Create new user without the need to be logged in. |
68 | 65 | """ |
69 | 66 | user = await crud.user.get_by_email(email=user_in.email) |
70 | 67 | if user: |
71 | 68 | raise HTTPException( |
72 | | - status_code=400, |
| 69 | + status_code=status.HTTP_400_BAD_REQUEST, |
73 | 70 | detail="The user with this username already exists in the system", |
74 | 71 | ) |
75 | 72 | user = await crud.user.create_me(obj_in=user_in) |
76 | | - return UserPydantic.from_orm(user) |
| 73 | + return await schemas.UserPydantic.from_tortoise_orm(user) |
77 | 74 |
|
78 | 75 |
|
79 | | -@router.get("/{user_id}", response_model=UserPydantic) |
| 76 | +@router.get("/{user_id}", response_model=schemas.UserPydantic) |
80 | 77 | async def read_user_by_id( |
81 | 78 | user_id: int, |
82 | 79 | current_user: models.User = Depends(deps.get_current_active_user), |
83 | | -) -> Any: |
| 80 | +): |
84 | 81 | """ |
85 | 82 | Get a specific user by id. |
86 | 83 | """ |
87 | 84 | user = await crud.user.get(id=user_id) |
88 | 85 | if user == current_user: |
89 | 86 | return user |
| 87 | + if not user: |
| 88 | + raise HTTPException( |
| 89 | + status_code=status.HTTP_400_BAD_REQUEST, detail="The user does not exist" |
| 90 | + ) |
90 | 91 | if not crud.user.is_superuser(current_user): |
91 | 92 | raise HTTPException( |
92 | | - status_code=400, detail="The user doesn't have enough privileges" |
| 93 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 94 | + detail="The user doesn't have enough privileges", |
93 | 95 | ) |
94 | | - return UserPydantic.from_orm(user) |
| 96 | + return await schemas.UserPydantic.from_tortoise_orm(user) |
95 | 97 |
|
96 | 98 |
|
97 | | -@router.put("/{user_id}", response_model=UserPydantic) |
| 99 | +@router.put("/{user_id}", response_model=schemas.UserPydantic) |
98 | 100 | async def update_user( |
99 | 101 | user_id: int, |
100 | 102 | user_in: schemas.UserUpdateBySuperuser, |
101 | 103 | current_user: models.User = Depends(deps.get_current_active_superuser), |
102 | | -) -> Any: |
| 104 | +): |
103 | 105 | """ |
104 | 106 | Update a user. |
105 | 107 | """ |
106 | 108 | user = await crud.user.get(id=user_id) |
107 | 109 | if not user: |
108 | 110 | raise HTTPException( |
109 | | - status_code=404, |
| 111 | + status_code=status.HTTP_404_NOT_FOUND, |
110 | 112 | detail="The user with this username does not exist in the system", |
111 | 113 | ) |
112 | 114 | user = await crud.user.update_by_superuser(db_obj=user, obj_in=user_in) |
113 | | - return UserPydantic.from_orm(user) |
| 115 | + return await schemas.UserPydantic.from_tortoise_orm(user) |
0 commit comments