Skip to content

Commit f385bb3

Browse files
committed
refactor: rename validation method to follow naming conventions
1 parent 4715e4d commit f385bb3

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

apps/common/utils/tool_code.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _createdir(self):
3838
os.umask(old_mask)
3939

4040
def exec_code(self, code_str, keywords):
41-
self.validateBannedKeywords(code_str)
41+
self.validate_banned_keywords(code_str)
4242
_id = str(uuid.uuid7())
4343
success = '{"code":200,"msg":"成功","data":exec_result}'
4444
err = '{"code":500,"msg":str(e),"data":None}'
@@ -96,7 +96,7 @@ def _exec_sandbox(self, _code, _id):
9696
os.remove(exec_python_file)
9797
return subprocess_result
9898

99-
def validateBannedKeywords(self, code_str):
99+
def validate_banned_keywords(self, code_str):
100100
matched = next((bad for bad in self.banned_keywords if bad in code_str), None)
101101
if matched:
102102
raise Exception(f"keyword '{matched}' is banned in the tool.")

apps/tools/serializers/tool.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ def insert(self, instance, with_valid=True):
200200
if with_valid:
201201
self.is_valid(raise_exception=True)
202202
ToolCreateRequest(data=instance).is_valid(raise_exception=True)
203+
# 校验代码是否包括禁止的关键字
204+
ToolExecutor().validate_banned_keywords(instance.get('code', ''))
203205
tool_id = uuid.uuid7()
204206
Tool(
205207
id=tool_id,
@@ -312,6 +314,8 @@ def edit(self, instance, with_valid=True):
312314
if with_valid:
313315
self.is_valid(raise_exception=True)
314316
ToolEditRequest(data=instance).is_valid(raise_exception=True)
317+
# 校验代码是否包括禁止的关键字
318+
ToolExecutor().validate_banned_keywords(instance.get('code', ''))
315319
if not QuerySet(Tool).filter(id=self.data.get('id')).exists():
316320
raise serializers.ValidationError(_('Tool not found'))
317321

0 commit comments

Comments
 (0)