56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
import json
|
|
import os
|
|
from temp_api.settings import BASE_DIR
|
|
from depend_main.functions.abstractFactoryFunction import create_class, create_class_from_db
|
|
import random
|
|
import requests
|
|
|
|
root = os.path.dirname(os.path.dirname(BASE_DIR))
|
|
|
|
|
|
# Create your views here.
|
|
class BaseApiView(APIView):
|
|
def post(self, request):
|
|
if request.content_type.startswith('multipart/form-data'):
|
|
request.data["api_info"] = json.loads(request.data["api_info"])
|
|
view_path = os.path.join(BASE_DIR, 'api_config_folder',
|
|
request.data["api_info"]['api_name_en'] + "_view_function.json")
|
|
# 预先载入需要使用的元对象
|
|
ins_dict = {}
|
|
for item in request.data["api_info"]["witch_meta_class"]:
|
|
if len(item["meta_class_init_func"]) > 0:
|
|
init_func = item["meta_class_init_func"][0]
|
|
else:
|
|
init_func = None
|
|
re = create_class_from_db(item["meta_class_content"][0], item, init_func)
|
|
ins_dict[re.__name__] = re
|
|
with open(view_path, 'r') as json_file:
|
|
data = json.load(json_file)
|
|
if request.content_type.startswith('multipart/form-data'):
|
|
# TODO文件上传格式待处理
|
|
run_data = {
|
|
"ins_dict": ins_dict,
|
|
"file": request.data["zip"],
|
|
"out_data": {}
|
|
}
|
|
del request.data["zip"]
|
|
for k, v in request.data.items():
|
|
run_data[k] = v
|
|
else:
|
|
run_data = {
|
|
"ins_dict": ins_dict,
|
|
"process_data": request.data["process_data"],
|
|
"out_data": {}
|
|
}
|
|
user_code = ""
|
|
for item in data['function_code'].values():
|
|
user_code = user_code + '\n' + ins_dict["py_tree"].to_string(item)
|
|
run_obj_data = {
|
|
"in_data": run_data
|
|
}
|
|
# print(user_code)
|
|
exec(user_code, globals(), run_obj_data)
|
|
return Response(run_obj_data["in_data"]["out_data"])
|