Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Models moved to separated files
Artur Smętd3e2a1f164f8
diff --git a/saleor/product/forms.py b/saleor/product/forms.pyindex 067c476b3b..0a28f4b336 100644--- a/saleor/product/forms.py+++ b/saleor/product/forms.py@@ -1,7 +1,7 @@from django import formsfrom ..cart.forms import AddToCartForm-from .models import Bag, BagVariant, Shirt, ShirtVariant, Color+from .models import Bag, BagVariant, Shirt, ShirtVariantclass BagForm(AddToCartForm):diff --git a/saleor/product/models.py b/saleor/product/models.pydeleted file mode 100644index 8e3b0ae6bc..0000000000--- a/saleor/product/models.py+++ /dev/null@@ -1,245 +0,0 @@-from __future__ import unicode_literals-from decimal import Decimal-import re--from django.conf import settings-from django.core.urlresolvers import reverse-from django.db import models-from django.utils.safestring import mark_safe-from django.utils.translation import pgettext_lazy-from django_images.models import Image-from django_prices.models import PriceField-from mptt.models import MPTTModel-from prices import FixedDiscount-from satchless.item import Item, ItemRange, StockedItem-from unidecode import unidecode--from ..core.utils.models import Subtyped---class NotApplicable(ValueError):- pass---class Category(MPTTModel):-- name = models.CharField(- pgettext_lazy('Category field', 'name'), max_length=128)- slug = models.SlugField(- pgettext_lazy('Category field', 'slug'), max_length=50, unique=True)- description = models.TextField(- pgettext_lazy('Category field', 'description'), blank=True)- parent = models.ForeignKey(- 'self', null=True, blank=True, related_name='children',- verbose_name=pgettext_lazy('Category field', 'parent'))-- def __unicode__(self):- return self.name-- def get_absolute_url(self):- return reverse('product:category', kwargs={'slug': self.slug})-- class Meta:- verbose_name_plural = 'categories'---class ProductCollection(models.Model):- name = models.CharField(max_length=100, db_index=True, unique=True)-- def __str__(self):- return self.name---class Product(Subtyped, ItemRange):-- name = models.CharField(- pgettext_lazy('Product field', 'name'), max_length=128)- category = models.ForeignKey(- Category, verbose_name=pgettext_lazy('Product field', 'category'),- related_name='products')-- collection = models.ForeignKey(ProductCollection, null=True, blank=True)-- def __unicode__(self):- return self.name-- def get_absolute_url(self):- return reverse('product:details', kwargs={'slug': self.get_slug(),- 'product_id': self.id})-- def get_slug(self):- value = unidecode(self.name)- value = re.sub(r'[^\w\s-]', '', value).strip().lower()-- return mark_safe(re.sub(r'[-\s]+', '-', value))-- def get_products_from_collection(self):- return Product.objects.filter(collection=self.collection,- collection__isnull=False)-- def __iter__(self):- return iter(self.variants.all())---class ProductVariant(models.Model, Item):-- name = models.CharField(pgettext_lazy('Product field', 'name'),- max_length=128, blank=True, default='')- price = PriceField(pgettext_lazy('Product field', 'price'),- currency=settings.DEFAULT_CURRENCY,- max_digits=12, decimal_places=4)- sku = models.CharField(- pgettext_lazy('Product field', 'sku'), max_length=32, unique=True)-- class Meta:- abstract = True-- def __unicode__(self):- return self.name or self.product.name-- def get_price_per_item(self, discounted=True, **kwargs):- price = self.price- if discounted:- discounts = list(get_product_discounts(self, **kwargs))- if discounts:- modifier = max(discounts)- price += modifier- return price-- def get_absolute_url(self):- slug = self.product.get_slug()- product_id = self.product.id- return reverse(- 'product:details', kwargs={'slug': slug, 'product_id': product_id})---class ImageManager(models.Manager):-- def first(self):- return self.get_query_set()[0]---class ProductImage(Image):-- product = models.ForeignKey(Product, related_name='images')-- objects = ImageManager()-- class Meta:- ordering = ['id']-- def __unicode__(self):- html = '<img src="%s" alt="">' % (- self.get_absolute_url('admin'),)- return mark_safe(html)---class ProductDiscountManager(models.Manager):-- def for_product(self, variant):- # Add a caching layer here to reduce the number of queries- return self.get_query_set().filter(products=variant.product)---class FixedProductDiscount(models.Model):-- name = models.CharField(max_length=255)- products = models.ManyToManyField(Product, blank=True)- discount = PriceField(pgettext_lazy('Discount field', 'discount value'),- currency=settings.DEFAULT_CURRENCY,- max_digits=12, decimal_places=4)-- objects = ProductDiscountManager()-- def modifier_for_product(self, variant):- if not self.products.filter(pk=variant.product.pk).exists():- raise NotApplicable('Discount not applicable for this product')- if self.discount > variant.get_price(discounted=False):- raise NotApplicable('Discount too high for this product')- return FixedDiscount(self.discount, name=self.name)-- def __unicode__(self):- return self.name-- def __repr__(self):- return 'FixedProductDiscount(name=%r, discount=%r)' % (- str(self.discount), self.name)---def get_product_discounts(variant, **kwargs):- for discount in FixedProductDiscount.objects.for_product(variant):- try:- yield discount.modifier_for_product(variant, **kwargs)- except NotApplicable:- pass---class StockedProduct(models.Model, StockedItem):-- stock = models.DecimalField(pgettext_lazy('Product item field', 'stock'),- max_digits=10, decimal_places=4,- default=Decimal(1))-- class Meta:- abstract = True-- def get_stock(self):- return self.stock---class PhysicalProduct(models.Model):-- weight = models.DecimalField(max_digits=6, decimal_places=2)- length = models.DecimalField(- max_digits=6, decimal_places=2, blank=True, default=0)- width = models.DecimalField(- max_digits=6, decimal_places=2, blank=True, default=0)- depth = models.DecimalField(- max_digits=6, decimal_places=2, blank=True, default=0)-- class Meta:- abstract = True---class Color(models.Model):-- name = models.CharField(max_length=100)- color = models.CharField(max_length=6)-- def __str__(self):- return str(self.name)---class ColoredVariants(models.Model):-- color = models.ForeignKey(Color)-- class Meta:- abstract = True---class Bag(Product, PhysicalProduct, ColoredVariants):- pass---class Shirt(Product, PhysicalProduct, ColoredVariants):- pass---class BagVariant(ProductVariant, StockedProduct):-- product = models.ForeignKey(Bag, related_name='variants')---class ShirtVariant(ProductVariant, StockedProduct):-- SIZE_CHOICES = (- ('xs', pgettext_lazy('Variant size', 'XS')),- ('s', pgettext_lazy('Variant size', 'S')),- ('m', pgettext_lazy('Variant size', 'M')),- ('l', pgettext_lazy('Variant size', 'L')),- ('xl', pgettext_lazy('Variant size', 'XL')),- ('xxl', pgettext_lazy('Variant size', 'XXL')))-- product = models.ForeignKey(Shirt, related_name='variants')- size = models.CharField(choices=SIZE_CHOICES, max_length=3)diff --git a/saleor/product/models/__init__.py b/saleor/product/models/__init__.pynew file mode 100644index 0000000000..4965b20314--- /dev/null+++ b/saleor/product/models/__init__.py@@ -0,0 +1,5 @@+from base_products import *+from discounts import *+from products import *+from variants import *+from images import *\ No newline at end of filediff --git a/saleor/product/models/base_products.py b/saleor/product/models/base_products.pynew file mode 100644index 0000000000..b4b94c91a0--- /dev/null+++ b/saleor/product/models/base_products.py@@ -0,0 +1,78 @@+from __future__ import unicode_literals+import re++from django.core.urlresolvers import reverse+from django.db import models+from django.utils.safestring import mark_safe+from django.utils.translation import pgettext_lazy+from mptt.models import MPTTModel+from satchless.item import ItemRange+from unidecode import unidecode++from ...core.utils.models import Subtyped+++class Category(MPTTModel):+ name = models.CharField(+ pgettext_lazy('Category field', 'name'), max_length=128)+ slug = models.SlugField(+ pgettext_lazy('Category field', 'slug'), max_length=50, unique=True)+ description = models.TextField(+ pgettext_lazy('Category field', 'description'), blank=True)+ parent = models.ForeignKey(+ 'self', null=True, blank=True, related_name='children',+ verbose_name=pgettext_lazy('Category field', 'parent'))++ def __unicode__(self):+ return self.name++ def get_absolute_url(self):+ return reverse('product:category', kwargs={'slug': self.slug})++ class Meta:+ verbose_name_plural = 'categories'+ app_label = 'product'+++class ProductCollection(models.Model):+ name = models.CharField(pgettext_lazy('Product Collection name', 'name'),+ max_length=100, db_index=True, unique=True)++ class Meta:+ app_label = 'product'++ def __unicode__(self):+ return self.name+++class Product(Subtyped, ItemRange):+ name = models.CharField(+ pgettext_lazy('Product field', 'name'), max_length=128)+ category = models.ForeignKey(+ Category, verbose_name=pgettext_lazy('Product field', 'category'),+ related_name='products')++ collection = models.ForeignKey(ProductCollection, null=True, blank=True)++ class Meta:+ app_label = 'product'++ def __unicode__(self):+ return self.name++ def get_absolute_url(self):+ return reverse('product:details', kwargs={'slug': self.get_slug(),+ 'product_id': self.id})++ def get_slug(self):+ value = unidecode(self.name)+ value = re.sub(r'[^\w\s-]', '', value).strip().lower()++ return mark_safe(re.sub(r'[-\s]+', '-', value))++ def get_products_from_collection(self):+ return Product.objects.filter(collection=self.collection,+ collection__isnull=False)++ def __iter__(self):+ return iter(self.variants.all())\ No newline at end of filediff --git a/saleor/product/models/discounts.py b/saleor/product/models/discounts.pynew file mode 100644index 0000000000..6082c7e04b--- /dev/null+++ b/saleor/product/models/discounts.py@@ -0,0 +1,54 @@+from __future__ import unicode_literals++from django.conf import settings+from django.db import models+from django.utils.translation import pgettext_lazy+from django_prices.models import PriceField+from prices import FixedDiscount++from base_products import Product+++class NotApplicable(ValueError):+ pass+++class ProductDiscountManager(models.Manager):+ def for_product(self, variant):+ # Add a caching layer here to reduce the number of queries+ return self.get_query_set().filter(products=variant.product)+++class FixedProductDiscount(models.Model):+ name = models.CharField(max_length=255)+ products = models.ManyToManyField(Product, blank=True)+ discount = PriceField(pgettext_lazy('Discount field', 'discount value'),+ currency=settings.DEFAULT_CURRENCY,+ max_digits=12, decimal_places=4)++ objects = ProductDiscountManager()++ class Meta:+ app_label = 'product'++ def modifier_for_product(self, variant):+ if not self.products.filter(pk=variant.product.pk).exists():+ raise NotApplicable('Discount not applicable for this product')+ if self.discount > variant.get_price(discounted=False):+ raise NotApplicable('Discount too high for this product')+ return FixedDiscount(self.discount, name=self.name)++ def __unicode__(self):+ return self.name++ def __repr__(self):+ return 'FixedProductDiscount(name=%r, discount=%r)' % (+ str(self.discount), self.name)+++def get_product_discounts(variant, **kwargs):+ for discount in FixedProductDiscount.objects.for_product(variant):+ try:+ yield discount.modifier_for_product(variant, **kwargs)+ except NotApplicable:+ passdiff --git a/saleor/product/models/images.py b/saleor/product/models/images.pynew file mode 100644index 0000000000..18fca48361--- /dev/null+++ b/saleor/product/models/images.py@@ -0,0 +1,27 @@+from __future__ import unicode_literals++from django.db import models+from django_images.models import Image+from django.utils.safestring import mark_safe++from base_products import Product+++class ImageManager(models.Manager):+ def first(self):+ return self.get_query_set()[0]+++class ProductImage(Image):+ product = models.ForeignKey(Product, related_name='images')++ objects = ImageManager()++ class Meta:+ ordering = ['id']+ app_label = 'product'++ def __unicode__(self):+ html = '<img src="%s" alt="">' % (+ self.get_absolute_url('admin'),)+ return mark_safe(html)diff --git a/saleor/product/models/products.py b/saleor/product/models/products.pynew file mode 100644index 0000000000..0b8b7f6639--- /dev/null+++ b/saleor/product/models/products.py@@ -0,0 +1,41 @@+from __future__ import unicode_literals++from django.utils.translation import pgettext_lazy+from django.db import models++from base_products import Product+from variants import ProductVariant, PhysicalProduct, ColoredVariants+from variants import StockedProduct+++class Bag(Product, PhysicalProduct, ColoredVariants):+ class Meta:+ app_label = 'product'+++class Shirt(Product, PhysicalProduct, ColoredVariants):+ class Meta:+ app_label = 'product'+++class BagVariant(ProductVariant, StockedProduct):+ product = models.ForeignKey(Bag, related_name='variants')++ class Meta:+ app_label = 'product'+++class ShirtVariant(ProductVariant, StockedProduct):+ SIZE_CHOICES = (+ ('xs', pgettext_lazy('Variant size', 'XS')),+ ('s', pgettext_lazy('Variant size', 'S')),+ ('m', pgettext_lazy('Variant size', 'M')),+ ('l', pgettext_lazy('Variant size', 'L')),+ ('xl', pgettext_lazy('Variant size', 'XL')),+ ('xxl', pgettext_lazy('Variant size', 'XXL')))++ product = models.ForeignKey(Shirt, related_name='variants')+ size = models.CharField(choices=SIZE_CHOICES, max_length=3)++ class Meta:+ app_label = 'product'\ No newline at end of filediff --git a/saleor/product/models/variants.py b/saleor/product/models/variants.pynew file mode 100644index 0000000000..0417bd97a7--- /dev/null+++ b/saleor/product/models/variants.py@@ -0,0 +1,91 @@+from __future__ import unicode_literals+from decimal import Decimal++from django.db import models+from django.utils.translation import pgettext_lazy+from satchless.item import Item, StockedItem+from django_prices.models import PriceField+from django.conf import settings+from django.core.urlresolvers import reverse++from discounts import get_product_discounts+++class Color(models.Model):+ name = models.CharField(pgettext_lazy('Color name field', 'name'),+ max_length=100)+ color = models.CharField(pgettext_lazy('Color hex value', 'HEX value'),+ max_length=6)++ class Meta:+ app_label = 'product'++ def __unicode__(self):+ return self.name+++class StockedProduct(models.Model, StockedItem):+ stock = models.DecimalField(pgettext_lazy('Product item field', 'stock'),+ max_digits=10, decimal_places=4,+ default=Decimal(1))++ class Meta:+ abstract = True+ app_label = 'product'++ def get_stock(self):+ return self.stock+++class PhysicalProduct(models.Model):+ weight = models.DecimalField(max_digits=6, decimal_places=2)+ length = models.DecimalField(+ max_digits=6, decimal_places=2, blank=True, default=0)+ width = models.DecimalField(+ max_digits=6, decimal_places=2, blank=True, default=0)+ depth = models.DecimalField(+ max_digits=6, decimal_places=2, blank=True, default=0)++ class Meta:+ abstract = True+ app_label = 'product'+++class ColoredVariants(models.Model):+ color = models.ForeignKey(Color)++ class Meta:+ abstract = True+ app_label = 'product'+++class ProductVariant(models.Model, Item):+ name = models.CharField(pgettext_lazy('Product field', 'name'),+ max_length=128, blank=True, default='')+ price = PriceField(pgettext_lazy('Product field', 'price'),+ currency=settings.DEFAULT_CURRENCY,+ max_digits=12, decimal_places=4)+ sku = models.CharField(+ pgettext_lazy('Product field', 'sku'), max_length=32, unique=True)++ class Meta:+ abstract = True+ app_label = 'product'++ def __unicode__(self):+ return self.name or self.product.name++ def get_price_per_item(self, discounted=True, **kwargs):+ price = self.price+ if discounted:+ discounts = list(get_product_discounts(self, **kwargs))+ if discounts:+ modifier = max(discounts)+ price += modifier+ return price++ def get_absolute_url(self):+ slug = self.product.get_slug()+ product_id = self.product.id+ return reverse(+ 'product:details', kwargs={'slug': slug, 'product_id': product_id})\ No newline at end of file