refac: introduce model id length limit
This commit is contained in:
@@ -38,6 +38,7 @@ class ERROR_MESSAGES(str, Enum):
|
|||||||
ID_TAKEN = "Uh-oh! This id is already registered. Please choose another id string."
|
ID_TAKEN = "Uh-oh! This id is already registered. Please choose another id string."
|
||||||
MODEL_ID_TAKEN = "Uh-oh! This model id is already registered. Please choose another model id string."
|
MODEL_ID_TAKEN = "Uh-oh! This model id is already registered. Please choose another model id string."
|
||||||
NAME_TAG_TAKEN = "Uh-oh! This name tag is already registered. Please choose another name tag string."
|
NAME_TAG_TAKEN = "Uh-oh! This name tag is already registered. Please choose another name tag string."
|
||||||
|
MODEL_ID_TOO_LONG = "The model id is too long. Please make sure your model id is less than 256 characters long."
|
||||||
|
|
||||||
INVALID_TOKEN = (
|
INVALID_TOKEN = (
|
||||||
"Your session has expired or the token is invalid. Please sign in again."
|
"Your session has expired or the token is invalid. Please sign in again."
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ log = logging.getLogger(__name__)
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
def validate_model_id(model_id: str) -> bool:
|
||||||
|
return model_id and len(model_id) <= 256
|
||||||
|
|
||||||
|
|
||||||
###########################
|
###########################
|
||||||
# GetModels
|
# GetModels
|
||||||
###########################
|
###########################
|
||||||
@@ -84,6 +88,12 @@ async def create_new_model(
|
|||||||
detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
|
detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not validate_model_id(form_data.id):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail=ERROR_MESSAGES.MODEL_ID_TOO_LONG,
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
model = Models.insert_new_model(form_data, user.id)
|
model = Models.insert_new_model(form_data, user.id)
|
||||||
if model:
|
if model:
|
||||||
@@ -124,7 +134,8 @@ async def import_models(
|
|||||||
for model_data in data:
|
for model_data in data:
|
||||||
# Here, you can add logic to validate model_data if needed
|
# Here, you can add logic to validate model_data if needed
|
||||||
model_id = model_data.get("id")
|
model_id = model_data.get("id")
|
||||||
if model_id:
|
|
||||||
|
if model_id and validate_model_id(model_id):
|
||||||
existing_model = Models.get_model_by_id(model_id)
|
existing_model = Models.get_model_by_id(model_id)
|
||||||
if existing_model:
|
if existing_model:
|
||||||
# Update existing model
|
# Update existing model
|
||||||
|
|||||||
Reference in New Issue
Block a user