@@ -903,20 +903,16 @@ def to_internal_value(self, data):
903
903
904
904
# Number types...
905
905
906
- class IntegerField ( Field ):
906
+ class MaxMinMixin ( object ):
907
907
default_error_messages = {
908
- 'invalid' : _ ('A valid integer is required.' ),
909
908
'max_value' : _ ('Ensure this value is less than or equal to {max_value}.' ),
910
909
'min_value' : _ ('Ensure this value is greater than or equal to {min_value}.' ),
911
- 'max_string_length' : _ ('String value too large.' )
912
910
}
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'
915
911
916
912
def __init__ (self , ** kwargs ):
917
913
self .max_value = kwargs .pop ('max_value' , None )
918
914
self .min_value = kwargs .pop ('min_value' , None )
919
- super (IntegerField , self ).__init__ (** kwargs )
915
+ super (MaxMinMixin , self ).__init__ (** kwargs )
920
916
if self .max_value is not None :
921
917
message = lazy (
922
918
self .error_messages ['max_value' ].format ,
@@ -930,6 +926,15 @@ def __init__(self, **kwargs):
930
926
self .validators .append (
931
927
MinValueValidator (self .min_value , message = message ))
932
928
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
+
933
938
def to_internal_value (self , data ):
934
939
if isinstance (data , six .text_type ) and len (data ) > self .MAX_STRING_LENGTH :
935
940
self .fail ('max_string_length' )
@@ -944,32 +949,13 @@ def to_representation(self, value):
944
949
return int (value )
945
950
946
951
947
- class FloatField (Field ):
952
+ class FloatField (MaxMinMixin , Field ):
948
953
default_error_messages = {
949
954
'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}.' ),
952
955
'max_string_length' : _ ('String value too large.' )
953
956
}
954
957
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
955
958
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
-
973
959
def to_internal_value (self , data ):
974
960
975
961
if isinstance (data , six .text_type ) and len (data ) > self .MAX_STRING_LENGTH :
@@ -984,11 +970,9 @@ def to_representation(self, value):
984
970
return float (value )
985
971
986
972
987
- class DecimalField (Field ):
973
+ class DecimalField (MaxMinMixin , Field ):
988
974
default_error_messages = {
989
975
'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}.' ),
992
976
'max_digits' : _ ('Ensure that there are no more than {max_digits} digits in total.' ),
993
977
'max_decimal_places' : _ ('Ensure that there are no more than {max_decimal_places} decimal places.' ),
994
978
'max_whole_digits' : _ ('Ensure that there are no more than {max_whole_digits} digits before the decimal point.' ),
@@ -1006,28 +990,12 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
1006
990
if self .localize :
1007
991
self .coerce_to_string = True
1008
992
1009
- self .max_value = max_value
1010
- self .min_value = min_value
1011
-
1012
993
if self .max_digits is not None and self .decimal_places is not None :
1013
994
self .max_whole_digits = self .max_digits - self .decimal_places
1014
995
else :
1015
996
self .max_whole_digits = None
1016
997
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 ))
998
+ super (DecimalField , self ).__init__ (max_value = max_value , min_value = min_value , ** kwargs )
1031
999
1032
1000
if rounding is not None :
1033
1001
valid_roundings = [v for k , v in vars (decimal ).items () if k .startswith ('ROUND_' )]
@@ -1351,7 +1319,7 @@ def to_representation(self, value):
1351
1319
return value .strftime (output_format )
1352
1320
1353
1321
1354
- class DurationField (Field ):
1322
+ class DurationField (MaxMinMixin , Field ):
1355
1323
default_error_messages = {
1356
1324
'invalid' : _ ('Duration has wrong format. Use one of these formats instead: {format}.' ),
1357
1325
}
0 commit comments