2014-03-15 01:34:49 +08:00
|
|
|
import copy
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
2015-01-11 00:14:20 +08:00
|
|
|
from django.core.validators import (
|
2015-01-28 20:35:27 +08:00
|
|
|
MaxLengthValidator, MaxValueValidator, MinLengthValidator,
|
2015-01-11 00:14:20 +08:00
|
|
|
MinValueValidator,
|
|
|
|
)
|
2014-03-15 01:34:49 +08:00
|
|
|
from django.utils.deconstruct import deconstructible
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _, ngettext_lazy
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ArrayMaxLengthValidator(MaxLengthValidator):
|
2017-01-27 03:58:33 +08:00
|
|
|
message = ngettext_lazy(
|
2014-03-27 00:44:21 +08:00
|
|
|
'List contains %(show_value)d item, it should contain no more than %(limit_value)d.',
|
|
|
|
'List contains %(show_value)d items, it should contain no more than %(limit_value)d.',
|
|
|
|
'limit_value')
|
|
|
|
|
|
|
|
|
|
|
|
class ArrayMinLengthValidator(MinLengthValidator):
|
2017-01-27 03:58:33 +08:00
|
|
|
message = ngettext_lazy(
|
2014-03-27 00:44:21 +08:00
|
|
|
'List contains %(show_value)d item, it should contain no fewer than %(limit_value)d.',
|
|
|
|
'List contains %(show_value)d items, it should contain no fewer than %(limit_value)d.',
|
|
|
|
'limit_value')
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
@deconstructible
|
2017-01-19 15:39:46 +08:00
|
|
|
class KeysValidator:
|
2014-03-15 01:34:49 +08:00
|
|
|
"""A validator designed for HStore to require/restrict keys."""
|
|
|
|
|
|
|
|
messages = {
|
|
|
|
'missing_keys': _('Some keys were missing: %(keys)s'),
|
|
|
|
'extra_keys': _('Some unknown keys were provided: %(keys)s'),
|
|
|
|
}
|
|
|
|
strict = False
|
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, keys, strict=False, messages=None):
|
2014-03-15 01:34:49 +08:00
|
|
|
self.keys = set(keys)
|
|
|
|
self.strict = strict
|
|
|
|
if messages is not None:
|
|
|
|
self.messages = copy.copy(self.messages)
|
|
|
|
self.messages.update(messages)
|
|
|
|
|
|
|
|
def __call__(self, value):
|
2017-05-28 07:08:46 +08:00
|
|
|
keys = set(value)
|
2014-03-15 01:34:49 +08:00
|
|
|
missing_keys = self.keys - keys
|
|
|
|
if missing_keys:
|
2016-03-29 06:33:29 +08:00
|
|
|
raise ValidationError(
|
|
|
|
self.messages['missing_keys'],
|
2014-03-15 01:34:49 +08:00
|
|
|
code='missing_keys',
|
|
|
|
params={'keys': ', '.join(missing_keys)},
|
|
|
|
)
|
|
|
|
if self.strict:
|
|
|
|
extra_keys = keys - self.keys
|
|
|
|
if extra_keys:
|
2016-03-29 06:33:29 +08:00
|
|
|
raise ValidationError(
|
|
|
|
self.messages['extra_keys'],
|
2014-03-15 01:34:49 +08:00
|
|
|
code='extra_keys',
|
|
|
|
params={'keys': ', '.join(extra_keys)},
|
|
|
|
)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return (
|
2016-04-04 08:37:32 +08:00
|
|
|
isinstance(other, self.__class__) and
|
|
|
|
self.keys == other.keys and
|
|
|
|
self.messages == other.messages and
|
|
|
|
self.strict == other.strict
|
2014-03-15 01:34:49 +08:00
|
|
|
)
|
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
class RangeMaxValueValidator(MaxValueValidator):
|
2016-01-24 00:47:07 +08:00
|
|
|
def compare(self, a, b):
|
|
|
|
return a.upper > b
|
2015-01-11 00:14:20 +08:00
|
|
|
message = _('Ensure that this range is completely less than or equal to %(limit_value)s.')
|
|
|
|
|
|
|
|
|
|
|
|
class RangeMinValueValidator(MinValueValidator):
|
2016-01-24 00:47:07 +08:00
|
|
|
def compare(self, a, b):
|
|
|
|
return a.lower < b
|
2015-01-11 00:14:20 +08:00
|
|
|
message = _('Ensure that this range is completely greater than or equal to %(limit_value)s.')
|