Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use enum for reduced tax goods types
Artur Smętd01aeae922eb
diff --git a/saleor/core/__init__.py b/saleor/core/__init__.pyindex a26d54fed4..d8fc936ac9 100644--- a/saleor/core/__init__.py+++ b/saleor/core/__init__.py@@ -1,3 +1,4 @@+from enum import Enumfrom django.conf import settingsfrom django.core.checks import register, Warning@@ -20,3 +21,31 @@ def check_session_caching(app_configs, **kwargs): # pragma: no cover' like Redis.','saleor.W001'))return errors+++class TaxRateType(Enum):+ ACCOMODATION = 'accomodation'+ ADMISSION_TO_CULTURAL_EVENTS = 'admission to cultural events'+ ADMISSION_TO_ENTERAINMENT_EVENTS = 'admission to entertainment events'+ ADMISSION_TO_SPORTING_EVENTS = 'admission to sporting events'+ ADVERTISING = 'advertising'+ AGRICULTURAL_SUPPLIES = 'agricultural supplies'+ BABY_FOODSTUFFS = 'baby foodstuffs'+ BIKES = 'bikes'+ BOOKS = 'books'+ CHILDRENDS_CLOTHING = 'childrens clothing'+ DOMESTIC_FUEL = 'domestic fuel'+ DOMESTIC_SERVICES = 'domestic services'+ E_BOOKS = 'e-books'+ FOODSTUFFS = 'foodstuffs'+ HOTELS = 'hotels'+ MEDICAL = 'medical'+ NEWSPAPERS = 'newspapers'+ PASSENGER_TRANSPORT = 'passenger transport'+ PHARMACEUTICALS = 'pharmaceuticals'+ PROPERTY_RENOVATIONS = 'property renovations'+ RESTAURANTS = 'restaurants'+ SOCIAL_HOUSING = 'social housing'+ STANDARD = 'standard'+ WATER = 'water'+ WINE = 'wine'diff --git a/saleor/core/i18n.py b/saleor/core/i18n.pyindex 045359d95b..02174148f6 100644--- a/saleor/core/i18n.py+++ b/saleor/core/i18n.py@@ -1,40 +1,52 @@from django.utils.translation import pgettext_lazyfrom django_countries import countries+from . import TaxRateType+ANY_COUNTRY = ''ANY_COUNTRY_DISPLAY = pgettext_lazy('Country choice', 'Any Country')COUNTRY_CODE_CHOICES = [(ANY_COUNTRY, ANY_COUNTRY_DISPLAY)] + list(countries)VAT_RATE_TYPE_TRANSLATIONS = {- 'accommodation': pgettext_lazy('VAT rate type', 'accommodation'),- 'admission to cultural events': pgettext_lazy(+ TaxRateType.ACCOMODATION: pgettext_lazy(+ 'VAT rate type', 'accommodation'),+ TaxRateType.ADMISSION_TO_CULTURAL_EVENTS: pgettext_lazy('VAT rate type', 'admission to cultural events'),- 'admission to entertainment events': pgettext_lazy(+ TaxRateType.ADMISSION_TO_ENTERAINMENT_EVENTS: pgettext_lazy('VAT rate type', 'admission to entertainment events'),- 'admission to sporting events': pgettext_lazy(+ TaxRateType.ADMISSION_TO_SPORTING_EVENTS: pgettext_lazy('VAT rate type', 'admission to sporting events'),- 'advertising': pgettext_lazy(+ TaxRateType.ADVERTISING: pgettext_lazy('VAT rate type', 'advertising'),- 'agricultural supplies': pgettext_lazy(+ TaxRateType.AGRICULTURAL_SUPPLIES: pgettext_lazy('VAT rate type', 'agricultural supplies'),- 'baby foodstuffs': pgettext_lazy('VAT rate type', 'baby foodstuffs'),- 'bikes': pgettext_lazy('VAT rate type', 'bikes'),- 'books': pgettext_lazy('VAT rate type', 'books'),- 'childrens clothing': pgettext_lazy('VAT rate type', 'childrens clothing'),- 'domestic fuel': pgettext_lazy('VAT rate type', 'domestic fuel'),- 'domestic services': pgettext_lazy('VAT rate type', 'domestic services'),- 'e-books': pgettext_lazy('VAT rate type', 'e-books'),- 'foodstuffs': pgettext_lazy('VAT rate type', 'foodstuffs'),- 'hotels': pgettext_lazy('VAT rate type', 'hotels'),- 'medical': pgettext_lazy('VAT rate type', 'medical'),- 'newspapers': pgettext_lazy('VAT rate type', 'newspapers'),- 'passenger transport': pgettext_lazy(+ TaxRateType.BABY_FOODSTUFFS: pgettext_lazy(+ 'VAT rate type', 'baby foodstuffs'),+ TaxRateType.BIKES: pgettext_lazy('VAT rate type', 'bikes'),+ TaxRateType.BOOKS: pgettext_lazy('VAT rate type', 'books'),+ TaxRateType.CHILDRENDS_CLOTHING: pgettext_lazy(+ 'VAT rate type', 'childrens clothing'),+ TaxRateType.DOMESTIC_FUEL: pgettext_lazy(+ 'VAT rate type', 'domestic fuel'),+ TaxRateType.DOMESTIC_SERVICES: pgettext_lazy(+ 'VAT rate type', 'domestic services'),+ TaxRateType.E_BOOKS: pgettext_lazy('VAT rate type', 'e-books'),+ TaxRateType.FOODSTUFFS: pgettext_lazy(+ 'VAT rate type', 'foodstuffs'),+ TaxRateType.HOTELS: pgettext_lazy('VAT rate type', 'hotels'),+ TaxRateType.MEDICAL: pgettext_lazy('VAT rate type', 'medical'),+ TaxRateType.NEWSPAPERS: pgettext_lazy(+ 'VAT rate type', 'newspapers'),+ TaxRateType.PASSENGER_TRANSPORT: pgettext_lazy('VAT rate type', 'passenger transport'),- 'pharmaceuticals': pgettext_lazy('VAT rate type', 'pharmaceuticals'),- 'property renovations': pgettext_lazy(+ TaxRateType.PHARMACEUTICALS: pgettext_lazy(+ 'VAT rate type', 'pharmaceuticals'),+ TaxRateType.PROPERTY_RENOVATIONS: pgettext_lazy('VAT rate type', 'property renovations'),- 'restaurants': pgettext_lazy('VAT rate type', 'restaurants'),- 'social housing': pgettext_lazy('VAT rate type', 'social housing'),- 'standard': pgettext_lazy('VAT rate type', 'standard'),- 'water': pgettext_lazy('VAT rate type', 'water'),- 'wine': pgettext_lazy('VAT rate type', 'wine')}+ TaxRateType.RESTAURANTS: pgettext_lazy(+ 'VAT rate type', 'restaurants'),+ TaxRateType.SOCIAL_HOUSING: pgettext_lazy(+ 'VAT rate type', 'social housing'),+ TaxRateType.STANDARD: pgettext_lazy('VAT rate type', 'standard'),+ TaxRateType.WATER: pgettext_lazy('VAT rate type', 'water'),+ TaxRateType.WINE: pgettext_lazy('VAT rate type', 'wine')}diff --git a/saleor/core/utils/taxes.py b/saleor/core/utils/taxes.pyindex f9d83f253f..43e1461cb2 100644--- a/saleor/core/utils/taxes.py+++ b/saleor/core/utils/taxes.py@@ -5,7 +5,9 @@ from django_prices_vatlayer.utils import (get_tax_for_rate, get_tax_rates_for_country)from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange-DEFAULT_TAX_RATE_NAME = 'standard'+from ...core import TaxRateType++DEFAULT_TAX_RATE_NAME = TaxRateType.STANDARD.valueZERO_MONEY = Money(0, settings.DEFAULT_CURRENCY)ZERO_TAXED_MONEY = TaxedMoney(net=ZERO_MONEY, gross=ZERO_MONEY)diff --git a/saleor/graphql/core/types/money.py b/saleor/graphql/core/types/money.pyindex 3d1578f0d6..412e916e01 100644--- a/saleor/graphql/core/types/money.py+++ b/saleor/graphql/core/types/money.py@@ -1,6 +1,8 @@import graphenefrom django_prices.templatetags import prices_i18n+from ....core import TaxRateType as TaxRateTypeEnum+class Money(graphene.ObjectType):currency = graphene.String(description='Currency code.', required=True)@@ -25,6 +27,8 @@ class MoneyRange(graphene.ObjectType):class Meta:description = 'Represents a range of amounts of money.'+TaxRateType = graphene.Enum.from_enum(TaxRateTypeEnum)+class TaxedMoney(graphene.ObjectType):currency = graphene.String(description='Currency code.', required=True)@@ -75,7 +79,7 @@ class VAT(graphene.ObjectType):class ReducedRate(graphene.ObjectType):rate = graphene.Float(description='Reduced VAT rate in percent.', required=True)- rate_type = graphene.String(description='A type of goods.', required=True)+ rate_type = TaxRateType(description='A type of goods.', required=True)class Meta:description = '''diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex cfd10c9cfb..4e2c0cd138 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -7,6 +7,7 @@ from ....product import modelsfrom ....product.utils.attributes import get_name_from_attributesfrom ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutationfrom ...core.types.common import Decimal, SeoInput+from ...core.types.money import TaxRateTypefrom ...core.utils import clean_seo_fieldsfrom ...file_upload.types import Uploadfrom ...shipping.types import WeightScalar@@ -406,6 +407,7 @@ class ProductTypeInput(graphene.InputObjectType):description="""Determines if shipping is required for productsof this variant.""")weight = WeightScalar(description='Weight of the ProductType items.')+ tax_rate = TaxRateType(description='A type of goods.')class ProductTypeCreate(ModelMutation):diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.pyindex f7b9f059cf..969df339df 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types.py@@ -12,7 +12,8 @@ from ...product.utils.costs import (from ..core.decorators import permission_requiredfrom ..core.filters import DistinctFilterSetfrom ..core.types.common import CountableDjangoObjectType-from ..core.types.money import Money, MoneyRange, TaxedMoney, TaxedMoneyRange+from ..core.types.money import (+ Money, MoneyRange, TaxedMoney, TaxedMoneyRange, TaxRateType)from ..utils import get_database_idfrom .filters import ProductFilterSet@@ -201,6 +202,7 @@ class ProductType(CountableDjangoObjectType):Product,filterset_class=ProductFilterSet,description='List of products of this type.')+ tax_rate = TaxRateType(description='A type of tax rate.')class Meta:description = """Represents a type of product. It defines whatdiff --git a/saleor/graphql/shop/types.py b/saleor/graphql/shop/types.pyindex aa79cf7988..ae406b80be 100644--- a/saleor/graphql/shop/types.py+++ b/saleor/graphql/shop/types.py@@ -4,7 +4,6 @@ from django_countries import countriesfrom graphql_jwt.decorators import permission_requiredfrom phonenumbers import COUNTRY_CODE_TO_REGION_CODE-from ...core.i18n import VAT_RATE_TYPE_TRANSLATIONSfrom ...core.permissions import get_permissionsfrom ...site import models as site_modelsfrom ..core.types.common import (@@ -39,11 +38,6 @@ class Domain(graphene.ObjectType):description = 'Represents shop\'s domain.'-class TaxReducedRateGoodsDisplay(graphene.ObjectType):- code = graphene.String(description='Goods code name')- name = graphene.String(description='Translated name')--class Shop(graphene.ObjectType):authorization_keys = graphene.List(AuthorizationKey, description='List of configured authorization keys.',@@ -81,9 +75,6 @@ class Shop(graphene.ObjectType):track_inventory_by_default = graphene.Boolean(description='Enable inventory tracking')default_weight_unit = WeightUnitsEnum(description='Default weight unit')- tax_reduced_rate_goods = graphene.List(- TaxReducedRateGoodsDisplay,- description='List of all types of goods applicable for reduced tax rates.')class Meta:description = '''@@ -142,13 +133,6 @@ class Shop(graphene.ObjectType):def resolve_header_text(self, info):return info.context.site.settings.header_text- @permission_required('site.manage_settings')- def resolve_tax_reduced_rate_goods(self, info):- return [- TaxReducedRateGoodsDisplay(code=rate, name=name)- for rate, name in VAT_RATE_TYPE_TRANSLATIONS.items()]--def resolve_include_taxes_in_prices(self, info):return info.context.site.settings.include_taxes_in_pricesdiff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 07234a1c41..f779334ed9 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -582,6 +582,7 @@ def test_product_type_query(}}}+ taxRate}}"""@@ -604,12 +605,13 @@ def test_product_type_query(assert 'errors' not in contentdata = content['data']assert data['productType']['products']['totalCount'] == no_products-+ assert data['productType']['taxRate'] == product_type.tax_rate.upper()def test_product_type_create_mutation(admin_api_client, product_type):query = """mutation createProductType($name: String!,+ $taxRate: TaxRateType!,$hasVariants: Boolean!,$isShippingRequired: Boolean!,$productAttributes: [ID],@@ -617,12 +619,14 @@ def test_product_type_create_mutation(admin_api_client, product_type):productTypeCreate(input: {name: $name,+ taxRate: $taxRate,hasVariants: $hasVariants,isShippingRequired: $isShippingRequired,productAttributes: $productAttributes,variantAttributes: $variantAttributes}) {productType {name+ taxRateisShippingRequiredhasVariantsvariantAttributes {@@ -657,13 +661,16 @@ def test_product_type_create_mutation(admin_api_client, product_type):variables = json.dumps({'name': product_type_name, 'hasVariants': has_variants,+ 'taxRate': 'STANDARD','isShippingRequired': require_shipping,'productAttributes': product_attributes_ids,'variantAttributes': variant_attributes_ids})+ initial_count = ProductType.objects.count()response = admin_api_client.post(reverse('api'), {'query': query, 'variables': variables})content = get_graphql_content(response)assert 'errors' not in content+ assert ProductType.objects.count() == initial_count + 1data = content['data']['productTypeCreate']assert data['productType']['name'] == product_type_nameassert data['productType']['hasVariants'] == has_variants@@ -672,7 +679,8 @@ def test_product_type_create_mutation(admin_api_client, product_type):assert len(data['productType']['productAttributes']['edges']) == no_pano_va = variant_attributes.count()assert len(data['productType']['variantAttributes']['edges']) == no_va-+ new_instance = ProductType.objects.latest('pk')+ assert new_instance.tax_rate == 'standard'def test_product_type_update_mutation(admin_api_client, product_type):query = """