Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Move product models to single module

Marcin Gębala84295d23e317
diff --git a/saleor/core/templatetags/attributes.py b/saleor/core/templatetags/attributes.py
index b9cb118614..88c825a597 100644
--- a/saleor/core/templatetags/attributes.py
+++ b/saleor/core/templatetags/attributes.py
@@ -1,6 +1,6 @@
from django.template import Library
-from ...product.models.utils import get_attributes_display_map
+from ...product.utils import get_attributes_display_map
register = Library()
diff --git a/saleor/product/models/base.py b/saleor/product/models.py
similarity index 85%
rename from saleor/product/models/base.py
rename to saleor/product/models.py
index cd5e6d4469..3572d5f9f9 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models.py
@@ -8,7 +8,7 @@ from django.contrib.postgres.fields import HStoreField
from django.core.urlresolvers import reverse
from django.core.validators import MinValueValidator, RegexValidator
from django.db import models
-from django.db.models import F, Manager, Q
+from django.db.models import F, Manager, Max, Q
from django.utils.encoding import python_2_unicode_compatible, smart_text
from django.utils.text import slugify
from django.utils.translation import pgettext_lazy
@@ -19,10 +19,11 @@ from mptt.models import MPTTModel
from prices import PriceRange
from satchless.item import InsufficientStock, Item, ItemRange
from text_unidecode import unidecode
+from versatileimagefield.fields import VersatileImageField, PPOIField
-from ...discount.models import calculate_discounted_price
+from ..discount.models import calculate_discounted_price
+from ..search import index
from .utils import get_attributes_display_map
-from ...search import index
class CategoryManager(Manager):
@@ -418,3 +419,66 @@ class AttributeChoiceValue(models.Model):
def __str__(self):
return self.display
+
+
+class ImageManager(models.Manager):
+ def first(self):
+ try:
+ return self.get_queryset()[0]
+ except IndexError:
+ pass
+
+
+class ProductImage(models.Model):
+ product = models.ForeignKey(
+ Product, related_name='images',
+ verbose_name=pgettext_lazy('Product image field', 'product'))
+ image = VersatileImageField(
+ upload_to='products', ppoi_field='ppoi', blank=False,
+ verbose_name=pgettext_lazy('Product image field', 'image'))
+ ppoi = PPOIField(verbose_name=pgettext_lazy('Product image field', 'ppoi'))
+ alt = models.CharField(
+ pgettext_lazy('Product image field', 'short description'),
+ max_length=128, blank=True)
+ order = models.PositiveIntegerField(
+ pgettext_lazy('Product image field', 'order'),
+ editable=False)
+
+ objects = ImageManager()
+
+ class Meta:
+ ordering = ('order', )
+ app_label = 'product'
+ verbose_name = pgettext_lazy('Product image model', 'product image')
+ verbose_name_plural = pgettext_lazy('Product image model', 'product images')
+
+ def get_ordering_queryset(self):
+ return self.product.images.all()
+
+ def save(self, *args, **kwargs):
+ if self.order is None:
+ qs = self.get_ordering_queryset()
+ existing_max = qs.aggregate(Max('order'))
+ existing_max = existing_max.get('order__max')
+ self.order = 0 if existing_max is None else existing_max + 1
+ super(ProductImage, self).save(*args, **kwargs)
+
+ def delete(self, *args, **kwargs):
+ qs = self.get_ordering_queryset()
+ qs.filter(order__gt=self.order).update(order=F('order') - 1)
+ super(ProductImage, self).delete(*args, **kwargs)
+
+
+class VariantImage(models.Model):
+ variant = models.ForeignKey(
+ 'ProductVariant', related_name='variant_images',
+ verbose_name=pgettext_lazy('Variant image field', 'variant'))
+ image = models.ForeignKey(
+ ProductImage, related_name='variant_images',
+ verbose_name=pgettext_lazy('Variant image field', 'image'))
+
+ class Meta:
+ verbose_name = pgettext_lazy(
+ 'Variant image model', 'variant image')
+ verbose_name_plural = pgettext_lazy(
+ 'Variant image model', 'variant images')
diff --git a/saleor/product/models/__init__.py b/saleor/product/models/__init__.py
deleted file mode 100644
index 0636ed68f7..0000000000
--- a/saleor/product/models/__init__.py
+++ /dev/null
@@ -1,7 +0,0 @@
-from .base import (AttributeChoiceValue, Category, Product, ProductAttribute,
- ProductVariant, Stock, ProductClass, StockLocation)
-from .images import ProductImage, VariantImage
-
-__all__ = ['AttributeChoiceValue', 'Category', 'Product', 'ProductAttribute',
- 'ProductImage', 'ProductVariant', 'Stock', 'VariantImage',
- 'ProductClass', 'StockLocation']
diff --git a/saleor/product/models/images.py b/saleor/product/models/images.py
deleted file mode 100644
index 6c688f338a..0000000000
--- a/saleor/product/models/images.py
+++ /dev/null
@@ -1,71 +0,0 @@
-from __future__ import unicode_literals
-
-from django.db import models
-from django.db.models import Max, F
-from django.utils.translation import pgettext_lazy
-from versatileimagefield.fields import VersatileImageField, PPOIField
-
-from .base import Product
-
-
-class ImageManager(models.Manager):
- def first(self):
- try:
- return self.get_queryset()[0]
- except IndexError:
- pass
-
-
-class ProductImage(models.Model):
- product = models.ForeignKey(
- Product, related_name='images',
- verbose_name=pgettext_lazy('Product image field', 'product'))
- image = VersatileImageField(
- upload_to='products', ppoi_field='ppoi', blank=False,
- verbose_name=pgettext_lazy('Product image field', 'image'))
- ppoi = PPOIField(verbose_name=pgettext_lazy('Product image field', 'ppoi'))
- alt = models.CharField(
- pgettext_lazy('Product image field', 'short description'),
- max_length=128, blank=True)
- order = models.PositiveIntegerField(
- pgettext_lazy('Product image field', 'order'),
- editable=False)
-
- objects = ImageManager()
-
- class Meta:
- ordering = ('order', )
- app_label = 'product'
- verbose_name = pgettext_lazy('Product image model', 'product image')
- verbose_name_plural = pgettext_lazy('Product image model', 'product images')
-
- def get_ordering_queryset(self):
- return self.product.images.all()
-
- def save(self, *args, **kwargs):
- if self.order is None:
- qs = self.get_ordering_queryset()
- existing_max = qs.aggregate(Max('order'))
- existing_max = existing_max.get('order__max')
- self.order = 0 if existing_max is None else existing_max + 1
- super(ProductImage, self).save(*args, **kwargs)
-
- def delete(self, *args, **kwargs):
- qs = self.get_ordering_queryset()
- qs.filter(order__gt=self.order).update(order=F('order') - 1)
- super(ProductImage, self).delete(*args, **kwargs)
-
-
-class VariantImage(models.Model):
- variant = models.ForeignKey(
- 'ProductVariant', related_name='variant_images',
- verbose_name=pgettext_lazy('Variant image field', 'variant'))
- image = models.ForeignKey(
- ProductImage, related_name='variant_images',
- verbose_name=pgettext_lazy('Variant image field', 'image'))
-
- class Meta:
- verbose_name = pgettext_lazy(
- 'Variant image model', 'variant image')
- verbose_name_plural = pgettext_lazy(
- 'Variant image model', 'variant images')
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index df015a872d..5428cbd294 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -8,7 +8,6 @@ from django_prices.templatetags import prices_i18n
from ..cart.utils import get_cart_from_request, get_or_create_cart_from_request
from ..core.utils import to_local_currency
from .forms import ProductForm
-from .models import Product
try:
from urllib.parse import urlencode
@@ -17,8 +16,8 @@ except ImportError:
def products_visible_to_user(user):
- if (user.is_authenticated() and
- user.is_active and user.is_staff):
+ from .models import Product
+ if user.is_authenticated() and user.is_active and user.is_staff:
return Product.objects.all()
else:
return Product.objects.get_available_products()
@@ -26,12 +25,11 @@ def products_visible_to_user(user):
def products_with_details(user):
products = products_visible_to_user(user)
- products = products.prefetch_related('categories', 'images',
- 'variants__stock',
- 'variants__variant_images__image',
- 'attributes__values',
- 'product_class__variant_attributes__values',
- 'product_class__product_attributes__values')
+ products = products.prefetch_related(
+ 'categories', 'images', 'variants__stock',
+ 'variants__variant_images__image', 'attributes__values',
+ 'product_class__variant_attributes__values',
+ 'product_class__product_attributes__values')
return products