61 lines
2.7 KiB
Python
61 lines
2.7 KiB
Python
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
from zero_code_depencey.dep.tree.treeClass import PythonCodeTreeController
|
|
from zero_code_depencey.dep.fileManage.file import FileContent
|
|
import json
|
|
import os
|
|
from Template_service.settings import BASE_DIR
|
|
from zero_code_depencey.dep.Validators.permissionValidator import url_required
|
|
config_file_path = os.path.join(BASE_DIR, 'dependency', 'api_config', 'project_info.json')
|
|
with open(config_file_path, "r") as config_file:
|
|
config = json.load(config_file)
|
|
splits = FileContent.judge_system()
|
|
path_list = str(BASE_DIR).split(splits)
|
|
is_use_valid = False
|
|
if "apis" in path_list:
|
|
index = path_list.index("apis")
|
|
path = splits.join(path_list[:index])
|
|
project_apis_configs_path = os.path.join(path, "project_info_config", "project_info_config", "apis.json")
|
|
if os.path.exists(project_apis_configs_path):
|
|
with open(project_apis_configs_path, 'r') as all_setting:
|
|
all_project_settings = json.load(all_setting)
|
|
is_use_valid = bool(all_project_settings["is_use_valid"] == 'True')
|
|
|
|
# Create your views here.
|
|
class BaseApiView(APIView):
|
|
@url_required(config["api_status_one"], is_use_valid)
|
|
def post(self, request):
|
|
# TODO python代码组装
|
|
view_path = os.path.join(BASE_DIR, 'dependency', 'api_config', 'views_function.json')
|
|
with open(view_path, 'r') as json_file:
|
|
data = json.load(json_file)
|
|
if request.content_type.startswith('multipart/form-data'):
|
|
run_data = request.data
|
|
else:
|
|
run_data = request.data['submit_data']['data']
|
|
user_code = ""
|
|
for item in data['function_code'].values():
|
|
user_code = user_code + '\n' + PythonCodeTreeController.to_string(item)
|
|
# TODO 以请求的数据字典中第一层的键值对作为传入变量
|
|
run_obj_data = {
|
|
"in_data": run_data
|
|
}
|
|
exec(user_code, globals(), run_obj_data)
|
|
# TODO 输出值按输出信息定义获取
|
|
if 'data' in run_obj_data['out_data']:
|
|
run_data['data'] = run_obj_data['out_data']['data']
|
|
# run_data = {**run_data, **run_obj_data['out_data']['data']}
|
|
if 'code' in run_obj_data['out_data']:
|
|
run_data['code'] = run_obj_data['out_data']['code']
|
|
if 'msg' in run_obj_data['out_data']:
|
|
run_data['msg'] = run_obj_data['out_data']['msg']
|
|
# TODO 返回响应
|
|
return Response({
|
|
"return_data": {
|
|
"data": {
|
|
'status': run_data.get('code', 200),
|
|
'msg': run_data.get('msg', 'error'),
|
|
'data': run_data.get('data', {})
|
|
}
|
|
}
|
|
}) |