django-eav2/eav/validators.py

112 lines
3 KiB
Python
Raw Normal View History

"""
This module contains a validator for each :class:`~eav.models.Attribute` datatype.
2010-09-27 13:28:52 +00:00
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/>`_).
2010-09-27 13:28:52 +00:00
These validators are called by the
:meth:`~eav.models.Attribute.validate_value` method in the
:class:`~eav.models.Attribute` model.
"""
2010-09-27 13:28:52 +00:00
2017-09-05 10:01:20 +00:00
import datetime
2021-10-16 17:43:20 +00:00
import json
from django.core.exceptions import ValidationError
2010-09-27 13:28:52 +00:00
from django.db import models
from django.utils.translation import gettext_lazy as _
2010-09-27 13:28:52 +00:00
def validate_text(value):
"""
2010-09-27 13:28:52 +00:00
Raises ``ValidationError`` unless *value* type is ``str`` or ``unicode``
"""
if not isinstance(value, str):
2010-09-27 13:28:52 +00:00
raise ValidationError(_(u"Must be str or unicode"))
def validate_float(value):
"""
2010-09-27 13:28:52 +00:00
Raises ``ValidationError`` unless *value* can be cast as a ``float``
"""
2010-09-27 13:28:52 +00:00
try:
float(value)
except ValueError:
raise ValidationError(_(u"Must be a float"))
def validate_int(value):
"""
2010-09-27 13:28:52 +00:00
Raises ``ValidationError`` unless *value* can be cast as an ``int``
"""
2010-09-27 13:28:52 +00:00
try:
int(value)
except ValueError:
raise ValidationError(_(u"Must be an integer"))
def validate_date(value):
"""
2010-09-27 13:28:52 +00:00
Raises ``ValidationError`` unless *value* is an instance of ``datetime``
or ``date``
"""
2021-10-16 17:43:02 +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):
"""
2010-09-27 13:28:52 +00:00
Raises ``ValidationError`` unless *value* type is ``bool``
"""
if not isinstance(value, bool):
2010-09-27 13:28:52 +00:00
raise ValidationError(_(u"Must be a boolean"))
def validate_object(value):
"""
2010-09-27 13:28:52 +00:00
Raises ``ValidationError`` unless *value* is a saved
django model instance.
"""
2010-09-27 13:28:52 +00:00
if not isinstance(value, models.Model):
raise ValidationError(_(u"Must be a django model object instance"))
2010-09-27 13:28:52 +00:00
if not value.pk:
raise ValidationError(_(u"Model has not been saved yet"))
def validate_enum(value):
"""
2010-09-27 13:28:52 +00:00
Raises ``ValidationError`` unless *value* is a saved
:class:`~eav.models.EnumValue` model instance.
"""
2021-10-16 17:45:01 +00:00
from eav.models import EnumValue
if isinstance(value, EnumValue) and not value.pk:
2010-09-27 13:28:52 +00:00
raise ValidationError(_(u"EnumValue has not been saved yet"))
2021-04-05 02:53:45 +00:00
def validate_json(value):
"""
Raises ``ValidationError`` unless *value* can be cast as an ``json object`` (a dict)
"""
try:
if isinstance(value, str):
value = json.loads(value)
if not isinstance(value, dict):
raise ValidationError(_(u"Must be a JSON Serializable object"))
except ValueError:
raise ValidationError(_(u"Must be a JSON Serializable object"))
def validate_csv(value):
"""
Raises ``ValidationError`` unless *value* is a c-s-v value.
"""
if isinstance(value, str):
value = value.split(";")
if not isinstance(value, list):
raise ValidationError(_(u"Must be Comma-Separated-Value."))