Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add product class model
Krzysztof Wolski50cfa71c510f
diff --git a/saleor/product/forms.py b/saleor/product/forms.pyindex a0e3cf2638..714174da3c 100644--- a/saleor/product/forms.py+++ b/saleor/product/forms.py@@ -14,7 +14,7 @@ class VariantChoiceField(forms.ModelChoiceField):discounts = Nonedef label_from_instance(self, obj):- attributes = obj.product.attributes.all()+ attributes = obj.product.variant_attributes.all()variant_label = obj.display_variant(attributes)label = '%(variant_label)s - %(price)s' % {'variant_label': variant_label,diff --git a/saleor/product/models/__init__.py b/saleor/product/models/__init__.pyindex af03fa8824..0636ed68f7 100644--- a/saleor/product/models/__init__.py+++ b/saleor/product/models/__init__.py@@ -1,7 +1,7 @@from .base import (AttributeChoiceValue, Category, Product, ProductAttribute,- ProductVariant, Stock, StockLocation)+ ProductVariant, Stock, ProductClass, StockLocation)from .images import ProductImage, VariantImage__all__ = ['AttributeChoiceValue', 'Category', 'Product', 'ProductAttribute','ProductImage', 'ProductVariant', 'Stock', 'VariantImage',- 'StockLocation']+ 'ProductClass', 'StockLocation']diff --git a/saleor/product/models/base.py b/saleor/product/models/base.pyindex 384e176ac8..b43ed37aeb 100644--- a/saleor/product/models/base.py+++ b/saleor/product/models/base.py@@ -12,7 +12,7 @@ from django.utils.encoding import python_2_unicode_compatible, smart_textfrom django.utils.text import slugifyfrom django.utils.translation import pgettext_lazyfrom django_prices.models import PriceField-from jsonfield import JSONField+from django.contrib.postgres.fields import JSONFieldfrom mptt.managers import TreeManagerfrom mptt.models import MPTTModelfrom satchless.item import InsufficientStock, Item, ItemRange@@ -63,6 +63,28 @@ class Category(MPTTModel):self.get_descendants().update(hidden=hidden)+@python_2_unicode_compatible+class ProductClass(models.Model):+ name = models.CharField(+ pgettext_lazy('Product field', 'name'), max_length=128)+ has_variants = models.BooleanField(default=True)+ product_attributes = models.ManyToManyField(+ 'ProductAttribute', related_name='products_class', blank=True)+ variant_attributes = models.ManyToManyField(+ 'ProductAttribute', related_name='product_variants_class', blank=True)++ class Meta:+ app_label = 'product'++ def __str__(self):+ return self.name++ def __repr__(self):+ class_ = type(self)+ return '<%s.%s(pk=%r, name=%r)>' % (+ class_.__module__, class_.__name__, self.pk, self.name)++class ProductManager(models.Manager):def get_available_products(self):today = datetime.date.today()@@ -72,6 +94,7 @@ class ProductManager(models.Manager):@python_2_unicode_compatibleclass Product(models.Model, ItemRange):+ product_class = models.ForeignKey(ProductClass, related_name='products')name = models.CharField(pgettext_lazy('Product field', 'name'), max_length=128)description = models.TextField(@@ -87,8 +110,12 @@ class Product(models.Model, ItemRange):max_digits=6, decimal_places=2)available_on = models.DateField(pgettext_lazy('Product field', 'available on'), blank=True, null=True)- attributes = models.ManyToManyField(+ product_attributes = models.ManyToManyField('ProductAttribute', related_name='products', blank=True)+ attributes = JSONField(pgettext_lazy('Product field', 'attributes'),+ default={})+ variant_attributes = models.ManyToManyField(+ 'ProductAttribute', related_name='product_variants', blank=True)updated_at = models.DateTimeField(pgettext_lazy('Product field', 'updated at'), auto_now=True, null=True)@@ -137,6 +164,12 @@ class Product(models.Model, ItemRange):return first_image.imagereturn None+ def get_attribute(self, pk):+ return self.attributes.get(str(pk))++ def set_attribute(self, pk, value_pk):+ self.attributes[str(pk)] = value_pk+@python_2_unicode_compatibleclass ProductVariant(models.Model, Item):@@ -209,9 +242,12 @@ class ProductVariant(models.Model, Item):def get_attribute(self, pk):return self.attributes.get(str(pk))+ def set_attribute(self, pk, value_pk):+ self.attributes[str(pk)] = value_pk+def display_variant(self, attributes=None):if attributes is None:- attributes = self.product.attributes.all()+ attributes = self.product.variant_attributes.all()values = get_attributes_display_map(self, attributes).values()if values:return ', '.join([smart_text(value) for value in values])diff --git a/saleor/product/utils.py b/saleor/product/utils.pyindex 39c80ad05d..45876c924e 100644--- a/saleor/product/utils.py+++ b/saleor/product/utils.py@@ -1,5 +1,7 @@from collections import namedtuple+from django.db.models import Q+from ..core.utils import to_local_currencyfrom .models import Product@@ -72,3 +74,9 @@ def get_availability(product, discounts=None, local_currency=None):discount=discount,price_range_local_currency=price_range_local,discount_local_currency=discount_local_currency)+++def filter_by_attribute(queryset, key, value):+ in_product = Q(attributes__contains={key.pk: value.pk})+ in_variant = Q(variants__attributes__contains={key.pk: value.pk})+ return queryset.filter(in_product | in_variant)