@@ -1154,61 +1154,43 @@ async def patch_single(
11541154
11551155 cls = self .table
11561156
1157- if issubclass (cls , BaseUser ):
1158- values = {
1159- getattr (cls , key ): getattr (model , key )
1160- for key in cleaned_data .keys ()
1161- }
1162- if values ["password" ]:
1163- cls ._validate_password (values ["password" ])
1164- values ["password" ] = cls .hash_password (values ["password" ])
1165- else :
1166- values .pop ("password" )
1167-
1168- await cls .update (values ).where (cls .email == values ["email" ]).run ()
1169- return Response (status_code = 200 )
1170- else :
1171- try :
1172- values = {
1173- getattr (cls , key ): getattr (model , key )
1174- for key in data .keys ()
1175- }
1176- except AttributeError :
1177- unrecognised_keys = set (data .keys ()) - set (
1178- model .model_dump ().keys ()
1179- )
1180- return Response (
1181- f"Unrecognised keys - { unrecognised_keys } ." ,
1182- status_code = 400 ,
1183- )
1157+ try :
1158+ values = {getattr (cls , key ): getattr (model , key ) for key in data .keys ()}
1159+ except AttributeError :
1160+ unrecognised_keys = set (data .keys ()) - set (model .model_dump ().keys ())
1161+ return Response (
1162+ f"Unrecognised keys - { unrecognised_keys } ." ,
1163+ status_code = 400 ,
1164+ )
11841165
1185- if self ._hook_map :
1186- values = await execute_patch_hooks (
1187- hooks = self ._hook_map ,
1188- hook_type = HookType .pre_patch ,
1189- row_id = row_id ,
1190- values = values ,
1191- request = request ,
1192- )
1166+ if self ._hook_map :
1167+ values = await execute_patch_hooks (
1168+ hooks = self ._hook_map ,
1169+ hook_type = HookType .pre_patch ,
1170+ row_id = row_id ,
1171+ values = values ,
1172+ request = request ,
1173+ )
11931174
1194- try :
1195- await cls .update (values ).where (
1196- cls ._meta .primary_key == row_id
1197- ).run ()
1198- new_row = (
1199- await cls .select (exclude_secrets = self .exclude_secrets )
1200- .where (cls ._meta .primary_key == row_id )
1201- .first ()
1202- .run ()
1203- )
1204- assert new_row
1205- return CustomJSONResponse (
1206- self .pydantic_model (** new_row ).model_dump_json ()
1207- )
1208- except ValueError :
1209- return Response (
1210- "Unable to save the resource." , status_code = 500
1211- )
1175+ if issubclass (cls , BaseUser ):
1176+ if password := values .pop ("password" , None ):
1177+ try :
1178+ cls ._validate_password (password )
1179+ except ValueError as e :
1180+ return Response (f"{ e } " , status_code = 400 )
1181+ values ["password" ] = cls .hash_password (password )
1182+ try :
1183+ await cls .update (values ).where (cls ._meta .primary_key == row_id ).run ()
1184+ new_row = (
1185+ await cls .select (exclude_secrets = self .exclude_secrets )
1186+ .where (cls ._meta .primary_key == row_id )
1187+ .first ()
1188+ .run ()
1189+ )
1190+ assert new_row
1191+ return CustomJSONResponse (self .pydantic_model (** new_row ).model_dump_json ())
1192+ except ValueError :
1193+ return Response ("Unable to save the resource." , status_code = 500 )
12121194
12131195 @apply_validators
12141196 @db_exception_handler
0 commit comments