@@ -99,7 +99,7 @@ async def ws_options(session: SessionDep, current_user: CurrentUser, trans: Tran
9999async def ws_change (session : SessionDep , current_user : CurrentUser , oid : int ):
100100 ws_list : list [UserWs ] = await user_ws_options (session , current_user .id )
101101 if not any (x .id == oid for x in ws_list ):
102- raise HTTPException (f"oid [{ oid } ] is invalid!" )
102+ raise Exception (f"oid [{ oid } ] is invalid!" )
103103 user_model : UserModel = get_db_user (session = session , user_id = current_user .id )
104104 user_model .oid = oid
105105 session .add (user_model )
@@ -115,13 +115,13 @@ async def query(session: SessionDep, trans: Trans, id: int) -> UserEditor:
115115 return result
116116
117117@router .post ("" )
118- async def create (session : SessionDep , creator : UserCreator ):
118+ async def create (session : SessionDep , creator : UserCreator , trans : Trans ):
119119 if check_account_exists (session = session , account = creator .account ):
120- raise Exception (f"Account [{ creator .account } ] already exists!" )
120+ raise Exception (trans ( 'i18n_exist' , msg = f" { trans ( 'i18n_user.account' ) [{creator .account }]} " ) )
121121 if check_email_exists (session = session , email = creator .email ):
122- raise Exception (f"Email [{ creator .email } ] already exists!" )
122+ raise Exception (trans ( 'i18n_exist' , msg = f" { trans ( 'i18n_user.email' ) [{creator .email }]} " ) )
123123 if not check_email_format (creator .email ):
124- raise Exception (f"Email [{ creator .email } ] format is invalid!" )
124+ raise Exception (trans ( 'i18n_format_invalid' , key = f" { trans ( 'i18n_user.email' ) [{creator .email }]} " ) )
125125 data = creator .model_dump (exclude_unset = True )
126126 user_model = UserModel .model_validate (data )
127127 #user_model.create_time = get_timestamp()
@@ -144,16 +144,16 @@ async def create(session: SessionDep, creator: UserCreator):
144144
145145@router .put ("" )
146146@clear_cache (namespace = CacheNamespace .AUTH_INFO , cacheName = CacheName .USER_INFO , keyExpression = "editor.id" )
147- async def update (session : SessionDep , editor : UserEditor ):
147+ async def update (session : SessionDep , editor : UserEditor , trans : Trans ):
148148 user_model : UserModel = get_db_user (session = session , user_id = editor .id )
149149 if not user_model :
150150 raise Exception (f"User with id [{ editor .id } ] not found!" )
151151 if editor .account != user_model .account :
152152 raise Exception (f"account cannot be changed!" )
153153 if editor .email != user_model .email and check_email_exists (session = session , account = editor .email ):
154- raise Exception (f"Email [{ editor .email } ] already exists!" )
154+ raise Exception (trans ( 'i18n_exist' , msg = f" { trans ( 'i18n_user.email' ) [{editor .email }]} " ) )
155155 if not check_email_format (editor .email ):
156- raise Exception (f"Email [{ editor .email } ] format is invalid!" )
156+ raise Exception (trans ( 'i18n_format_invalid' , key = f" { trans ( 'i18n_user.email' ) [{editor .email }]} " ) )
157157 origin_oid : int = user_model .oid
158158 del_stmt = sqlmodel_delete (UserWsModel ).where (UserWsModel .uid == editor .id )
159159 session .exec (del_stmt )
@@ -188,43 +188,43 @@ async def batch_del(session: SessionDep, id_list: list[int]):
188188
189189@router .put ("/language" )
190190@clear_cache (namespace = CacheNamespace .AUTH_INFO , cacheName = CacheName .USER_INFO , keyExpression = "current_user.id" )
191- async def langChange (session : SessionDep , current_user : CurrentUser , language : UserLanguage ):
191+ async def langChange (session : SessionDep , current_user : CurrentUser , trans : Trans , language : UserLanguage ):
192192 lang = language .language
193193 if lang not in ["zh-CN" , "en" ]:
194- return { "message" : "Language not supported" }
194+ raise Exception ( trans ( 'i18n_user.language_not_support' , key = lang ))
195195 db_user : UserModel = get_db_user (session = session , user_id = current_user .id )
196196 db_user .language = lang
197197 session .add (db_user )
198198 session .commit ()
199199
200200@router .patch ("/pwd/{id}" )
201201@clear_cache (namespace = CacheNamespace .AUTH_INFO , cacheName = CacheName .USER_INFO , keyExpression = "id" )
202- async def pwdReset (session : SessionDep , current_user : CurrentUser , id : int ):
202+ async def pwdReset (session : SessionDep , current_user : CurrentUser , trans : Trans , id : int ):
203203 if not current_user .isAdmin :
204- raise HTTPException ( 'only for admin' )
204+ raise Exception ( trans ( 'i18n_permission.no_permission' , url = " patch[/user/pwd/id]," , msg = trans ( 'i18n_permission.only_admin' )) )
205205 db_user : UserModel = get_db_user (session = session , user_id = id )
206206 db_user .password = default_md5_pwd ()
207207 session .add (db_user )
208208 session .commit ()
209209
210210@router .put ("/pwd" )
211211@clear_cache (namespace = CacheNamespace .AUTH_INFO , cacheName = CacheName .USER_INFO , keyExpression = "current_user.id" )
212- async def pwdUpdate (session : SessionDep , current_user : CurrentUser , editor : PwdEditor ):
212+ async def pwdUpdate (session : SessionDep , current_user : CurrentUser , trans : Trans , editor : PwdEditor ):
213213 new_pwd = editor .new_pwd
214214 if not check_pwd_format (new_pwd ):
215- raise Exception ("Password format is invalid!" )
215+ raise Exception (trans ( 'i18n_format_invalid' , key = trans ( 'i18n_user.password' )) )
216216 db_user : UserModel = get_db_user (session = session , user_id = current_user .id )
217217 if not verify_md5pwd (editor .pwd , db_user .password ):
218- raise Exception (f"pwd [ { editor . pwd } ] error" )
218+ raise Exception (trans ( 'i18n_error' , key = trans ( 'i18n_user.password' )) )
219219 db_user .password = md5pwd (new_pwd )
220220 session .add (db_user )
221221 session .commit ()
222222
223223@router .patch ("/status" )
224224@clear_cache (namespace = CacheNamespace .AUTH_INFO , cacheName = CacheName .USER_INFO , keyExpression = "statusDto.id" )
225- async def langChange (session : SessionDep , current_user : CurrentUser , statusDto : UserStatus ):
225+ async def langChange (session : SessionDep , current_user : CurrentUser , trans : Trans , statusDto : UserStatus ):
226226 if not current_user .isAdmin :
227- raise Exception ("no permission to execute" )
227+ raise Exception (trans ( 'i18n_permission.no_permission' , url = ", " , msg = trans ( 'i18n_permission.only_admin' )) )
228228 status = statusDto .status
229229 if status not in [0 , 1 ]:
230230 return {"message" : "status not supported" }
0 commit comments