|
20 | 20 | from common.field.common import UploadedImageField |
21 | 21 | from common.result import result |
22 | 22 | from common.utils.common import get_file_content |
| 23 | +from common.utils.rsa_util import rsa_long_decrypt, rsa_long_encrypt |
23 | 24 | from common.utils.tool_code import ToolExecutor |
24 | 25 | from knowledge.models import File, FileSourceType |
25 | 26 | from maxkb.const import CONFIG, PROJECT_DIR |
@@ -289,17 +290,45 @@ def edit(self, instance, with_valid=True): |
289 | 290 | edit_dict = {field: instance.get(field) for field in edit_field_list if ( |
290 | 291 | field in instance and instance.get(field) is not None)} |
291 | 292 |
|
| 293 | + tool = QuerySet(Tool).filter(id=self.data.get('id')).first() |
| 294 | + if 'init_params' in edit_dict: |
| 295 | + if edit_dict['init_field_list'] is not None: |
| 296 | + rm_key = [] |
| 297 | + for key in edit_dict['init_params']: |
| 298 | + if key not in [field['field'] for field in edit_dict['init_field_list']]: |
| 299 | + rm_key.append(key) |
| 300 | + for key in rm_key: |
| 301 | + edit_dict['init_params'].pop(key) |
| 302 | + if tool.init_params: |
| 303 | + old_init_params = json.loads(rsa_long_decrypt(tool.init_params)) |
| 304 | + for key in edit_dict['init_params']: |
| 305 | + if key in old_init_params and edit_dict['init_params'][key] == encryption(old_init_params[key]): |
| 306 | + edit_dict['init_params'][key] = old_init_params[key] |
| 307 | + edit_dict['init_params'] = rsa_long_encrypt(json.dumps(edit_dict['init_params'])) |
| 308 | + |
292 | 309 | QuerySet(Tool).filter(id=self.data.get('id')).update(**edit_dict) |
293 | 310 |
|
294 | 311 | return self.one() |
295 | 312 |
|
296 | 313 | def delete(self): |
297 | 314 | self.is_valid(raise_exception=True) |
| 315 | + tool = QuerySet(Tool).filter(id=self.data.get('id')).first() |
| 316 | + if tool.template_id is None and tool.icon != '/ui/favicon.ico': |
| 317 | + QuerySet(File).filter(id=tool.icon.split('/')[-1]).delete() |
298 | 318 | QuerySet(Tool).filter(id=self.data.get('id')).delete() |
299 | 319 |
|
300 | 320 | def one(self): |
301 | 321 | self.is_valid(raise_exception=True) |
302 | 322 | tool = QuerySet(Tool).filter(id=self.data.get('id')).first() |
| 323 | + if tool.init_params: |
| 324 | + tool.init_params = json.loads(rsa_long_decrypt(tool.init_params)) |
| 325 | + if tool.init_field_list: |
| 326 | + password_fields = [i["field"] for i in tool.init_field_list if |
| 327 | + i.get("input_type") == "PasswordInput"] |
| 328 | + if tool.init_params: |
| 329 | + for k in tool.init_params: |
| 330 | + if k in password_fields and tool.init_params[k]: |
| 331 | + tool.init_params[k] = encryption(tool.init_params[k]) |
303 | 332 | return ToolModelSerializer(tool).data |
304 | 333 |
|
305 | 334 | def export(self): |
|
0 commit comments