11import inspect
22import logging
3- import re
43from collections .abc import Sequence
54from datetime import datetime , timedelta , timezone
65from io import BytesIO , StringIO
@@ -45,27 +44,6 @@ def is_allowed_field_or_path(field: str, allowed_fields: set[str]) -> bool:
4544 return base_field in allowed_fields
4645
4746
48- def convert_id (id : str | int | UUID ) -> int | UUID | None :
49- """Convert the given id to int or UUID.
50-
51- :param id: A string value.
52- :return: An int or UUID value. Or None if the given id is invalid.
53- """
54- if isinstance (id , int | UUID ):
55- return id
56-
57- # Check if the input_str is an integer
58- if re .fullmatch (r"\d+" , id ):
59- return int (id )
60-
61- # Check if the input_str is a valid UUID
62- if re .fullmatch (r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" , id ):
63- return UUID (id )
64-
65- logger .warning ("Invalid id: %s" , id )
66- return None
67-
68-
6947async def get_user_id_from_session_id (session_id : str | None ) -> UUID | int | None :
7048 """This method is used to get user id from session_id.
7149
@@ -95,7 +73,6 @@ async def get_user_id_from_session_id(session_id: str | None) -> UUID | int | No
9573 if not user_id :
9674 return None
9775
98- user_id = convert_id (user_id )
9976 if not user_id or not await admin_model .get_obj (user_id ):
10077 return None
10178
@@ -290,8 +267,9 @@ async def get(
290267
291268 try :
292269 obj = await admin_model .get_obj (id )
293- except (ValueError , TypeError ):
294- raise AdminApiException (404 , detail = f"{ model } not found." ) from None
270+ except Exception as e :
271+ logger .error ("Error getting %s %s: %s" , model , id , e )
272+ raise AdminApiException (500 , detail = f"Error getting { model } { id } : { e } " ) from e
295273 if not obj :
296274 raise AdminApiException (404 , detail = f"{ model } not found." )
297275 return obj
@@ -309,7 +287,11 @@ async def add(
309287 if not admin_model :
310288 raise AdminApiException (404 , detail = f"{ model } model is not registered." )
311289 self ._bind_admin_context (admin_model , request = request , user = current_user )
312- return await admin_model .save_model (None , payload ) # type: ignore [return-value]
290+ try :
291+ return await admin_model .save_model (None , payload ) # type: ignore [return-value]
292+ except Exception as e :
293+ logger .error ("Error adding %s: %s" , model , e )
294+ raise AdminApiException (500 , detail = f"Error adding { model } : { e } " ) from e
313295
314296 async def change_password (
315297 self ,
@@ -336,8 +318,8 @@ async def change_password(
336318
337319 try :
338320 user = await admin_model .get_obj (id )
339- except ( ValueError , TypeError ) :
340- raise AdminApiException (404 , detail = f"{ settings .ADMIN_USER_MODEL } not found. " ) from None
321+ except Exception as e :
322+ raise AdminApiException (500 , detail = f"Error getting { settings .ADMIN_USER_MODEL } { id } : { e } " ) from e
341323 if not user :
342324 raise AdminApiException (404 , detail = f"{ settings .ADMIN_USER_MODEL } not found." )
343325
@@ -367,8 +349,9 @@ async def change(
367349
368350 try :
369351 obj = await admin_model .save_model (id , payload )
370- except (ValueError , TypeError ):
371- raise AdminApiException (404 , detail = f"{ model } not found." ) from None
352+ except Exception as e :
353+ logger .error ("Error changing %s %s: %s" , model , id , e )
354+ raise AdminApiException (500 , detail = f"Error changing { model } { id } : { e } " ) from e
372355 if not obj :
373356 raise AdminApiException (404 , detail = f"{ model } not found." )
374357 return obj
@@ -468,8 +451,9 @@ async def delete(
468451 raise AdminApiException (403 , detail = "You cannot delete yourself." )
469452 try :
470453 await admin_model .delete_model (id )
471- except (ValueError , TypeError ):
472- raise AdminApiException (404 , detail = f"{ model } not found." ) from None
454+ except Exception as e :
455+ logger .error ("Error deleting %s %s: %s" , model , id , e )
456+ raise AdminApiException (500 , detail = f"Error deleting { model } { id } : { e } " ) from e
473457 return id
474458
475459 async def action (
0 commit comments