Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Introduce merge_dicts helper for Python3.4
Pacu2619741b88752
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.pyindex 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' % credentialsreturn 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 resultdiff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.pyindex b427e7da9d..6ad68972cd 100644--- a/saleor/dashboard/category/forms.py+++ b/saleor/dashboard/category/forms.py@@ -4,6 +4,7 @@ from django.utils.text import slugifyfrom django.utils.translation import pgettext_lazyfrom text_unidecode import unidecode+from ...core.utils import merge_dictsfrom ...product.models import Categoryfrom ..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 = Categoryexclude = ['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_TEXTSwidgets = SEO_WIDGETSdiff --git a/saleor/dashboard/collection/forms.py b/saleor/dashboard/collection/forms.pyindex e9eefea8cc..7d7c8cc6eb 100644--- a/saleor/dashboard/collection/forms.py+++ b/saleor/dashboard/collection/forms.py@@ -4,6 +4,7 @@ from django.utils.text import slugifyfrom django.utils.translation import pgettext_lazyfrom text_unidecode import unidecode+from ...core.utils import merge_dictsfrom ...product.models import Collection, Productfrom ..forms import AjaxSelect2MultipleChoiceFieldfrom ..seo.utils import (@@ -19,17 +20,15 @@ class CollectionForm(forms.ModelForm):class Meta:model = Collectionexclude = ['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_WIDGETShelp_texts = SEO_HELP_TEXTSdiff --git a/saleor/dashboard/page/forms.py b/saleor/dashboard/page/forms.pyindex d92c2b2d6a..3b1da816ae 100644--- a/saleor/dashboard/page/forms.py+++ b/saleor/dashboard/page/forms.py@@ -1,6 +1,7 @@from django import formsfrom django.utils.translation import pgettext_lazy+from ...core.utils import merge_dictsfrom ...page.models import Pagefrom ..product.forms import RichTextFieldfrom ..seo.utils import (@@ -23,18 +24,22 @@ class PageForm(forms.ModelForm):class Meta:model = Pageexclude = []- 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.pyindex 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_lazyfrom mptt.forms import TreeNodeChoiceFieldfrom . import ProductBulkAction+from ...core.utils import merge_dictsfrom ...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 = Productexclude = ['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_TEXTSwidgets = SEO_WIDGETSdiff --git a/tests/test_core.py b/tests/test_core.pyindex 3b4a9f6bfe..facc779b85 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -7,7 +7,7 @@ from prices import Moneyfrom saleor.account.models import Address, Userfrom 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_htmlfrom saleor.discount.models import Sale, Voucherfrom 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()