base_field.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # coding=utf-8
  2. """
  3. @project: maxkb
  4. @Author:虎
  5. @file: base_field.py
  6. @date:2023/10/31 18:07
  7. @desc:
  8. """
  9. from enum import Enum
  10. from typing import List, Dict
  11. from common.exception.app_exception import AppApiException
  12. from common.forms.label.base_label import BaseLabel
  13. from django.utils.translation import gettext_lazy as _
  14. class TriggerType(Enum):
  15. # 执行函数获取 OptionList数据
  16. OPTION_LIST = 'OPTION_LIST'
  17. # 执行函数获取子表单
  18. CHILD_FORMS = 'CHILD_FORMS'
  19. class BaseField:
  20. def __init__(self,
  21. input_type: str,
  22. label: str or BaseLabel,
  23. required: bool = False,
  24. default_value: object = None,
  25. relation_show_field_dict: Dict = None,
  26. relation_trigger_field_dict: Dict = None,
  27. trigger_type: TriggerType = TriggerType.OPTION_LIST,
  28. attrs: Dict[str, object] = None,
  29. props_info: Dict[str, object] = None):
  30. """
  31. :param input_type: 字段
  32. :param label: 提示
  33. :param default_value: 默认值
  34. :param relation_show_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才显示
  35. :param relation_trigger_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才 执行函数获取 数据
  36. :param trigger_type: 执行器类型 OPTION_LIST请求Option_list数据 CHILD_FORMS请求子表单
  37. :param attrs: 前端attr数据
  38. :param props_info: 其他额外信息
  39. """
  40. if props_info is None:
  41. props_info = {}
  42. if attrs is None:
  43. attrs = {}
  44. self.label = label
  45. self.attrs = attrs
  46. self.props_info = props_info
  47. self.default_value = default_value
  48. self.input_type = input_type
  49. self.relation_show_field_dict = {} if relation_show_field_dict is None else relation_show_field_dict
  50. self.relation_trigger_field_dict = [] if relation_trigger_field_dict is None else relation_trigger_field_dict
  51. self.required = required
  52. self.trigger_type = trigger_type
  53. def is_valid(self, value):
  54. field_label = self.label.label if hasattr(self.label, 'to_dict') else self.label
  55. if self.required and value is None:
  56. raise AppApiException(500,
  57. _('The field {field_label} is required').format(field_label=field_label))
  58. def to_dict(self, **kwargs):
  59. return {
  60. 'input_type': self.input_type,
  61. 'label': self.label.to_dict(**kwargs) if hasattr(self.label, 'to_dict') else self.label,
  62. 'required': self.required,
  63. 'default_value': self.default_value,
  64. 'relation_show_field_dict': self.relation_show_field_dict,
  65. 'relation_trigger_field_dict': self.relation_trigger_field_dict,
  66. 'trigger_type': self.trigger_type.value,
  67. 'attrs': self.attrs,
  68. 'props_info': self.props_info,
  69. **kwargs
  70. }
  71. class BaseDefaultOptionField(BaseField):
  72. def __init__(self, input_type: str,
  73. label: str,
  74. text_field: str,
  75. value_field: str,
  76. option_list: List[dict],
  77. required: bool = False,
  78. default_value: object = None,
  79. relation_show_field_dict: Dict[str, object] = None,
  80. attrs: Dict[str, object] = None,
  81. props_info: Dict[str, object] = None):
  82. """
  83. :param input_type: 字段
  84. :param label: label
  85. :param text_field: 文本字段
  86. :param value_field: 值字段
  87. :param option_list: 可选列表
  88. :param required: 是否必填
  89. :param default_value: 默认值
  90. :param relation_show_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才显示
  91. :param attrs: 前端attr数据
  92. :param props_info: 其他额外信息
  93. """
  94. super().__init__(input_type, label, required, default_value, relation_show_field_dict,
  95. {}, TriggerType.OPTION_LIST, attrs, props_info)
  96. self.text_field = text_field
  97. self.value_field = value_field
  98. self.option_list = option_list
  99. def to_dict(self, **kwargs):
  100. return {**super().to_dict(**kwargs), 'text_field': self.text_field, 'value_field': self.value_field,
  101. 'option_list': self.option_list}
  102. class BaseExecField(BaseField):
  103. def __init__(self,
  104. input_type: str,
  105. label: str,
  106. text_field: str,
  107. value_field: str,
  108. provider: str,
  109. method: str,
  110. required: bool = False,
  111. default_value: object = None,
  112. relation_show_field_dict: Dict = None,
  113. relation_trigger_field_dict: Dict = None,
  114. trigger_type: TriggerType = TriggerType.OPTION_LIST,
  115. attrs: Dict[str, object] = None,
  116. props_info: Dict[str, object] = None):
  117. """
  118. :param input_type: 字段
  119. :param label: 提示
  120. :param text_field: 文本字段
  121. :param value_field: 值字段
  122. :param provider: 指定供应商
  123. :param method: 执行供应商函数 method
  124. :param required: 是否必填
  125. :param default_value: 默认值
  126. :param relation_show_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才显示
  127. :param relation_trigger_field_dict: {field:field_value_list} 表示在 field有值 ,并且值在field_value_list中才 执行函数获取 数据
  128. :param trigger_type: 执行器类型 OPTION_LIST请求Option_list数据 CHILD_FORMS请求子表单
  129. :param attrs: 前端attr数据
  130. :param props_info: 其他额外信息
  131. """
  132. super().__init__(input_type, label, required, default_value, relation_show_field_dict,
  133. relation_trigger_field_dict,
  134. trigger_type, attrs, props_info)
  135. self.text_field = text_field
  136. self.value_field = value_field
  137. self.provider = provider
  138. self.method = method
  139. def to_dict(self, **kwargs):
  140. return {**super().to_dict(**kwargs), 'text_field': self.text_field, 'value_field': self.value_field,
  141. 'provider': self.provider, 'method': self.method}