Skip to content

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

Update brand in feed

Krzysztof Wolski68a7dd1a4669
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 437a63c07e..f79a3f3ba9 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -29,7 +29,8 @@ DEFAULT_SCHEMA = {
'T-Shirt': {
'product_attributes': {
'Color': ['Blue', 'White'],
- 'Collar': ['Round', 'V-Neck', 'Polo']
+ 'Collar': ['Round', 'V-Neck', 'Polo'],
+ 'Brand': ['Saleor']
},
'variant_attributes': {
'Size': ['XS', 'S', 'M', 'L', 'XL', 'XXL']
@@ -37,13 +38,16 @@ DEFAULT_SCHEMA = {
'images_dir': 't-shirts/',
},
'Mugs': {
- 'product_attributes': {},
+ 'product_attributes': {
+ 'Brand': ['Saleor']
+ },
'variant_attributes': {},
'images_dir': 'mugs/'
},
'Coffee': {
'product_attributes': {
- 'Coffee Genre': ['Arabica', 'Robusta']
+ 'Coffee Genre': ['Arabica', 'Robusta'],
+ 'Brand': ['Saleor']
},
'variant_attributes': {
'Box Size': ['100g', '250g', '500g', '1kg']
@@ -53,7 +57,8 @@ DEFAULT_SCHEMA = {
},
'Candy': {
'product_attributes': {
- 'Flavor': ['Sour', 'Sweet']
+ 'Flavor': ['Sour', 'Sweet'],
+ 'Brand': ['Saleor']
},
'variant_attributes': {
'Candy Box Size': ['100g', '250g', '500g']
diff --git a/saleor/data_feeds/google_merchant.py b/saleor/data_feeds/google_merchant.py
index f4eedba8e5..a76fa25a49 100644
--- a/saleor/data_feeds/google_merchant.py
+++ b/saleor/data_feeds/google_merchant.py
@@ -8,9 +8,11 @@ from django.contrib.sites.models import Site
from django.contrib.syndication.views import add_domain
from django.core.files.storage import default_storage
from django.utils import six
+from django.utils.encoding import smart_text
from ..discount.models import Sale
-from ..product.models import Category, ProductAttribute, ProductVariant
+from ..product.models import (AttributeChoiceValue, Category, ProductAttribute,
+ ProductVariant)
CATEGORY_SEPARATOR = ' > '
@@ -71,7 +73,7 @@ def item_condition(item):
return 'new'
-def item_brand(item, attributes_dict):
+def item_brand(item, attributes_dict, attribute_values_dict):
"""
This field is required.
Read more:
@@ -91,6 +93,10 @@ def item_brand(item, attributes_dict):
if brand is None:
brand = item.product.get_attribute(publisher_attribute_pk)
+ if brand is not None:
+ brand_name = attribute_values_dict.get(brand)
+ if brand_name is not None:
+ return brand_name
return brand
@@ -154,7 +160,7 @@ def item_sale_price(item, discounts):
def item_attributes(item, categories, category_paths, current_site,
- discounts, attributes_dict):
+ discounts, attributes_dict, attribute_values_dict):
product_data = {
'id': item_id(item),
'title': item_title(item),
@@ -181,7 +187,7 @@ def item_attributes(item, categories, category_paths, current_site,
if tax:
product_data['tax'] = tax
- brand = item_brand(item, attributes_dict)
+ brand = item_brand(item, attributes_dict, attribute_values_dict)
if brand:
product_data['brand'] = brand
@@ -198,11 +204,14 @@ def write_feed(file_obj):
discounts = Sale.objects.all().prefetch_related('products',
'categories')
attributes_dict = {a.name: a.pk for a in ProductAttribute.objects.all()}
+ attribute_values_dict = {smart_text(a.pk): smart_text(a) for a
+ in AttributeChoiceValue.objects.all()}
category_paths = {}
current_site = Site.objects.get_current()
for item in get_feed_items():
item_data = item_attributes(item, categories, category_paths,
- current_site, discounts, attributes_dict)
+ current_site, discounts, attributes_dict,
+ attribute_values_dict)
writer.writerow(item_data)
diff --git a/tests/test_data_feeds.py b/tests/test_data_feeds.py
index 8461cd311b..ab853beb73 100644
--- a/tests/test_data_feeds.py
+++ b/tests/test_data_feeds.py
@@ -1,16 +1,18 @@
from __future__ import unicode_literals
import csv
+
from mock import Mock
from django.contrib.sites.models import Site
from django.utils import six
+from django.utils.encoding import smart_text
if six.PY3:
from io import StringIO
else:
from StringIO import StringIO
-from saleor.product.models import Category
+from saleor.product.models import AttributeChoiceValue, Category
from saleor.data_feeds.google_merchant import (get_feed_items,
item_attributes,
item_google_product_category,
@@ -26,8 +28,11 @@ def test_saleor_feed_items(product_in_stock):
category_paths = {}
attributes_dict = {}
current_site = Site.objects.get_current()
+ attribute_values_dict = {smart_text(a.pk): smart_text(a) for a
+ in AttributeChoiceValue.objects.all()}
attributes = item_attributes(items[0], categories, category_paths,
- current_site, discounts, attributes_dict)
+ current_site, discounts, attributes_dict,
+ attribute_values_dict)
assert attributes.get('mpn') == valid_variant.sku
assert attributes.get('availability') == 'in stock'