refactor: rename validation method to follow naming conventions
This commit is contained in:
parent
4715e4d84e
commit
f385bb3aec
@ -38,7 +38,7 @@ class ToolExecutor:
|
|||||||
os.umask(old_mask)
|
os.umask(old_mask)
|
||||||
|
|
||||||
def exec_code(self, code_str, keywords):
|
def exec_code(self, code_str, keywords):
|
||||||
self.validateBannedKeywords(code_str)
|
self.validate_banned_keywords(code_str)
|
||||||
_id = str(uuid.uuid7())
|
_id = str(uuid.uuid7())
|
||||||
success = '{"code":200,"msg":"成功","data":exec_result}'
|
success = '{"code":200,"msg":"成功","data":exec_result}'
|
||||||
err = '{"code":500,"msg":str(e),"data":None}'
|
err = '{"code":500,"msg":str(e),"data":None}'
|
||||||
@ -96,7 +96,7 @@ except Exception as e:
|
|||||||
os.remove(exec_python_file)
|
os.remove(exec_python_file)
|
||||||
return subprocess_result
|
return subprocess_result
|
||||||
|
|
||||||
def validateBannedKeywords(self, code_str):
|
def validate_banned_keywords(self, code_str):
|
||||||
matched = next((bad for bad in self.banned_keywords if bad in code_str), None)
|
matched = next((bad for bad in self.banned_keywords if bad in code_str), None)
|
||||||
if matched:
|
if matched:
|
||||||
raise Exception(f"keyword '{matched}' is banned in the tool.")
|
raise Exception(f"keyword '{matched}' is banned in the tool.")
|
||||||
|
|||||||
@ -200,6 +200,8 @@ class ToolSerializer(serializers.Serializer):
|
|||||||
if with_valid:
|
if with_valid:
|
||||||
self.is_valid(raise_exception=True)
|
self.is_valid(raise_exception=True)
|
||||||
ToolCreateRequest(data=instance).is_valid(raise_exception=True)
|
ToolCreateRequest(data=instance).is_valid(raise_exception=True)
|
||||||
|
# 校验代码是否包括禁止的关键字
|
||||||
|
ToolExecutor().validate_banned_keywords(instance.get('code', ''))
|
||||||
tool_id = uuid.uuid7()
|
tool_id = uuid.uuid7()
|
||||||
Tool(
|
Tool(
|
||||||
id=tool_id,
|
id=tool_id,
|
||||||
@ -312,6 +314,8 @@ class ToolSerializer(serializers.Serializer):
|
|||||||
if with_valid:
|
if with_valid:
|
||||||
self.is_valid(raise_exception=True)
|
self.is_valid(raise_exception=True)
|
||||||
ToolEditRequest(data=instance).is_valid(raise_exception=True)
|
ToolEditRequest(data=instance).is_valid(raise_exception=True)
|
||||||
|
# 校验代码是否包括禁止的关键字
|
||||||
|
ToolExecutor().validate_banned_keywords(instance.get('code', ''))
|
||||||
if not QuerySet(Tool).filter(id=self.data.get('id')).exists():
|
if not QuerySet(Tool).filter(id=self.data.get('id')).exists():
|
||||||
raise serializers.ValidationError(_('Tool not found'))
|
raise serializers.ValidationError(_('Tool not found'))
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user