Python django.core.validators.MinValueValidator() Examples
The following are 30
code examples of django.core.validators.MinValueValidator().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.core.validators
, or try the search function
.
Example #1
Source File: test_pods.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def test_sets_up_fields_based_on_pod_no_max_cpu_speed(self): request = MagicMock() pod = make_pod_with_hints() pod.hints.cpu_speed = 0 pod.save() form = ComposeMachineForm(request=request, pod=pod) self.assertThat( form.fields["cpu_speed"], MatchesStructure( required=Equals(False), validators=MatchesSetwise( MatchesAll( IsInstance(MinValueValidator), MatchesStructure(limit_value=Equals(300)), ) ), ), )
Example #2
Source File: fields.py From django-htk with MIT License | 6 votes |
def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, *args, **kwargs): self.min_value = min_value self.max_value = max_value validators = kwargs.pop('validators', []) if min_value: validators.append(MinValueValidator(min_value)) if max_value: validators.append(MaxValueValidator(max_value)) return super(IntegerRangeField, self).__init__( verbose_name=verbose_name, name=name, validators=validators, *args, **kwargs )
Example #3
Source File: __init__.py From python2017 with MIT License | 6 votes |
def validators(self): # These validators can't be added at field initialization time since # they're based on values retrieved from `connection`. validators_ = super(IntegerField, self).validators internal_type = self.get_internal_type() min_value, max_value = connection.ops.integer_field_range(internal_type) if min_value is not None: for validator in validators_: if isinstance(validator, validators.MinValueValidator) and validator.limit_value >= min_value: break else: validators_.append(validators.MinValueValidator(min_value)) if max_value is not None: for validator in validators_: if isinstance(validator, validators.MaxValueValidator) and validator.limit_value <= max_value: break else: validators_.append(validators.MaxValueValidator(max_value)) return validators_
Example #4
Source File: __init__.py From python with Apache License 2.0 | 6 votes |
def validators(self): # These validators can't be added at field initialization time since # they're based on values retrieved from `connection`. validators_ = super(IntegerField, self).validators internal_type = self.get_internal_type() min_value, max_value = connection.ops.integer_field_range(internal_type) if min_value is not None: for validator in validators_: if isinstance(validator, validators.MinValueValidator) and validator.limit_value >= min_value: break else: validators_.append(validators.MinValueValidator(min_value)) if max_value is not None: for validator in validators_: if isinstance(validator, validators.MaxValueValidator) and validator.limit_value <= max_value: break else: validators_.append(validators.MaxValueValidator(max_value)) return validators_
Example #5
Source File: serializers.py From esdc-ce with Apache License 2.0 | 6 votes |
def __init__(self, request, instance, *args, **kwargs): super(SnapshotDefineSerializer, self).__init__(request, instance, *args, **kwargs) if not kwargs.get('many', False): dc_settings = request.dc.settings # Limit maximum number of snapshots - Issue #chili-447 if dc_settings.VMS_VM_SNAPSHOT_LIMIT_AUTO is None: min_count, max_count = RETENTION_MIN, RETENTION_MAX else: min_count, max_count = 1, int(dc_settings.VMS_VM_SNAPSHOT_LIMIT_AUTO) self.fields['retention'].validators.append(validators.MinValueValidator(min_count)) self.fields['retention'].validators.append(validators.MaxValueValidator(max_count)) if instance.vm.is_kvm(): self._update_fields_ = list(self._update_fields_) self._update_fields_.append('fsfreeze')
Example #6
Source File: fields.py From esdc-ce with Apache License 2.0 | 6 votes |
def __init__(self, *args, **kwargs): try: self.model_field = kwargs.pop('model_field') except KeyError: raise ValueError("ModelField requires 'model_field' kwarg") self.min_length = kwargs.pop('min_length', getattr(self.model_field, 'min_length', None)) self.max_length = kwargs.pop('max_length', getattr(self.model_field, 'max_length', None)) self.min_value = kwargs.pop('min_value', getattr(self.model_field, 'min_value', None)) self.max_value = kwargs.pop('max_value', getattr(self.model_field, 'max_value', None)) super(ModelField, self).__init__(*args, **kwargs) if self.min_length is not None: self.validators.append(validators.MinLengthValidator(self.min_length)) if self.max_length is not None: self.validators.append(validators.MaxLengthValidator(self.max_length)) if self.min_value is not None: self.validators.append(validators.MinValueValidator(self.min_value)) if self.max_value is not None: self.validators.append(validators.MaxValueValidator(self.max_value))
Example #7
Source File: __init__.py From bioforum with MIT License | 6 votes |
def validators(self): # These validators can't be added at field initialization time since # they're based on values retrieved from `connection`. validators_ = super().validators internal_type = self.get_internal_type() min_value, max_value = connection.ops.integer_field_range(internal_type) if min_value is not None: for validator in validators_: if isinstance(validator, validators.MinValueValidator) and validator.limit_value >= min_value: break else: validators_.append(validators.MinValueValidator(min_value)) if max_value is not None: for validator in validators_: if isinstance(validator, validators.MaxValueValidator) and validator.limit_value <= max_value: break else: validators_.append(validators.MaxValueValidator(max_value)) return validators_
Example #8
Source File: fields_storage.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def clean(self, value): value = super().clean(value) if value is not None: # Exit early if this is percentage value. if is_percentage(value): return value else: value = machine_readable_bytes(value) # Run validation again, but with the min and max validators. This is # because the value has now been converted to an integer. self.validators = [] if self.min_value is not None: self.validators.append(MinValueValidator(self.min_value)) if self.max_value is not None: self.validators.append(MaxValueValidator(self.max_value)) self.run_validators(value) return value
Example #9
Source File: fields.py From esdc-ce with Apache License 2.0 | 5 votes |
def __init__(self, max_value=None, min_value=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value super(IntegerField, self).__init__(*args, **kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))
Example #10
Source File: fields.py From esdc-ce with Apache License 2.0 | 5 votes |
def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value self.max_digits, self.decimal_places = max_digits, decimal_places super(DecimalField, self).__init__(*args, **kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))
Example #11
Source File: serializers.py From esdc-ce with Apache License 2.0 | 5 votes |
def __init__(self, request, instance, *args, **kwargs): vm_template = kwargs.pop('vm_template', False) self._update_fields_ = list(self._update_fields_) super(BackupDefineSerializer, self).__init__(request, instance, *args, **kwargs) if not kwargs.get('many', False): dc_settings = request.dc.settings backup_nodes = get_nodes(request, is_backup=True) self.fields['node'].queryset = backup_nodes self.fields['zpool'].default = dc_settings.VMS_STORAGE_DEFAULT self.fields['compression'].default = dc_settings.VMS_VM_BACKUP_COMPRESSION_DEFAULT # Set first backup node and backup node storage available in DC # (used only when called by VmDefineBackup.create_from_template()) if vm_template: try: self.fields['node'].default = first_node = backup_nodes[0] except IndexError: pass else: first_node_zpools = get_zpools(request).filter(node=first_node).values_list('zpool', flat=True) if first_node_zpools and dc_settings.VMS_STORAGE_DEFAULT not in first_node_zpools: self.fields['zpool'].default = first_node_zpools[0] if request.method != 'POST': self.fields['type'].read_only = True # Limit maximum number of backups - Issue #chili-447 if dc_settings.VMS_VM_BACKUP_LIMIT is None: min_count, max_count = RETENTION_MIN, RETENTION_MAX else: min_count, max_count = 1, int(dc_settings.VMS_VM_BACKUP_LIMIT) self.fields['retention'].validators.append(validators.MinValueValidator(min_count)) self.fields['retention'].validators.append(validators.MaxValueValidator(max_count)) if instance.vm.is_kvm(): self._update_fields_.append('fsfreeze')
Example #12
Source File: test_integerfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_backend_range_validation(self): """ Backend specific ranges are enforced at the model validation level (#12030). """ min_value, max_value = self.backend_range if min_value is not None: instance = self.model(value=min_value - 1) expected_message = validators.MinValueValidator.message % { 'limit_value': min_value, } with self.assertRaisesMessage(ValidationError, expected_message): instance.full_clean() instance.value = min_value instance.full_clean() if max_value is not None: instance = self.model(value=max_value + 1) expected_message = validators.MaxValueValidator.message % { 'limit_value': max_value, } with self.assertRaisesMessage(ValidationError, expected_message): instance.full_clean() instance.value = max_value instance.full_clean()
Example #13
Source File: test_array.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_with_validators(self): field = ArrayField(models.IntegerField(validators=[validators.MinValueValidator(1)])) field.clean([1, 2], None) with self.assertRaises(exceptions.ValidationError) as cm: field.clean([0], None) self.assertEqual(len(cm.exception.error_list), 1) exception = cm.exception.error_list[0] self.assertEqual( exception.message, 'Item 1 in the array did not validate: Ensure this value is greater than or equal to 1.' ) self.assertEqual(exception.code, 'item_invalid') self.assertEqual(exception.params, {'nth': 1, 'value': 0, 'limit_value': 1, 'show_value': 0})
Example #14
Source File: test_integerfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_redundant_backend_range_validators(self): """ If there are stricter validators than the ones from the database backend then the backend validators aren't added. """ min_backend_value, max_backend_value = self.backend_range if min_backend_value is not None: min_custom_value = min_backend_value + 1 ranged_value_field = self.model._meta.get_field('value').__class__( validators=[validators.MinValueValidator(min_custom_value)] ) field_range_message = validators.MinValueValidator.message % { 'limit_value': min_custom_value, } with self.assertRaisesMessage(ValidationError, "[%r]" % field_range_message): ranged_value_field.run_validators(min_backend_value - 1) if max_backend_value is not None: max_custom_value = max_backend_value - 1 ranged_value_field = self.model._meta.get_field('value').__class__( validators=[validators.MaxValueValidator(max_custom_value)] ) field_range_message = validators.MaxValueValidator.message % { 'limit_value': max_custom_value, } with self.assertRaisesMessage(ValidationError, "[%r]" % field_range_message): ranged_value_field.run_validators(max_backend_value + 1)
Example #15
Source File: test_integerfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_backend_range_validation(self): """ Backend specific ranges are enforced at the model validation level (#12030). """ min_value, max_value = self.backend_range if min_value is not None: instance = self.model(value=min_value - 1) expected_message = validators.MinValueValidator.message % { 'limit_value': min_value, } with self.assertRaisesMessage(ValidationError, expected_message): instance.full_clean() instance.value = min_value instance.full_clean() if max_value is not None: instance = self.model(value=max_value + 1) expected_message = validators.MaxValueValidator.message % { 'limit_value': max_value, } with self.assertRaisesMessage(ValidationError, expected_message): instance.full_clean() instance.value = max_value instance.full_clean()
Example #16
Source File: test_array.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_with_validators(self): field = ArrayField(models.IntegerField(validators=[validators.MinValueValidator(1)])) field.clean([1, 2], None) with self.assertRaises(exceptions.ValidationError) as cm: field.clean([0], None) self.assertEqual(len(cm.exception.error_list), 1) exception = cm.exception.error_list[0] self.assertEqual( exception.message, 'Item 1 in the array did not validate: Ensure this value is greater than or equal to 1.' ) self.assertEqual(exception.code, 'item_invalid') self.assertEqual(exception.params, {'nth': 1, 'value': 0, 'limit_value': 1, 'show_value': 0})
Example #17
Source File: forms.py From django-bom with GNU General Public License v3.0 | 5 votes |
def __init__(self, *args, **kwargs): self.organization = kwargs.get('instance', None) super(OrganizationNumberLenForm, self).__init__(*args, **kwargs) self.fields['number_class_code_len'].validators.append(MinValueValidator(self.organization.number_class_code_len)) self.fields['number_item_len'].validators.append(MinValueValidator(self.organization.number_item_len)) self.fields['number_variation_len'].validators.append(MinValueValidator(self.organization.number_variation_len))
Example #18
Source File: __init__.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def clean(self, value): value = super().clean(value) if value is not None: value = machine_readable_bytes(value) # Run validation again, but with the min and max validators. This is # because the value has now been converted to an integer. self.validators = [] if self.min_value is not None: self.validators.append(MinValueValidator(self.min_value)) if self.max_value is not None: self.validators.append(MaxValueValidator(self.max_value)) self.run_validators(value) return value
Example #19
Source File: fields.py From django-htk with MIT License | 5 votes |
def __init__(self, min_value=1, max_value=5, *args, **kwargs): self.min_value = min_value self.max_value = max_value super(StarRatingField, self).__init__( blank=True, null=True, validators=[ MinValueValidator(min_value), MaxValueValidator(max_value), ], *args, **kwargs )
Example #20
Source File: fields.py From python2017 with MIT License | 5 votes |
def __init__(self, max_value=None, min_value=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value if kwargs.get('localize') and self.widget == NumberInput: # Localized number input is not well supported on most browsers kwargs.setdefault('widget', super(IntegerField, self).widget) super(IntegerField, self).__init__(*args, **kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))
Example #21
Source File: forms.py From coursys with GNU General Public License v3.0 | 5 votes |
def initialize(self, grade_source): self.fields['grade'].validators.extend([ MinValueValidator(grade_source.lower_bound), MaxValueValidator(grade_source.upper_bound), ]) self.fields['grade'].widget.attrs['size'] = 5
Example #22
Source File: fields.py From django-cryptographic-fields with MIT License | 5 votes |
def validators(self): # These validators can't be added at field initialization time since # they're based on values retrieved from `connection`. range_validators = [] internal_type = self.__class__.__name__[9:] min_value, max_value = django.db.connection.ops.integer_field_range( internal_type) if min_value is not None: range_validators.append(validators.MinValueValidator(min_value)) if max_value is not None: range_validators.append(validators.MaxValueValidator(max_value)) return super(EncryptedNumberMixin, self).validators + range_validators
Example #23
Source File: __init__.py From openhgsenti with Apache License 2.0 | 5 votes |
def validators(self): # These validators can't be added at field initialization time since # they're based on values retrieved from `connection`. range_validators = [] internal_type = self.get_internal_type() min_value, max_value = connection.ops.integer_field_range(internal_type) if min_value is not None: range_validators.append(validators.MinValueValidator(min_value)) if max_value is not None: range_validators.append(validators.MaxValueValidator(max_value)) return super(IntegerField, self).validators + range_validators
Example #24
Source File: fields.py From openhgsenti with Apache License 2.0 | 5 votes |
def __init__(self, max_value=None, min_value=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value if kwargs.get('localize') and self.widget == NumberInput: # Localized number input is not well supported on most browsers kwargs.setdefault('widget', super(IntegerField, self).widget) super(IntegerField, self).__init__(*args, **kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))
Example #25
Source File: fields.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value self.max_digits, self.decimal_places = max_digits, decimal_places Field.__init__(self, *args, **kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))
Example #26
Source File: fields.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def __init__(self, max_value=None, min_value=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value super(IntegerField, self).__init__(*args, **kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))
Example #27
Source File: fields.py From python with Apache License 2.0 | 5 votes |
def __init__(self, max_value=None, min_value=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value if kwargs.get('localize') and self.widget == NumberInput: # Localized number input is not well supported on most browsers kwargs.setdefault('widget', super(IntegerField, self).widget) super(IntegerField, self).__init__(*args, **kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))
Example #28
Source File: serializers.py From SchoolIdolAPI with Apache License 2.0 | 5 votes |
def validate(self, data): errors = {} request = self.context['request'] card = None if request.method == 'POST': try: data['card'] = models.Card.objects.get(pk=request.POST['card']) card = data['card'] except (ObjectDoesNotExist, KeyError): if 'card' not in request.POST: errors['card'] = 'This field is required' else: errors['card'] = 'Invalid id' try: data['owner_account'] = models.Account.objects.get(pk=request.POST['owner_account'], owner=request.user) except (ObjectDoesNotExist, KeyError): if 'owner_account' not in request.POST: errors['owner_account'] = 'This field is required' else: errors['owner_account'] = 'This account does\'t exist or isn\'t yours' else: card = self.instance.card # Check for skill slots if 'skill_slots' in data and card: for validator in [MinValueValidator(card.min_skill_slot), MaxValueValidator(card.max_skill_slot)]: try: validator(data['skill_slots']) except DjangoCoreValidationError as e: errors['skill_slots'] = e.messages elif card: data['skill_slots'] = card.min_skill_slot if errors: raise serializers.ValidationError(errors) return data
Example #29
Source File: __init__.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
def validators(self): # These validators can't be added at field initialization time since # they're based on values retrieved from `connection`. validators_ = super().validators internal_type = self.get_internal_type() min_value, max_value = connection.ops.integer_field_range(internal_type) if (min_value is not None and not any(isinstance(validator, validators.MinValueValidator) and validator.limit_value >= min_value for validator in validators_)): validators_.append(validators.MinValueValidator(min_value)) if (max_value is not None and not any(isinstance(validator, validators.MaxValueValidator) and validator.limit_value <= max_value for validator in validators_)): validators_.append(validators.MaxValueValidator(max_value)) return validators_
Example #30
Source File: fields.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
def __init__(self, *, max_value=None, min_value=None, **kwargs): self.max_value, self.min_value = max_value, min_value if kwargs.get('localize') and self.widget == NumberInput: # Localized number input is not well supported on most browsers kwargs.setdefault('widget', super().widget) super().__init__(**kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))