-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.py
executable file
·149 lines (108 loc) · 4.39 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import uuid
from flask_restplus import Resource, reqparse
from flask import request
import json
from common.log import logger
import flask_restplus
from flask_restplus import fields
from extensions import api
class MyRequestParser(reqparse.RequestParser):
"""自定义参数请求"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class BaseResource(Resource):
default_help = '不是一个有效值'
# tid_file_dir = Config._REPORT_FILES_DIR
def __init__(self, *args, **kwargs):
self.parser = MyRequestParser()
logger.info(request.remote_addr + '请求参数:' + str(request.json))
super().__init__(*args, **kwargs)
# 自定义请求体检验
class MyBaseResource(Resource):
def __init__(self, *args, **kwargs):
pass
def get_params(self):
try:
params = request.form
if not params:
params = json.loads(request.data.decode())
except:
flask_restplus.abort(400, message='请求体格式有误')
logger.info(request.remote_addr + '请求参数:' + str(params))
return params
class ResponseResult:
@staticmethod
def success(msg, data):
return {"code": 200, "status": 'true', "msg": msg, "data": data}
@staticmethod
def error(code=500, msg=None):
return {"code": code, "status": 'false', "msg": msg}
baseResponse = api.model('baseResponse', {
'code': fields.Integer(example=200, description='错误码'),
'status': fields.Boolean(description='请求是否成功标识'),
'msg': fields.String(description='错误信息'),
})
class CusObj(fields.Raw):
def format(self, value):
return value
class ResultSuccess:
def __init__(self, code=200, msg='success', data=None):
self.code = code
self.success = True
self.msg = msg
self.data = data
class ResultError:
def __init__(self, code=500, msg='error', data=None):
self.code = code
self.success = False
self.msg = msg
self.data = data
class Result:
NEW = 'NEW'
SUBMITTED = 'SUBMITTED'
PROCESSING = 'PROCESSING'
FINISHED = 'FINISHED'
ERROR = 'ERROR'
def __init__(self, request_id, status=None):
self.request_id = request_id
self.status = status if status else type(self).NEW
def to_json_dict(self):
return {
'request_id': str(self.request_id),
'status': self.status
}
def __repr__(self):
return '<Result[request_id: %s, status: %s]>' % (self.request_id, self.status)
def __str__(self):
return self.__repr__()
def __eq__(self, other):
if type(self) != type(other):
return False
return self.request_id == other.request_id and self.status == other.status
class GetUUID:
@staticmethod
def getTaskId(data):
if data.get('request_id'):
task_id = data.get('request_id')
else:
uid = str(uuid.uuid1())
task_id = ''.join(uid.split('-'))
return task_id
def get_fun(fn):
return '.'.join([fn.__module__, fn.__name__])
model_type_request_params = ['manager_index_curve_time_period', 'manager_index_curve',
'represent_fund_select', 'represent_fund_curve', 'return_and_distribution',
'get_end_date', 'manager_interval_return',
'manager_interval_draw_down', 'manager_index_curve_time_period',
'return_and_risk_indicators', 'manager_size_time_period', 'manager_size',
'asset_distribution_end_date', 'asset_distribution',
'asset_distribution_change', 'stock_distribution_end_date',
'stock_distribution_time_period',
'max_industry_distribution', 'industry_distribution_change', 'max_stocks',
'max_stocks_changes',
'stocks_details_time_period', 'stocks_details', 'stocks_details_curves',
'bond_distribution_end_date', 'bond_distribution_time_period',
'bond_distribution', 'bond_distribution_changes', 'asset_distribution_end_date',
'manager_size_time_period', 'max_bonds', 'max_bonds_changes']