Skip to content

Commit 4537444

Browse files
committed
Remove mixin
1 parent d1822ec commit 4537444

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

rest_framework/fields.py

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -903,16 +903,20 @@ def to_internal_value(self, data):
903903

904904
# Number types...
905905

906-
class MaxMinMixin(object):
906+
class IntegerField(Field):
907907
default_error_messages = {
908+
'invalid': _('A valid integer is required.'),
908909
'max_value': _('Ensure this value is less than or equal to {max_value}.'),
909910
'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
911+
'max_string_length': _('String value too large.')
910912
}
913+
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
914+
re_decimal = re.compile(r'\.0*\s*$') # allow e.g. '1.0' as an int, but not '1.2'
911915

912916
def __init__(self, **kwargs):
913917
self.max_value = kwargs.pop('max_value', None)
914918
self.min_value = kwargs.pop('min_value', None)
915-
super(MaxMinMixin, self).__init__(**kwargs)
919+
super(IntegerField, self).__init__(**kwargs)
916920
if self.max_value is not None:
917921
message = lazy(
918922
self.error_messages['max_value'].format,
@@ -926,15 +930,6 @@ def __init__(self, **kwargs):
926930
self.validators.append(
927931
MinValueValidator(self.min_value, message=message))
928932

929-
930-
class IntegerField(MaxMinMixin, Field):
931-
default_error_messages = {
932-
'invalid': _('A valid integer is required.'),
933-
'max_string_length': _('String value too large.')
934-
}
935-
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
936-
re_decimal = re.compile(r'\.0*\s*$') # allow e.g. '1.0' as an int, but not '1.2'
937-
938933
def to_internal_value(self, data):
939934
if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH:
940935
self.fail('max_string_length')
@@ -949,13 +944,32 @@ def to_representation(self, value):
949944
return int(value)
950945

951946

952-
class FloatField(MaxMinMixin, Field):
947+
class FloatField(Field):
953948
default_error_messages = {
954949
'invalid': _('A valid number is required.'),
950+
'max_value': _('Ensure this value is less than or equal to {max_value}.'),
951+
'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
955952
'max_string_length': _('String value too large.')
956953
}
957954
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
958955

956+
def __init__(self, **kwargs):
957+
self.max_value = kwargs.pop('max_value', None)
958+
self.min_value = kwargs.pop('min_value', None)
959+
super(FloatField, self).__init__(**kwargs)
960+
if self.max_value is not None:
961+
message = lazy(
962+
self.error_messages['max_value'].format,
963+
six.text_type)(max_value=self.max_value)
964+
self.validators.append(
965+
MaxValueValidator(self.max_value, message=message))
966+
if self.min_value is not None:
967+
message = lazy(
968+
self.error_messages['min_value'].format,
969+
six.text_type)(min_value=self.min_value)
970+
self.validators.append(
971+
MinValueValidator(self.min_value, message=message))
972+
959973
def to_internal_value(self, data):
960974

961975
if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH:
@@ -970,9 +984,11 @@ def to_representation(self, value):
970984
return float(value)
971985

972986

973-
class DecimalField(MaxMinMixin, Field):
987+
class DecimalField(Field):
974988
default_error_messages = {
975989
'invalid': _('A valid number is required.'),
990+
'max_value': _('Ensure this value is less than or equal to {max_value}.'),
991+
'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
976992
'max_digits': _('Ensure that there are no more than {max_digits} digits in total.'),
977993
'max_decimal_places': _('Ensure that there are no more than {max_decimal_places} decimal places.'),
978994
'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.'),
@@ -990,12 +1006,28 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
9901006
if self.localize:
9911007
self.coerce_to_string = True
9921008

1009+
self.max_value = max_value
1010+
self.min_value = min_value
1011+
9931012
if self.max_digits is not None and self.decimal_places is not None:
9941013
self.max_whole_digits = self.max_digits - self.decimal_places
9951014
else:
9961015
self.max_whole_digits = None
9971016

998-
super(DecimalField, self).__init__(max_value=max_value, min_value=min_value, **kwargs)
1017+
super(DecimalField, self).__init__(**kwargs)
1018+
1019+
if self.max_value is not None:
1020+
message = lazy(
1021+
self.error_messages['max_value'].format,
1022+
six.text_type)(max_value=self.max_value)
1023+
self.validators.append(
1024+
MaxValueValidator(self.max_value, message=message))
1025+
if self.min_value is not None:
1026+
message = lazy(
1027+
self.error_messages['min_value'].format,
1028+
six.text_type)(min_value=self.min_value)
1029+
self.validators.append(
1030+
MinValueValidator(self.min_value, message=message))
9991031

10001032
if rounding is not None:
10011033
valid_roundings = [v for k, v in vars(decimal).items() if k.startswith('ROUND_')]
@@ -1319,11 +1351,30 @@ def to_representation(self, value):
13191351
return value.strftime(output_format)
13201352

13211353

1322-
class DurationField(MaxMinMixin, Field):
1354+
class DurationField(Field):
13231355
default_error_messages = {
13241356
'invalid': _('Duration has wrong format. Use one of these formats instead: {format}.'),
1357+
'max_value': _('Ensure this value is less than or equal to {max_value}.'),
1358+
'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
13251359
}
13261360

1361+
def __init__(self, **kwargs):
1362+
self.max_value = kwargs.pop('max_value', None)
1363+
self.min_value = kwargs.pop('min_value', None)
1364+
super(DurationField, self).__init__(**kwargs)
1365+
if self.max_value is not None:
1366+
message = lazy(
1367+
self.error_messages['max_value'].format,
1368+
six.text_type)(max_value=self.max_value)
1369+
self.validators.append(
1370+
MaxValueValidator(self.max_value, message=message))
1371+
if self.min_value is not None:
1372+
message = lazy(
1373+
self.error_messages['min_value'].format,
1374+
six.text_type)(min_value=self.min_value)
1375+
self.validators.append(
1376+
MinValueValidator(self.min_value, message=message))
1377+
13271378
def to_internal_value(self, value):
13281379
if isinstance(value, datetime.timedelta):
13291380
return value

tests/test_fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ class TestNoOutputFormatTimeField(FieldValues):
14611461

14621462
class TestMinMaxDurationField(FieldValues):
14631463
"""
1464-
Valid and invalid values for `IntegerField` with min and max limits.
1464+
Valid and invalid values for `DurationField` with min and max limits.
14651465
"""
14661466
valid_inputs = {
14671467
'3 08:32:01.000123': datetime.timedelta(days=3, hours=8, minutes=32, seconds=1, microseconds=123),

0 commit comments

Comments
 (0)