Exemplo n.º 1
0
    def clean(self):
        cleaned_data = super(VariantAttributeFormMixin, self).clean()
        product = cleaned_data['product']

        # Temporary collect values to generate slug
        # Get only the values which variate
        values = []
        product_values = []

        for attribute in self.__attributes.filter(variates=True):
            value = Value(attribute=attribute)
            value.value = cleaned_data.get(
                self.__attribute_field_names[attribute.key]
            )
            if not value.is_empty():
                # At this point make sure product doesn't have the same value
                if product.attributes[attribute.key] == value.value:
                    product_values.append(value)
                values.append(value)

        # Make sure we don't define the same values as the parent product
        if len(product_values) == len(values):
            raise ValidationError(
                _("This variant does not differ from it's parent product.")
            )

        # Enforce at least one variated value
        if not values:
            raise ValidationError(
                _("A variant requires at least one variated attribute.")
            )

        # Generate slug if needed
        if cleaned_data.has_key('slug'):
            if not cleaned_data['slug']:
                cleaned_data['slug'] = values_slug(
                    values,
                    prefix=cleaned_data['product'].slug
                )

            self.data[self.add_prefix('slug')] = cleaned_data['slug']

        return cleaned_data
Exemplo n.º 2
0
    def save(self, product, attribute, values=None):
        # Make sure values is an iterable
        values = values or []
        invalidated = self.get_invalidated_values(product, attribute, values)
        invalidated.delete()

        existing = [
            value.get_value()
            for value in self.get_existing_values(
                product, attribute, values
            )
        ]
        for value in values:
            if not value in existing:
                obj = Value(
                    product=product,
                    variates=True,
                    attribute=attribute
                )
                obj.set_value(value)
                obj.save()
Exemplo n.º 3
0
    def clean(self):
        cleaned_data = super(VariantAttributeFormMixin, self).clean()
        product = cleaned_data['product']

        # Temporary collect values to generate slug
        # Get only the values which variate
        values = []
        product_values = []

        for attribute in self.__attributes.filter(variates=True):
            value = Value(attribute=attribute)
            value.value = cleaned_data.get(
                self.__attribute_field_names[attribute.key])
            if not value.is_empty():
                # At this point make sure product doesn't have the same value
                if product.attributes[attribute.key] == value.value:
                    product_values.append(value)
                values.append(value)

        # Make sure we don't define the same values as the parent product
        if len(product_values) == len(values):
            raise ValidationError(
                _("This variant does not differ from it's parent product."))

        # Enforce at least one variated value
        if not values:
            raise ValidationError(
                _("A variant requires at least one variated attribute."))

        # Generate slug if needed
        if cleaned_data.has_key('slug'):
            if not cleaned_data['slug']:
                cleaned_data['slug'] = values_slug(
                    values, prefix=cleaned_data['product'].slug)

            self.data[self.add_prefix('slug')] = cleaned_data['slug']

        return cleaned_data
Exemplo n.º 4
0
    def save(self, product, attribute, values=None):
        # Make sure values is an iterable
        values = values or []
        invalidated = self.get_invalidated_values(product, attribute, values)
        invalidated.delete()

        existing = [
            value.get_value()
            for value in self.get_existing_values(product, attribute, values)
        ]
        for value in values:
            if not value in existing:
                obj = Value(product=product,
                            variates=True,
                            attribute=attribute)
                obj.set_value(value)
                obj.save()