2015-01-28 20:35:27 +08:00
|
|
|
from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
|
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
from django import forms
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core import exceptions
|
2019-09-17 01:29:13 +08:00
|
|
|
from django.forms.widgets import HiddenInput, MultiWidget
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-01-11 00:14:20 +08:00
|
|
|
|
2017-05-21 05:05:48 +08:00
|
|
|
__all__ = [
|
2018-10-03 07:17:23 +08:00
|
|
|
'BaseRangeField', 'IntegerRangeField', 'DecimalRangeField',
|
2019-09-17 01:29:13 +08:00
|
|
|
'DateTimeRangeField', 'DateRangeField', 'HiddenRangeWidget', 'RangeWidget',
|
2017-05-21 05:05:48 +08:00
|
|
|
]
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
|
2019-09-17 01:29:13 +08:00
|
|
|
class RangeWidget(MultiWidget):
|
|
|
|
def __init__(self, base_widget, attrs=None):
|
|
|
|
widgets = (base_widget, base_widget)
|
|
|
|
super().__init__(widgets, attrs)
|
|
|
|
|
|
|
|
def decompress(self, value):
|
|
|
|
if value:
|
|
|
|
return (value.lower, value.upper)
|
|
|
|
return (None, None)
|
|
|
|
|
|
|
|
|
|
|
|
class HiddenRangeWidget(RangeWidget):
|
|
|
|
"""A widget that splits input into two <input type="hidden"> inputs."""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super().__init__(HiddenInput, attrs)
|
|
|
|
|
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
class BaseRangeField(forms.MultiValueField):
|
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _('Enter two valid values.'),
|
|
|
|
'bound_ordering': _('The start of the range must not exceed the end of the range.'),
|
|
|
|
}
|
2019-09-17 01:29:13 +08:00
|
|
|
hidden_widget = HiddenRangeWidget
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2015-12-27 04:11:53 +08:00
|
|
|
if 'widget' not in kwargs:
|
|
|
|
kwargs['widget'] = RangeWidget(self.base_field.widget)
|
|
|
|
if 'fields' not in kwargs:
|
|
|
|
kwargs['fields'] = [self.base_field(required=False), self.base_field(required=False)]
|
2015-01-11 00:14:20 +08:00
|
|
|
kwargs.setdefault('required', False)
|
|
|
|
kwargs.setdefault('require_all_fields', False)
|
2021-06-18 05:13:49 +08:00
|
|
|
self.range_kwargs = {}
|
|
|
|
if default_bounds := kwargs.pop('default_bounds', None):
|
|
|
|
self.range_kwargs = {'bounds': default_bounds}
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(**kwargs)
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def prepare_value(self, value):
|
2015-05-22 21:54:31 +08:00
|
|
|
lower_base, upper_base = self.fields
|
2015-01-11 00:14:20 +08:00
|
|
|
if isinstance(value, self.range_type):
|
2015-05-22 21:54:31 +08:00
|
|
|
return [
|
|
|
|
lower_base.prepare_value(value.lower),
|
|
|
|
upper_base.prepare_value(value.upper),
|
|
|
|
]
|
2015-01-11 00:14:20 +08:00
|
|
|
if value is None:
|
2015-05-22 21:54:31 +08:00
|
|
|
return [
|
|
|
|
lower_base.prepare_value(None),
|
|
|
|
upper_base.prepare_value(None),
|
|
|
|
]
|
2015-01-11 00:14:20 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
def compress(self, values):
|
|
|
|
if not values:
|
|
|
|
return None
|
|
|
|
lower, upper = values
|
|
|
|
if lower is not None and upper is not None and lower > upper:
|
|
|
|
raise exceptions.ValidationError(
|
|
|
|
self.error_messages['bound_ordering'],
|
|
|
|
code='bound_ordering',
|
|
|
|
)
|
|
|
|
try:
|
2021-06-18 05:13:49 +08:00
|
|
|
range_value = self.range_type(lower, upper, **self.range_kwargs)
|
2015-01-11 00:14:20 +08:00
|
|
|
except TypeError:
|
|
|
|
raise exceptions.ValidationError(
|
|
|
|
self.error_messages['invalid'],
|
|
|
|
code='invalid',
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return range_value
|
|
|
|
|
|
|
|
|
|
|
|
class IntegerRangeField(BaseRangeField):
|
2015-02-20 05:19:33 +08:00
|
|
|
default_error_messages = {'invalid': _('Enter two whole numbers.')}
|
2015-01-11 00:14:20 +08:00
|
|
|
base_field = forms.IntegerField
|
|
|
|
range_type = NumericRange
|
|
|
|
|
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
class DecimalRangeField(BaseRangeField):
|
2015-02-20 05:19:33 +08:00
|
|
|
default_error_messages = {'invalid': _('Enter two numbers.')}
|
2018-10-03 07:17:23 +08:00
|
|
|
base_field = forms.DecimalField
|
2015-01-11 00:14:20 +08:00
|
|
|
range_type = NumericRange
|
|
|
|
|
|
|
|
|
|
|
|
class DateTimeRangeField(BaseRangeField):
|
2015-02-20 05:19:33 +08:00
|
|
|
default_error_messages = {'invalid': _('Enter two valid date/times.')}
|
2015-01-11 00:14:20 +08:00
|
|
|
base_field = forms.DateTimeField
|
|
|
|
range_type = DateTimeTZRange
|
|
|
|
|
|
|
|
|
|
|
|
class DateRangeField(BaseRangeField):
|
2015-02-20 05:19:33 +08:00
|
|
|
default_error_messages = {'invalid': _('Enter two valid dates.')}
|
2015-01-11 00:14:20 +08:00
|
|
|
base_field = forms.DateField
|
|
|
|
range_type = DateRange
|