Skip to content

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

Introduce merge_dicts helper for Python3.4

Pacu2619741b88752
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index e8c0fdbe60..d85f5e3c91 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -136,3 +136,14 @@ def create_superuser(credentials):
else:
msg = 'Superuser already exists - %(email)s' % credentials
return msg
+
+
+def merge_dicts(*dict_args):
+ """Workaround for Python3.4 to merge two dicts within one line of code.
+ Given any number of dicts, shallow copy and merge into a new dict,
+ precedence goes to key value pairs in latter dicts.
+ """
+ result = {}
+ for dictionary in dict_args:
+ result.update(dictionary)
+ return result
diff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.py
index b427e7da9d..6ad68972cd 100644
--- a/saleor/dashboard/category/forms.py
+++ b/saleor/dashboard/category/forms.py
@@ -4,6 +4,7 @@ from django.utils.text import slugify
from django.utils.translation import pgettext_lazy
from text_unidecode import unidecode
+from ...core.utils import merge_dicts
from ...product.models import Category
from ..seo.utils import (
MIN_DESCRIPTION_LENGTH, MIN_TITLE_LENGTH, SEO_HELP_TEXTS, SEO_LABELS,
@@ -24,14 +25,12 @@ class CategoryForm(forms.ModelForm):
class Meta:
model = Category
exclude = ['slug']
- labels = {
- 'name': pgettext_lazy(
- 'Item name',
- 'Name'),
- 'description': pgettext_lazy(
- 'Description',
- 'Description'),
- **SEO_LABELS}
+ labels = merge_dicts(
+ {
+ 'name': pgettext_lazy('Item name', 'Name'),
+ 'description': pgettext_lazy('Description', 'Description')
+ },
+ SEO_LABELS)
help_texts = SEO_HELP_TEXTS
widgets = SEO_WIDGETS
diff --git a/saleor/dashboard/collection/forms.py b/saleor/dashboard/collection/forms.py
index e9eefea8cc..7d7c8cc6eb 100644
--- a/saleor/dashboard/collection/forms.py
+++ b/saleor/dashboard/collection/forms.py
@@ -4,6 +4,7 @@ from django.utils.text import slugify
from django.utils.translation import pgettext_lazy
from text_unidecode import unidecode
+from ...core.utils import merge_dicts
from ...product.models import Collection, Product
from ..forms import AjaxSelect2MultipleChoiceField
from ..seo.utils import (
@@ -19,17 +20,15 @@ class CollectionForm(forms.ModelForm):
class Meta:
model = Collection
exclude = ['slug']
- labels = {
- 'name': pgettext_lazy(
- 'Item name',
- 'Name'),
- 'products': pgettext_lazy(
- 'Products selection',
- 'Products'),
- 'background_image': pgettext_lazy(
- 'Products selection',
- 'Background Image'),
- **SEO_LABELS}
+ labels = merge_dicts(
+ {
+ 'name': pgettext_lazy('Item name', 'Name'),
+ 'products': pgettext_lazy('Products selection', 'Products'),
+ 'background_image': pgettext_lazy(
+ 'Products selection',
+ 'Background Image')
+ },
+ SEO_LABELS)
widgets = SEO_WIDGETS
help_texts = SEO_HELP_TEXTS
diff --git a/saleor/dashboard/page/forms.py b/saleor/dashboard/page/forms.py
index d92c2b2d6a..3b1da816ae 100644
--- a/saleor/dashboard/page/forms.py
+++ b/saleor/dashboard/page/forms.py
@@ -1,6 +1,7 @@
from django import forms
from django.utils.translation import pgettext_lazy
+from ...core.utils import merge_dicts
from ...page.models import Page
from ..product.forms import RichTextField
from ..seo.utils import (
@@ -23,18 +24,22 @@ class PageForm(forms.ModelForm):
class Meta:
model = Page
exclude = []
- widgets = {
- 'slug': forms.TextInput(attrs={'placeholder': 'example-slug'}),
- **SEO_WIDGETS}
- labels = {
- 'is_visible': pgettext_lazy(
- 'Visibility status indicator', 'Publish'),
- **SEO_LABELS}
- help_texts = {
- 'slug': pgettext_lazy(
- 'Form field help text',
- 'Slug is being used to create page URL'),
- **SEO_HELP_TEXTS}
+ widgets = merge_dicts(
+ {'slug': forms.TextInput(attrs={'placeholder': 'example-slug'})},
+ SEO_WIDGETS)
+ labels = merge_dicts(
+ {
+ 'is_visible': pgettext_lazy(
+ 'Visibility status indicator', 'Publish')
+ },
+ SEO_LABELS)
+ help_texts = merge_dicts(
+ {
+ 'slug': pgettext_lazy(
+ 'Form field help text',
+ 'Slug is being used to create page URL')
+ },
+ SEO_HELP_TEXTS)
content = RichTextField()
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index fb6e600da8..f5ed1a26d1 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -10,6 +10,7 @@ from django.utils.translation import pgettext_lazy
from mptt.forms import TreeNodeChoiceField
from . import ProductBulkAction
+from ...core.utils import merge_dicts
from ...product.models import (
AttributeChoiceValue, Category, Collection, Product, ProductAttribute,
ProductImage, ProductType, ProductVariant, Stock, StockLocation,
@@ -189,22 +190,25 @@ class ProductForm(forms.ModelForm, AttributesMixin):
class Meta:
model = Product
exclude = ['attributes', 'product_type', 'updated_at']
- labels = {
- 'name': pgettext_lazy('Item name', 'Name'),
- 'description': pgettext_lazy('Description', 'Description'),
- 'seo_description': pgettext_lazy(
- 'A SEO friendly description', 'SEO Friendly Description'),
- 'category': pgettext_lazy('Category', 'Category'),
- 'price': pgettext_lazy('Currency amount', 'Price'),
- 'available_on': pgettext_lazy(
- 'Availability date', 'Availability date'),
- 'is_published': pgettext_lazy(
- 'Product published toggle', 'Published'),
- 'is_featured': pgettext_lazy(
- 'Featured product toggle', 'Feature this product on homepage'),
- 'collections': pgettext_lazy(
- 'Add to collection select', 'Collections'),
- **SEO_LABELS}
+ labels = merge_dicts(
+ {
+ 'name': pgettext_lazy('Item name', 'Name'),
+ 'description': pgettext_lazy('Description', 'Description'),
+ 'seo_description': pgettext_lazy(
+ 'A SEO friendly description', 'SEO Friendly Description'),
+ 'category': pgettext_lazy('Category', 'Category'),
+ 'price': pgettext_lazy('Currency amount', 'Price'),
+ 'available_on': pgettext_lazy(
+ 'Availability date', 'Availability date'),
+ 'is_published': pgettext_lazy(
+ 'Product published toggle', 'Published'),
+ 'is_featured': pgettext_lazy(
+ 'Featured product toggle',
+ 'Feature this product on homepage'),
+ 'collections': pgettext_lazy(
+ 'Add to collection select', 'Collections'),
+ },
+ SEO_LABELS)
help_texts = SEO_HELP_TEXTS
widgets = SEO_WIDGETS
diff --git a/tests/test_core.py b/tests/test_core.py
index 3b4a9f6bfe..facc779b85 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -7,7 +7,7 @@ from prices import Money
from saleor.account.models import Address, User
from saleor.core.utils import (
Country, create_superuser, format_money, get_country_by_ip,
- get_currency_for_country, random_data)
+ get_currency_for_country, merge_dicts, random_data)
from saleor.core.utils.text import get_cleaner, strip_html
from saleor.discount.models import Sale, Voucher
from saleor.order.models import Order
@@ -184,3 +184,19 @@ def test_utils_strip_html():
'\t<b>World</b>')
text = strip_html(base_text, strip_whitespace=True)
assert text == 'Hello World'
+
+
+ 'first_dict, second_dict',
+ (
+ ({}, {}),
+ ({}, {'foo': 'bar'}),
+ ({'foo': 'bar'}, {'bar': 'foo'}), # Regular case
+ ({'foo': 'bar'}, {}),
+ ({'foo': 'bar'}, {'foo': 'bar'}))) # Merge two same dicts
+def test_merge_dicts(first_dict, second_dict):
+ result = merge_dicts(first_dict, second_dict)
+ expected_result = {}
+ expected_result.update(first_dict)
+ expected_result.update(second_dict)
+ assert result.items() == expected_result.items()