Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Optimize displaying attributes
Marcin Gębaladecbc2cffab0
diff --git a/saleor/core/templatetags/attributes.py b/saleor/core/templatetags/attributes.pynew file mode 100644index 0000000000..d78ca571ab--- /dev/null+++ b/saleor/core/templatetags/attributes.py@@ -0,0 +1,18 @@+from django.template import Library+from saleor.product.utils import get_attributes_display++register = Library()++ATTRIBUTE_EMPTY_VALUE = '-'+++def get_attributes_values(variant, attributes):+ attr_values = get_attributes_display(variant, attributes)+ values = []+ for attribute in attributes:+ if attribute.pk in attr_values:+ values.append(attr_values[attribute.pk])+ else:+ values.append(ATTRIBUTE_EMPTY_VALUE)+ return valuesdiff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 9759352405..e770281510 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -46,7 +46,7 @@ def product_edit(request, pk):product = get_object_or_404(Product.objects.select_subclasses().prefetch_related('images','variants'), pk=pk)- attributes = product.attributes.all()+ attributes = product.attributes.prefetch_related('values')images = product.images.all()variants = product.variants.select_subclasses()stock_items = Stock.objects.filter(variant__in=variants)diff --git a/saleor/dashboard/templates/dashboard/product/product_form.html b/saleor/dashboard/templates/dashboard/product/product_form.htmlindex 21dca6b762..e603447598 100644--- a/saleor/dashboard/templates/dashboard/product/product_form.html+++ b/saleor/dashboard/templates/dashboard/product/product_form.html@@ -3,6 +3,7 @@{% load i18n %}{% load materializecss %}{% load static %}+{% load attributes %}{% block title %}{{ title }} - {% trans "Products" %} - {{ block.super }}{% endblock %}@@ -155,8 +156,8 @@</th>{% endif %}<th>{% trans "SKU" %}</th>- {% for name, value in variants.0.get_attributes_display.items %}- <th>{{ name }}</th>+ {% for attribute in attributes %}+ <th>{{ attribute.display|capfirst }}</th>{% endfor %}<th class="wide">{% trans "Variant name" %}</th><th class="right-align">{% trans "Price" %}</th>@@ -176,9 +177,9 @@href="{% url 'dashboard:variant-update' product_pk=product.pk variant_pk=variant.pk %}">{{ variant.sku }}</a></td>- {% for name, attribute in variant.get_attributes_display.items %}+ {% for attr_display in variant|get_attributes_values:attributes %}<td>- {{ attribute }}+ {{ attr_display }}</td>{% endfor %}<td>{{ variant.name }}</td>diff --git a/saleor/product/forms.py b/saleor/product/forms.pyindex 8311ac722f..e04add0a2d 100644--- a/saleor/product/forms.py+++ b/saleor/product/forms.py@@ -6,27 +6,20 @@ from django.utils.translation import pgettext_lazyfrom django_prices.templatetags.prices_i18n import grossfrom ..cart.forms import AddToCartForm+from .utils import get_attributes_displayclass VariantChoiceIterator(ModelChoiceIterator):def __init__(self, field):super(VariantChoiceIterator, self).__init__(field)- attr_display = {}- if self.queryset:- product = self.queryset.instance- for attr in product.attributes.all():- attr_display[str(attr.pk)] = {choice.pk: choice.display for choice in attr.values.all()}- self.attr_display = attr_display+ self.product = self.queryset.instance if self.queryset else None+ self.attributes = self.product.attributes.prefetch_related(+ 'values') if self.product else Nonedef choice(self, obj):- if self.attr_display:- _label = []- for attr_pk, value in obj.attributes.items():- if attr_pk in self.attr_display:- _label.append(self.attr_display[attr_pk].get(value, ''))- else:- _label.append(value)- label = ', '.join(_label)+ if self.product:+ values = get_attributes_display(obj, self.attributes).values()+ label = ', '.join(values)else:label = self.field.label_from_instance(obj)label += ' - ' + gross(obj.get_price())diff --git a/saleor/product/models/base.py b/saleor/product/models/base.pyindex 0887da052f..e9d0b5e613 100644--- a/saleor/product/models/base.py+++ b/saleor/product/models/base.py@@ -2,7 +2,6 @@ from __future__ import unicode_literalsfrom decimal import Decimalfrom django.conf import settings-from django.core.exceptions import ObjectDoesNotExistfrom django.core.urlresolvers import reversefrom django.core.validators import MinValueValidator, RegexValidatorfrom django.db.models import Manager@@ -192,22 +191,6 @@ class ProductVariant(models.Model, Item):def is_available(self):return any([stock_item.is_available() for stock_item in self.stock.all()])- def get_attributes_display(self):- display = {}- for attr_pk, value in self.attributes.iteritems():- if self.product.attributes.filter(pk=attr_pk):- attribute = ProductAttribute.objects.get(pk=attr_pk)- if attribute.has_values():- try:- value = attribute.values.get(pk=value)- except ObjectDoesNotExist:- pass- else:- display[attribute.display] = value.display- else:- display[attribute.display] = value- return display-def get_attribute(self, pk):return self.attributes.get(str(pk))diff --git a/saleor/product/utils.py b/saleor/product/utils.pynew file mode 100644index 0000000000..8972e31ff3--- /dev/null+++ b/saleor/product/utils.py@@ -0,0 +1,17 @@+from django.core.exceptions import ObjectDoesNotExist+++def get_attributes_display(variant, attributes):+ display = {}+ for attribute in attributes:+ value = variant.get_attribute(attribute.pk)+ if value:+ try:+ choice = attribute.values.get(pk=value)+ except ObjectDoesNotExist:+ pass+ except ValueError:+ display[attribute.pk] = value+ else:+ display[attribute.pk] = choice.display+ return display