Skip to content

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

Fix compability with django 1.8

Krzysztof Wolski5e82c67a3077
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index 8a85523bf7..52df2cfdcf 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -161,10 +161,10 @@ class Product(models.Model, ItemRange):
return None
def get_attribute(self, pk):
- return self.attributes.get(str(pk))
+ return self.attributes.get(smart_text(pk))
def set_attribute(self, pk, value_pk):
- self.attributes[str(pk)] = value_pk
+ self.attributes[smart_text(pk)] = smart_text(value_pk)
@python_2_unicode_compatible
@@ -236,10 +236,10 @@ class ProductVariant(models.Model, Item):
[stock.quantity_available > 0 for stock in self.stock.all()])
def get_attribute(self, pk):
- return self.attributes.get(str(pk))
+ return self.attributes.get(smart_text(pk))
def set_attribute(self, pk, value_pk):
- self.attributes[str(pk)] = value_pk
+ self.attributes[smart_text(pk)] = smart_text(value_pk)
def display_variant(self, attributes=None):
if attributes is None:
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index 45876c924e..39c80ad05d 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -1,7 +1,5 @@
from collections import namedtuple
-from django.db.models import Q
-
from ..core.utils import to_local_currency
from .models import Product
@@ -74,9 +72,3 @@ def get_availability(product, discounts=None, local_currency=None):
discount=discount,
price_range_local_currency=price_range_local,
discount_local_currency=discount_local_currency)
-
-
-def filter_by_attribute(queryset, key, value):
- in_product = Q(attributes__contains={key.pk: value.pk})
- in_variant = Q(variants__attributes__contains={key.pk: value.pk})
- return queryset.filter(in_product | in_variant)
diff --git a/tests/test_product.py b/tests/test_product.py
index a011d34e94..a07d3ebba3 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -3,7 +3,8 @@ import datetime
from mock import Mock
from saleor.product import models
-from saleor.product.utils import get_availability, filter_by_attribute
+from saleor.product.utils import get_availability
+from tests.utils import filter_products_by_attribute
def test_stock_selector(product_in_stock):
@@ -80,18 +81,18 @@ def test_filtering_by_attribute(db, color_attribute):
variant_b.set_attribute(color_attribute.pk, color.pk)
variant_b.save()
- filtered = filter_by_attribute(models.Product.objects.all(),
- color_attribute, color)
- assert product_a in filtered
- assert product_b in filtered
+ filtered = filter_products_by_attribute(models.Product.objects.all(),
+ color_attribute.pk, color.pk)
+ assert product_a in list(filtered)
+ assert product_b in list(filtered)
product_a.set_attribute(color_attribute.pk, color_2.pk)
product_a.save()
- filtered = filter_by_attribute(models.Product.objects.all(),
- color_attribute, color)
- assert product_a not in filtered
- assert product_b in filtered
- filtered = filter_by_attribute(models.Product.objects.all(),
- color_attribute, color_2)
- assert product_a in filtered
- assert product_b not in filtered
+ filtered = filter_products_by_attribute(models.Product.objects.all(),
+ color_attribute.pk, color.pk)
+ assert product_a not in list(filtered)
+ assert product_b in list(filtered)
+ filtered = filter_products_by_attribute(models.Product.objects.all(),
+ color_attribute.pk, color_2.pk)
+ assert product_a in list(filtered)
+ assert product_b not in list(filtered)
diff --git a/tests/utils.py b/tests/utils.py
index 8cb0135b73..7b2c7bfad2 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -3,6 +3,9 @@ try:
except ImportError:
from urlparse import urlparse
+from django.db.models import Q
+from django.utils.encoding import smart_text
+
def get_redirect_location(response):
# Due to Django 1.8 compatibility, we have to handle both cases
@@ -11,3 +14,11 @@ def get_redirect_location(response):
url = urlparse(location)
location = url.path
return location
+
+
+def filter_products_by_attribute(queryset, attribute_id, value):
+ key = smart_text(attribute_id)
+ value = smart_text(value)
+ in_product = Q(attributes__contains={key: value})
+ in_variant = Q(variants__attributes__contains={key: value})
+ return queryset.filter(in_product | in_variant)