Skip to content

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

Adjust dashboard and API to recent changes

Marcin Gębala8b2a5254bb3c
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index e164fdb902..38cd08b5fc 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -1,7 +1,7 @@
import bleach
from django import forms
from django.conf import settings
-from django.db.models import Count
+from django.db.models import Count, Q
from django.forms.models import ModelChoiceIterator
from django.forms.widgets import CheckboxSelectMultiple
from django.utils.encoding import smart_text
@@ -76,6 +76,15 @@ class ProductTypeForm(forms.ModelForm):
'ProductVariant weight help text',
'Default weight that will be used for calculating shipping'
' price for products of that type.'))
+ product_attributes = forms.ModelMultipleChoiceField(
+ queryset=Attribute.objects.none(), required=False,
+ label=pgettext_lazy(
+ 'Product type attributes', 'Attributes common to all variants.'))
+ variant_attributes = forms.ModelMultipleChoiceField(
+ queryset=Attribute.objects.none(), required=False,
+ label=pgettext_lazy(
+ 'Product type attributes',
+ 'Attributes specific to each variant.'))
class Meta:
model = ProductType
@@ -87,12 +96,6 @@ class ProductTypeForm(forms.ModelForm):
'has_variants': pgettext_lazy(
'Enable variants',
'Enable variants'),
- 'variant_attributes': pgettext_lazy(
- 'Product type attributes',
- 'Attributes specific to each variant'),
- 'product_attributes': pgettext_lazy(
- 'Product type attributes',
- 'Attributes common to all variants'),
'is_shipping_required': pgettext_lazy(
'Shipping toggle',
'Require shipping')}
@@ -100,12 +103,33 @@ class ProductTypeForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['tax_rate'].choices = get_tax_rate_type_choices()
+ unassigned_attrs_q = Q(
+ product_type__isnull=True, product_variant_type__isnull=True)
+
+ if self.instance.pk:
+ product_attrs_qs = Attribute.objects.filter(
+ Q(product_type=self.instance) | unassigned_attrs_q)
+ variant_attrs_qs = Attribute.objects.filter(
+ Q(product_variant_type=self.instance) | unassigned_attrs_q)
+ product_attrs_initial = self.instance.product_attributes.all()
+ variant_attrs_initial = self.instance.variant_attributes.all()
+ else:
+ unassigned_attrs = Attribute.objects.filter(unassigned_attrs_q)
+ product_attrs_qs = unassigned_attrs
+ variant_attrs_qs = unassigned_attrs
+ product_attrs_initial = []
+ variant_attrs_initial = []
+
+ self.fields['product_attributes'].queryset = product_attrs_qs
+ self.fields['variant_attributes'].queryset = variant_attrs_qs
+ self.fields['product_attributes'].initial = product_attrs_initial
+ self.fields['variant_attributes'].initial = variant_attrs_initial
def clean(self):
data = super().clean()
has_variants = self.cleaned_data['has_variants']
- product_attr = set(self.cleaned_data['product_attributes'])
- variant_attr = set(self.cleaned_data['variant_attributes'])
+ product_attr = set(self.cleaned_data.get('product_attributes', []))
+ variant_attr = set(self.cleaned_data.get('variant_attributes', []))
if not has_variants and variant_attr:
msg = pgettext_lazy(
'Product type form error',
@@ -137,10 +161,11 @@ class ProductTypeForm(forms.ModelForm):
product__product_type__variant_attributes__in=attributes_changed)
variants_to_be_updated = variants_to_be_updated.prefetch_related(
'product__product_type__variant_attributes__values').all()
- attributes = self.instance.variant_attributes.all()
+ attributes = self.instance.variant_attributes.prefetch_related(
+ 'translations', 'values__translations')
for variant in variants_to_be_updated:
variant.name = get_name_from_attributes(variant, attributes)
- variant.save()
+ variant.save(update_fields=['name'])
def check_if_variants_changed(self, has_variants):
variants_changed = (
@@ -156,6 +181,14 @@ class ProductTypeForm(forms.ModelForm):
'one variant.')
self.add_error('has_variants', msg)
+ def save(self, *args, **kwargs):
+ instance = super().save(*args, **kwargs)
+ new_product_attrs = self.cleaned_data.get('product_attributes', [])
+ new_variant_attrs = self.cleaned_data.get('variant_attributes', [])
+ instance.product_attributes.set(new_product_attrs)
+ instance.variant_attributes.set(new_variant_attrs)
+ return instance
+
class AttributesMixin:
"""Form mixin that dynamically adds attribute fields."""
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 9daa3e4493..64849c3891 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -532,16 +532,19 @@ def ajax_upload_image(request, product_pk):
@staff_member_required
@permission_required('product.manage_products')
def attribute_list(request):
- attributes = (Attribute.objects.prefetch_related('values')
- .order_by('name'))
+ attributes = (
+ Attribute.objects.prefetch_related(
+ 'values', 'product_type', 'product_variant_type').order_by('name'))
attribute_filter = AttributeFilter(request.GET, queryset=attributes)
- attributes = [
- (attribute.pk, attribute.name, attribute.values.all())
- for attribute in attribute_filter.qs]
+ attributes = [(
+ attribute.pk, attribute.name,
+ attribute.product_type or attribute.product_variant_type,
+ attribute.values.all()) for attribute in attribute_filter.qs]
attributes = get_paginator_items(
attributes, settings.DASHBOARD_PAGINATE_BY, request.GET.get('page'))
ctx = {
- 'attributes': attributes, 'filter_set': attribute_filter,
+ 'attributes': attributes,
+ 'filter_set': attribute_filter,
'is_empty': not attribute_filter.queryset.exists()}
return TemplateResponse(
request, 'dashboard/product/attribute/list.html', ctx)
@@ -550,10 +553,13 @@ def attribute_list(request):
@staff_member_required
@permission_required('product.manage_products')
def attribute_details(request, pk):
- attributes = Attribute.objects.prefetch_related('values').all()
+ attributes = Attribute.objects.prefetch_related(
+ 'values', 'product_type', 'product_variant_type').all()
attribute = get_object_or_404(attributes, pk=pk)
+ product_type = attribute.product_type or attribute.product_variant_type
values = attribute.values.all()
- ctx = {'attribute': attribute, 'values': values}
+ ctx = {
+ 'attribute': attribute, 'product_type': product_type, 'values': values}
return TemplateResponse(
request, 'dashboard/product/attribute/detail.html', ctx)
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 58521f3122..47906ff647 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -188,6 +188,10 @@ class AttributeUpdate(AttributeMixin, ModelMutation):
description = 'Updates attribute.'
model = models.Attribute
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('product.manage_products')
+
@classmethod
def clean_remove_values(cls, cleaned_input, instance, errors):
"""Check if AttributeValues to be removed are assigned to given
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index dbf22c04c2..8c7fc774c9 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -449,6 +449,14 @@ class ProductTypeCreate(ModelMutation):
def user_is_allowed(cls, user, input):
return user.has_perm('product.manage_products')
+ @classmethod
+ def _save_m2m(cls, info, instance, cleaned_data):
+ super()._save_m2m(info, instance, cleaned_data)
+ if 'product_attributes' in cleaned_data:
+ instance.product_attributes.set(cleaned_data['product_attributes'])
+ if 'variant_attributes' in cleaned_data:
+ instance.variant_attributes.set(cleaned_data['variant_attributes'])
+
class ProductTypeUpdate(ProductTypeCreate):
class Arguments:
diff --git a/templates/dashboard/product/attribute/detail.html b/templates/dashboard/product/attribute/detail.html
index 13464ce8bf..9d3ea5eaec 100644
--- a/templates/dashboard/product/attribute/detail.html
+++ b/templates/dashboard/product/attribute/detail.html
@@ -42,6 +42,11 @@
<span class="card-title">
{{ attribute.name }}
</span>
+ {% if product_type %}
+ {% trans "Product type" %}: <a href="{% url "dashboard:product-type-update" pk=product_type.pk %}">{{ product_type }}</a>
+ {% elif attribute.pk %}
+ <a href="{% url "dashboard:product-type-list" %}">Assign to a product type</a>
+ {% endif %}
</div>
<div class="card-action">
<a class="btn-flat waves-effect" href="{% url "dashboard:attribute-update" attribute.pk %}">
diff --git a/templates/dashboard/product/attribute/list.html b/templates/dashboard/product/attribute/list.html
index 5338c48409..10504e61b6 100644
--- a/templates/dashboard/product/attribute/list.html
+++ b/templates/dashboard/product/attribute/list.html
@@ -40,18 +40,22 @@
<thead>
<tr>
{% trans "Name" context "Dashboard attributes list table header name" as label %}
- {% sorting_header 'name' label is_wide=True %}
+ {% sorting_header 'name' label %}
+ <th>{% trans "Product type" context "Name of product type related to the attribute" %}</th>
<th>
{% trans "Values" context "Dashboard attributes list table header values" %}
</th>
</tr>
</thead>
<tbody>
- {% for pk, name, values in attributes %}
+ {% for pk, name, product_type, values in attributes %}
<tr data-action-go="{% url 'dashboard:attribute-details' pk=pk %}">
<td>
{{ name }}
</td>
+ <td>
+ {{ product_type|default:'-' }}
+ </td>
<td>
{% if values %}{{ values|join:", " }}{% else %}-{% endif %}
</td>
diff --git a/templates/dashboard/product/product_type/list.html b/templates/dashboard/product/product_type/list.html
index 6bb82403bf..5c6d42216d 100644
--- a/templates/dashboard/product/product_type/list.html
+++ b/templates/dashboard/product/product_type/list.html
@@ -42,7 +42,7 @@
<thead>
<tr>
{% trans "Name" context "Dashboard product types list table header name" as label %}
- {% sorting_header 'name' label is_wide=True %}
+ {% sorting_header 'name' label %}
<th>
{% trans "Product attributes" context "Product type item list text" %}
diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.py
index 47510284b2..40810c7f1e 100644
--- a/tests/dashboard/test_product.py
+++ b/tests/dashboard/test_product.py
@@ -781,7 +781,7 @@ def test_view_attribute_list(db, admin_client, color_attribute):
assert len(result) == 1
assert result[0][0] == color_attribute.pk
assert result[0][1] == color_attribute.name
- assert len(result[0][2]) == 2
+ assert len(result[0][3]) == 2
assert not response.context['is_empty']