fix: The interface for obtaining model metadata cannot access public models#2787
fix: The interface for obtaining model metadata cannot access public models#2787shaohuzhang1 merged 1 commit intomainfrom
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| model = QuerySet(Model).get(id=self.data.get('id')) | ||
| return {'id': str(model.id), 'provider': model.provider, 'name': model.name, 'model_type': model.model_type, | ||
| 'model_name': model.model_name, | ||
| 'status': model.status, |
There was a problem hiding this comment.
The provided code snippet has a few potential issues and optimizations:
-
Duplicate
ifblock:if model is None: model = QuerySet(Model).get(id=self.data.get('id'))
This duplicated
ifcondition can be removed. It's already covered by the outerif with_valid: .... Ifwith_validis true, it will have checked for existence earlier. -
String comparison efficiency:
if str(model.user_id) != str(self.data.get("user_id")):
String comparisons like this should be avoided unless absolutely necessary because they involve type conversion. Instead, you could compare the actual values directly:
if model.user_id != self.data.get("user_id"):
-
Code readability:
- The code uses different spaces between operators (
==,!=) which might look inconsistent. - Adding consistent spacing can improve readability.
- The code uses different spaces between operators (
-
Redundant checks:
if model is None: raise AppApiException(500, _('Model does not exist')
You don't always need to raise an exception when a model doesn't exist; simply returning
Noneor handling it gracefully might be better depending on its context.
Here’s a revised version of the function with these improvements:
@@ -313,18 +313,21 @@ def one(self, with_valid=False):
return ModelSerializer.model_to_dict(model)
def one_meta(self, with_valid=False):
if with_valid:
super().is_valid(raise_exception=True)
model_id = self.data.get('id')
try:
model = Model.objects.filter(id=model_id).first()
if not model:
raise AppApiException(500, _('Model does not exist'))
if model.permission_type == 'PRIVATE' and model.user_id != self.data.get("user_id"):
raise Exception(_('No permission to use this model') + f" ({model.name})")
except Exception as e:
handle_exception(e)
model_id = self.data.get('id')
if isinstance(model_id, int): # Ensure model_id is an integer
model = Model.objects.get(id=model_id)
else:
raise ValueError(_('Invalid model ID'))
return {
'id': str(model.id),
'provider': model.provider,
'name': model.name,
'model_type': model.model_type,
'model_name': model.model_name,
'status': model.status
}Key points:
- Removed unnecessary duplicate
ifblocks. - Simplified string comparison by avoiding type conversion unless needed.
- Added additional logic to ensure that
self.data['id']is an integer before callingQuerySet.get(). - Improved overall code readability and maintainability through consistent formatting and proper separation of concerns.
fix: The interface for obtaining model metadata cannot access public models