Browse Source

Refactor product attribute validation

* The get_validator method was dropped. Elsewhere we use the explicit
  lookup by type, so why not there? It only makes adding custom
  validation for e.g. richt text values more verbose, as both adding a
  validation method and overriding get_validator is needed.
  get_validator() was not used elsewhere in the codebase.
* The unused is_value_valid (used for option values) has been dropped.
  The same check is already done in _validate_option.
master
Maik Hoepfel 11 years ago
parent
commit
2b957ae555

+ 4
- 0
docs/source/releases/v0.8.rst View File

@@ -315,6 +315,10 @@ Misc
315 315
 
316 316
   After: ``{% basket_form request product 'single' as basket_form %}``
317 317
 
318
+* Product attribute validation has been cleaned up. As part of that, the
319
+  trivial ``ProductAttribute.get_validator`` and the unused
320
+  ``ProductAttribute.is_value_valid`` methods have been removed.
321
+
318 322
 .. _rewritten: https://github.com/tangentlabs/django-oscar/commit/d8b4dbfed17be90846ea4bc47b5f7b39ad944c24
319 323
 
320 324
 Basket line stockrecords

+ 12
- 37
oscar/apps/catalogue/abstract_models.py View File

@@ -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
     """

+ 4
- 4
tests/integration/catalogue/product_tests.py View File

@@ -120,10 +120,10 @@ class ProductAttributeCreationTests(TestCase):
120 120
         pa = factories.ProductAttribute(
121 121
             type='option', option_group=option_group)
122 122
 
123
-        self.assertRaises(ValidationError, pa.get_validator(), 'invalid')
124
-        pa.get_validator()(option_1)
125
-        pa.get_validator()(option_2)
123
+        self.assertRaises(ValidationError, pa.validate_value, 'invalid')
124
+        pa.validate_value(option_1)
125
+        pa.validate_value(option_2)
126 126
 
127 127
         invalid_option = AttributeOption(option='invalid option')
128 128
         self.assertRaises(
129
-            ValidationError, pa.get_validator(), invalid_option)
129
+            ValidationError, pa.validate_value, invalid_option)

Loading…
Cancel
Save