1
1
from fastapi import APIRouter , Depends , Form , UploadFile , File
2
2
from fastapi .responses import RedirectResponse , Response
3
- from pydantic import BaseModel , EmailStr
4
3
from sqlmodel import Session
5
4
from typing import Optional
6
- from utils .models import User , DataIntegrityError
7
- from utils .auth import get_session , get_authenticated_user , verify_password , PasswordValidationError
5
+ from utils .models import User , UserBase , DataIntegrityError
6
+ from utils .auth import get_session , get_authenticated_user
8
7
from utils .images import validate_and_process_image
9
8
10
9
router = APIRouter (prefix = "/user" , tags = ["user" ])
13
12
# --- Server Request and Response Models ---
14
13
15
14
16
- class UpdateProfile ( BaseModel ):
15
+ class UpdateUser ( UserBase ):
17
16
"""Request model for updating user profile information"""
18
- name : str
19
- avatar_file : Optional [bytes ] = None
20
- avatar_content_type : Optional [str ] = None
21
-
22
17
@classmethod
23
18
async def as_form (
24
19
cls ,
25
- name : str = Form (... ),
20
+ name : Optional [ str ] = Form (None ),
26
21
avatar_file : Optional [UploadFile ] = File (None ),
27
22
):
28
23
avatar_data = None
@@ -34,81 +29,41 @@ async def as_form(
34
29
35
30
return cls (
36
31
name = name ,
37
- avatar_file = avatar_data ,
32
+ avatar_data = avatar_data ,
38
33
avatar_content_type = avatar_content_type
39
34
)
40
35
41
36
42
- class UserDeleteAccount (BaseModel ):
43
- confirm_delete_password : str
44
-
45
- @classmethod
46
- async def as_form (
47
- cls ,
48
- confirm_delete_password : str = Form (...),
49
- ):
50
- return cls (confirm_delete_password = confirm_delete_password )
51
-
52
-
53
37
# --- Routes ---
54
38
55
39
56
- @router .post ("/update_profile " , response_class = RedirectResponse )
40
+ @router .post ("/update " , response_class = RedirectResponse )
57
41
async def update_profile (
58
- user_profile : UpdateProfile = Depends (UpdateProfile .as_form ),
42
+ user_profile : UpdateUser = Depends (UpdateUser .as_form ),
59
43
user : User = Depends (get_authenticated_user ),
60
44
session : Session = Depends (get_session )
61
45
):
62
46
# Handle avatar update
63
- if user_profile .avatar_file :
47
+ if user_profile .avatar_data :
64
48
processed_image , content_type = validate_and_process_image (
65
- user_profile .avatar_file ,
49
+ user_profile .avatar_data ,
66
50
user_profile .avatar_content_type
67
51
)
68
- user_profile .avatar_file = processed_image
52
+ user_profile .avatar_data = processed_image
69
53
user_profile .avatar_content_type = content_type
70
54
71
55
# Update user details
72
56
user .name = user_profile .name
73
57
74
- if user_profile .avatar_file :
75
- user .avatar_data = user_profile .avatar_file
58
+ if user_profile .avatar_data :
59
+ user .avatar_data = user_profile .avatar_data
76
60
user .avatar_content_type = user_profile .avatar_content_type
77
61
78
62
session .commit ()
79
63
session .refresh (user )
80
64
return RedirectResponse (url = "/profile" , status_code = 303 )
81
65
82
66
83
- @router .post ("/delete_account" , response_class = RedirectResponse )
84
- async def delete_account (
85
- user_delete_account : UserDeleteAccount = Depends (
86
- UserDeleteAccount .as_form ),
87
- user : User = Depends (get_authenticated_user ),
88
- session : Session = Depends (get_session )
89
- ):
90
- if not user .password :
91
- raise DataIntegrityError (
92
- resource = "User password"
93
- )
94
-
95
- if not verify_password (
96
- user_delete_account .confirm_delete_password ,
97
- user .password .hashed_password
98
- ):
99
- raise PasswordValidationError (
100
- field = "confirm_delete_password" ,
101
- message = "Password is incorrect"
102
- )
103
-
104
- # Delete the user
105
- session .delete (user )
106
- session .commit ()
107
-
108
- # Log out the user
109
- return RedirectResponse (url = "/auth/logout" , status_code = 303 )
110
-
111
-
112
67
@router .get ("/avatar" )
113
68
async def get_avatar (
114
69
user : User = Depends (get_authenticated_user ),
0 commit comments