|
@@ -643,6 +643,7 @@ class AbstractProductAttribute(models.Model):
|
643
|
643
|
def _validate_text(self, value):
|
644
|
644
|
if not isinstance(value, six.string_types):
|
645
|
645
|
raise ValidationError(_("Must be str or unicode"))
|
|
646
|
+ _validate_richtext = _validate_text
|
646
|
647
|
|
647
|
648
|
def _validate_float(self, value):
|
648
|
649
|
try:
|
|
@@ -680,8 +681,8 @@ class AbstractProductAttribute(models.Model):
|
680
|
681
|
_("Must be an AttributeOption model object instance"))
|
681
|
682
|
if not value.pk:
|
682
|
683
|
raise ValidationError(_("AttributeOption has not been saved yet"))
|
683
|
|
- valid_values = self.option_group.options.values_list('option',
|
684
|
|
- flat=True)
|
|
684
|
+ valid_values = self.option_group.options.values_list(
|
|
685
|
+ 'option', flat=True)
|
685
|
686
|
if value.option not in valid_values:
|
686
|
687
|
raise ValidationError(
|
687
|
688
|
_("%(enum)s is not a valid choice for %(attr)s") %
|
|
@@ -690,40 +691,27 @@ class AbstractProductAttribute(models.Model):
|
690
|
691
|
def _validate_file(self, value):
|
691
|
692
|
if value and not isinstance(value, File):
|
692
|
693
|
raise ValidationError(_("Must be a file field"))
|
|
694
|
+ _validate_image = _validate_file
|
693
|
695
|
|
694
|
|
- def get_validator(self):
|
695
|
|
- DATATYPE_VALIDATORS = {
|
696
|
|
- 'text': self._validate_text,
|
697
|
|
- 'integer': self._validate_int,
|
698
|
|
- 'boolean': self._validate_bool,
|
699
|
|
- 'float': self._validate_float,
|
700
|
|
- 'richtext': self._validate_text,
|
701
|
|
- 'date': self._validate_date,
|
702
|
|
- 'entity': self._validate_entity,
|
703
|
|
- 'option': self._validate_option,
|
704
|
|
- 'file': self._validate_file,
|
705
|
|
- 'image': self._validate_file,
|
706
|
|
- }
|
707
|
|
-
|
708
|
|
- return DATATYPE_VALIDATORS[self.type]
|
|
696
|
+ def validate_value(self, value):
|
|
697
|
+ validator = getattr(self, '_validate_%s' % self.type)
|
|
698
|
+ validator(value)
|
709
|
699
|
|
710
|
700
|
def __unicode__(self):
|
711
|
701
|
return self.name
|
712
|
702
|
|
713
|
|
- def save(self, *args, **kwargs):
|
714
|
|
- super(AbstractProductAttribute, self).save(*args, **kwargs)
|
715
|
|
-
|
716
|
703
|
def save_value(self, product, value):
|
|
704
|
+ ProductAttributeValue = get_model('catalogue', 'ProductAttributeValue')
|
717
|
705
|
try:
|
718
|
706
|
value_obj = product.attribute_values.get(attribute=self)
|
719
|
|
- except get_model('catalogue', 'ProductAttributeValue').DoesNotExist:
|
720
|
|
- # FileField uses False for anouncing deletion of the file
|
|
707
|
+ except ProductAttributeValue.DoesNotExist:
|
|
708
|
+ # FileField uses False for announcing deletion of the file
|
721
|
709
|
# not creating a new value
|
722
|
710
|
delete_file = self.is_file and value is False
|
723
|
711
|
if value is None or value == '' or delete_file:
|
724
|
712
|
return
|
725
|
|
- model = get_model('catalogue', 'ProductAttributeValue')
|
726
|
|
- value_obj = model.objects.create(product=product, attribute=self)
|
|
713
|
+ value_obj = ProductAttributeValue.objects.create(
|
|
714
|
+ product=product, attribute=self)
|
727
|
715
|
|
728
|
716
|
if self.is_file:
|
729
|
717
|
# File fields in Django are treated differently, see
|
|
@@ -746,19 +734,6 @@ class AbstractProductAttribute(models.Model):
|
746
|
734
|
value_obj.value = value
|
747
|
735
|
value_obj.save()
|
748
|
736
|
|
749
|
|
- def validate_value(self, value):
|
750
|
|
- self.get_validator()(value)
|
751
|
|
-
|
752
|
|
- def is_value_valid(self, value):
|
753
|
|
- """
|
754
|
|
- Check whether the passed value is valid for this attribute
|
755
|
|
- """
|
756
|
|
- if self.type == 'option':
|
757
|
|
- valid_values = self.option_group.options.values_list('option',
|
758
|
|
- flat=True)
|
759
|
|
- return value in valid_values
|
760
|
|
- return True
|
761
|
|
-
|
762
|
737
|
|
763
|
738
|
class AbstractProductAttributeValue(models.Model):
|
764
|
739
|
"""
|