slider_field.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # coding=utf-8
  2. """
  3. @project: MaxKB
  4. @Author:虎
  5. @file: slider_field.py
  6. @date:2024/8/22 17:06
  7. @desc:
  8. """
  9. from typing import Dict
  10. from common.exception.app_exception import AppApiException
  11. from common.forms import BaseField, TriggerType, BaseLabel
  12. from django.utils.translation import gettext_lazy as _
  13. class SliderField(BaseField):
  14. """
  15. 滑块输入框
  16. """
  17. def __init__(self, label: str or BaseLabel,
  18. _min,
  19. _max,
  20. _step,
  21. precision,
  22. required: bool = False,
  23. default_value=None,
  24. relation_show_field_dict: Dict = None,
  25. attrs=None, props_info=None):
  26. """
  27. @param label: 提示
  28. @param _min: 最小值
  29. @param _max: 最大值
  30. @param _step: 步长
  31. @param precision: 保留多少小数
  32. @param required: 是否必填
  33. @param default_value: 默认值
  34. @param relation_show_field_dict:
  35. @param attrs:
  36. @param props_info:
  37. """
  38. _attrs = {'min': _min, 'max': _max, 'step': _step,
  39. 'precision': precision, 'show-input-controls': False, 'show-input': True}
  40. if attrs is not None:
  41. _attrs.update(attrs)
  42. super().__init__('Slider', label, required, default_value, relation_show_field_dict,
  43. {},
  44. TriggerType.OPTION_LIST, _attrs, props_info)
  45. def is_valid(self, value):
  46. super().is_valid(value)
  47. field_label = self.label.label if hasattr(self.label, 'to_dict') else self.label
  48. if value is not None:
  49. if value < self.attrs.get('min'):
  50. raise AppApiException(500,
  51. _("The {field_label} cannot be less than {min}").format(field_label=field_label,
  52. min=self.attrs.get(
  53. 'min')))
  54. if value > self.attrs.get('max'):
  55. raise AppApiException(500,
  56. _("The {field_label} cannot be greater than {max}").format(
  57. field_label=field_label,
  58. max=self.attrs.get(
  59. 'max')))