forked from 1Panel-dev/MaxKB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_field.py
156 lines (140 loc) · 6.57 KB
/
base_field.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
150
151
152
153
154
155
156
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: base_field.py
@date:2023/10/31 18:07
@desc:
"""
from enum import Enum
from typing import List, Dict
from common.exception.app_exception import AppApiException
from common.forms.label.base_label import BaseLabel
class TriggerType(Enum):
# 执行函数获取 OptionList数据
OPTION_LIST = 'OPTION_LIST'
# 执行函数获取子表单
CHILD_FORMS = 'CHILD_FORMS'
class BaseField:
def __init__(self,
input_type: str,
label: str or BaseLabel,
required: bool = False,
default_value: object = None,
relation_show_field_dict: Dict = None,
relation_trigger_field_dict: Dict = None,
trigger_type: TriggerType = TriggerType.OPTION_LIST,
attrs: Dict[str, object] = None,
props_info: Dict[str, object] = None):
"""
:param input_type: 字段
:param label: 提示
:param default_value: 默认值
:param relation_show_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才显示
:param relation_trigger_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才 执行函数获取 数据
:param trigger_type: 执行器类型 OPTION_LIST请求Option_list数据 CHILD_FORMS请求子表单
:param attrs: 前端attr数据
:param props_info: 其他额外信息
"""
if props_info is None:
props_info = {}
if attrs is None:
attrs = {}
self.label = label
self.attrs = attrs
self.props_info = props_info
self.default_value = default_value
self.input_type = input_type
self.relation_show_field_dict = {} if relation_show_field_dict is None else relation_show_field_dict
self.relation_trigger_field_dict = [] if relation_trigger_field_dict is None else relation_trigger_field_dict
self.required = required
self.trigger_type = trigger_type
def is_valid(self, value):
field_label = self.label.label if hasattr(self.label, 'to_dict') else self.label
if self.required and value is None:
raise AppApiException(500,
f"{field_label} 为必填参数")
def to_dict(self, **kwargs):
return {
'input_type': self.input_type,
'label': self.label.to_dict(**kwargs) if hasattr(self.label, 'to_dict') else self.label,
'required': self.required,
'default_value': self.default_value,
'relation_show_field_dict': self.relation_show_field_dict,
'relation_trigger_field_dict': self.relation_trigger_field_dict,
'trigger_type': self.trigger_type.value,
'attrs': self.attrs,
'props_info': self.props_info,
**kwargs
}
class BaseDefaultOptionField(BaseField):
def __init__(self, input_type: str,
label: str,
text_field: str,
value_field: str,
option_list: List[dict],
required: bool = False,
default_value: object = None,
relation_show_field_dict: Dict[str, object] = None,
attrs: Dict[str, object] = None,
props_info: Dict[str, object] = None):
"""
:param input_type: 字段
:param label: label
:param text_field: 文本字段
:param value_field: 值字段
:param option_list: 可选列表
:param required: 是否必填
:param default_value: 默认值
:param relation_show_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才显示
:param attrs: 前端attr数据
:param props_info: 其他额外信息
"""
super().__init__(input_type, label, required, default_value, relation_show_field_dict,
{}, TriggerType.OPTION_LIST, attrs, props_info)
self.text_field = text_field
self.value_field = value_field
self.option_list = option_list
def to_dict(self, **kwargs):
return {**super().to_dict(**kwargs), 'text_field': self.text_field, 'value_field': self.value_field,
'option_list': self.option_list}
class BaseExecField(BaseField):
def __init__(self,
input_type: str,
label: str,
text_field: str,
value_field: str,
provider: str,
method: str,
required: bool = False,
default_value: object = None,
relation_show_field_dict: Dict = None,
relation_trigger_field_dict: Dict = None,
trigger_type: TriggerType = TriggerType.OPTION_LIST,
attrs: Dict[str, object] = None,
props_info: Dict[str, object] = None):
"""
:param input_type: 字段
:param label: 提示
:param text_field: 文本字段
:param value_field: 值字段
:param provider: 指定供应商
:param method: 执行供应商函数 method
:param required: 是否必填
:param default_value: 默认值
:param relation_show_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才显示
:param relation_trigger_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才 执行函数获取 数据
:param trigger_type: 执行器类型 OPTION_LIST请求Option_list数据 CHILD_FORMS请求子表单
:param attrs: 前端attr数据
:param props_info: 其他额外信息
"""
super().__init__(input_type, label, required, default_value, relation_show_field_dict,
relation_trigger_field_dict,
trigger_type, attrs, props_info)
self.text_field = text_field
self.value_field = value_field
self.provider = provider
self.method = method
def to_dict(self, **kwargs):
return {**super().to_dict(**kwargs), 'text_field': self.text_field, 'value_field': self.value_field,
'provider': self.provider, 'method': self.method}