2010-09-27 13:28:52 +00:00
|
|
|
|
'''
|
2018-06-03 13:42:16 +00:00
|
|
|
|
Validtors.
|
|
|
|
|
|
|
2010-09-27 13:28:52 +00:00
|
|
|
|
This module contains a validator for each Attribute datatype.
|
|
|
|
|
|
|
|
|
|
|
|
A validator is a callable that takes a value and raises a ``ValidationError``
|
|
|
|
|
|
if it doesn’t meet some criteria. (see
|
|
|
|
|
|
`django validators <http://docs.djangoproject.com/en/dev/ref/validators/>`_)
|
|
|
|
|
|
|
|
|
|
|
|
These validators are called by the
|
|
|
|
|
|
:meth:`~eav.models.Attribute.validate_value` method in the
|
|
|
|
|
|
:class:`~eav.models.Attribute` model.
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
2017-09-05 10:01:20 +00:00
|
|
|
|
import datetime
|
2018-05-31 01:21:20 +00:00
|
|
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
2010-09-27 13:28:52 +00:00
|
|
|
|
from django.db import models
|
2018-05-31 01:21:20 +00:00
|
|
|
|
from django.utils import timezone
|
2010-09-27 13:28:52 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_text(value):
|
|
|
|
|
|
'''
|
|
|
|
|
|
Raises ``ValidationError`` unless *value* type is ``str`` or ``unicode``
|
|
|
|
|
|
'''
|
2018-04-06 11:59:51 +00:00
|
|
|
|
if not type(value) == str:
|
2010-09-27 13:28:52 +00:00
|
|
|
|
raise ValidationError(_(u"Must be str or unicode"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_float(value):
|
|
|
|
|
|
'''
|
|
|
|
|
|
Raises ``ValidationError`` unless *value* can be cast as a ``float``
|
|
|
|
|
|
'''
|
|
|
|
|
|
try:
|
|
|
|
|
|
float(value)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
raise ValidationError(_(u"Must be a float"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_int(value):
|
|
|
|
|
|
'''
|
|
|
|
|
|
Raises ``ValidationError`` unless *value* can be cast as an ``int``
|
|
|
|
|
|
'''
|
|
|
|
|
|
try:
|
|
|
|
|
|
int(value)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
raise ValidationError(_(u"Must be an integer"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_date(value):
|
|
|
|
|
|
'''
|
|
|
|
|
|
Raises ``ValidationError`` unless *value* is an instance of ``datetime``
|
|
|
|
|
|
or ``date``
|
|
|
|
|
|
'''
|
2017-09-05 10:01:20 +00:00
|
|
|
|
if not isinstance(value, datetime.datetime) and not isinstance(value, datetime.date):
|
2010-09-27 13:28:52 +00:00
|
|
|
|
raise ValidationError(_(u"Must be a date or datetime"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_bool(value):
|
|
|
|
|
|
'''
|
|
|
|
|
|
Raises ``ValidationError`` unless *value* type is ``bool``
|
|
|
|
|
|
'''
|
|
|
|
|
|
if not type(value) == bool:
|
|
|
|
|
|
raise ValidationError(_(u"Must be a boolean"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_object(value):
|
|
|
|
|
|
'''
|
|
|
|
|
|
Raises ``ValidationError`` unless *value* is a saved
|
|
|
|
|
|
django model instance.
|
|
|
|
|
|
'''
|
|
|
|
|
|
if not isinstance(value, models.Model):
|
|
|
|
|
|
raise ValidationError(_(u"Must be a django model object instance"))
|
|
|
|
|
|
if not value.pk:
|
|
|
|
|
|
raise ValidationError(_(u"Model has not been saved yet"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_enum(value):
|
|
|
|
|
|
'''
|
|
|
|
|
|
Raises ``ValidationError`` unless *value* is a saved
|
|
|
|
|
|
:class:`~eav.models.EnumValue` model instance.
|
|
|
|
|
|
'''
|
|
|
|
|
|
from .models import EnumValue
|
|
|
|
|
|
if not isinstance(value, EnumValue):
|
|
|
|
|
|
raise ValidationError(_(u"Must be an EnumValue model object instance"))
|
|
|
|
|
|
if not value.pk:
|
|
|
|
|
|
raise ValidationError(_(u"EnumValue has not been saved yet"))
|