Enum validation

This commit is contained in:
David Gelvin 2010-09-16 16:13:47 +03:00
parent e9ef2c0c65
commit f21d08b3bc

View file

@ -86,6 +86,9 @@ class EavAttribute(models.Model):
blank=True, null=True,
help_text=_(u"Short description"))
enum_group = models.ForeignKey(EnumGroup, verbose_name=_(u"choice group"),
blank=True, null=True)
@property
def help_text(self):
return self.description
@ -107,6 +110,11 @@ class EavAttribute(models.Model):
self.full_clean()
super(EavAttribute, self).save(*args, **kwargs)
def clean(self):
if self.datatype == self.TYPE_ENUM and not enum_group:
raise ValidationError(_(
u"You must set the enum_group for TYPE_ENUM attributes"))
def add_label(self, label):
label, created = EavAttributeLabel.objects.get_or_create(name=label)
label.eavattribute_set.add(self)
@ -220,6 +228,15 @@ class EavValue(models.Model):
self.full_clean()
super(EavValue, self).save(*args, **kwargs)
def clean(self):
if self.attribute.datatype == EavAttribute.TYPE_ENUM and \
self.value_enum:
if self.value_enum not in self.attribute.enumvalues:
raise ValidationError(_(u"%(choice)s is not a valid " \
u"choice for %s(attribute)") % \
{'choice': self.value_enum,
'attribute': self.attribute})
# todo: do it in a faster way (one update)
def _blank(self):
"""