Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move logic to a helper
Patryk Zawadzkia1395d4902fc
diff --git a/saleor/core/templatetags/attributes.py b/saleor/core/templatetags/attributes.pyindex 88c825a597..b9cb118614 100644--- a/saleor/core/templatetags/attributes.py+++ b/saleor/core/templatetags/attributes.py@@ -1,6 +1,6 @@from django.template import Library-from ...product.utils import get_attributes_display_map+from ...product.models.utils import get_attributes_display_mapregister = Library()diff --git a/saleor/product/models/base.py b/saleor/product/models/base.pyindex 2ddddd105c..14278da979 100644--- a/saleor/product/models/base.py+++ b/saleor/product/models/base.py@@ -21,7 +21,7 @@ from unidecode import unidecodefrom versatileimagefield.fields import VersatileImageFieldfrom ...discount.models import get_variant_discounts-from ..utils import get_attributes_display_map+from .utils import get_attributes_display_mapfrom .fields import WeightFielddiff --git a/saleor/product/models/utils.py b/saleor/product/models/utils.pynew file mode 100644index 0000000000..2e5aef5e93--- /dev/null+++ b/saleor/product/models/utils.py@@ -0,0 +1,12 @@+def get_attributes_display_map(variant, attributes):+ display = {}+ for attribute in attributes:+ value = variant.get_attribute(attribute.pk)+ if value:+ choices = {a.pk: a for a in attribute.values.all()}+ attr = choices.get(value)+ if attr:+ display[attribute.pk] = attr+ else:+ display[attribute.pk] = value+ return displaydiff --git a/saleor/product/utils.py b/saleor/product/utils.pyindex 2e5aef5e93..8194e553cd 100644--- a/saleor/product/utils.py+++ b/saleor/product/utils.py@@ -1,12 +1,19 @@-def get_attributes_display_map(variant, attributes):- display = {}- for attribute in attributes:- value = variant.get_attribute(attribute.pk)- if value:- choices = {a.pk: a for a in attribute.values.all()}- attr = choices.get(value)- if attr:- display[attribute.pk] = attr- else:- display[attribute.pk] = value- return display+from .models import Product+++def products_visible_to_user(user):+ if (user.is_authenticated() and+ user.is_active and user.is_staff):+ return Product.objects.all()+ else:+ return Product.objects.get_available_products()+++def products_with_details(user):+ products = products_visible_to_user(user)+ products = products.select_subclasses()+ products = products.prefetch_related('categories', 'images',+ 'variants__stock',+ 'variants__variant_images__image',+ 'attributes__values')+ return productsdiff --git a/saleor/product/views.py b/saleor/product/views.pyindex 398fcc2214..85170a1fff 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -10,20 +10,12 @@ from django.template.response import TemplateResponsefrom ..cart.decorators import get_cart_from_requestfrom ..core.utils import get_paginator_itemsfrom .forms import get_form_class_for_product-from .models import Category, Product+from .models import Category+from .utils import products_with_detailsdef product_details(request, slug, product_id):- if (request.user.is_authenticated() and- request.user.is_active and request.user.is_staff):- products = Product.objects.all()- else:- products = Product.objects.get_available_products()- products = products.select_subclasses()- products = products.prefetch_related('categories', 'images',- 'variants__stock',- 'variants__variant_images__image',- 'attributes__values')+ products = products_with_details(user=request.user)product = get_object_or_404(products, id=product_id)if product.get_slug() != slug:return HttpResponsePermanentRedirect(product.get_absolute_url())