Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor translation resolver
fowczarek75f65f5be216
diff --git a/saleor/graphql/page/resolvers.py b/saleor/graphql/page/resolvers.pyindex b4d3ca47ee..c3112454ef 100644--- a/saleor/graphql/page/resolvers.py+++ b/saleor/graphql/page/resolvers.py@@ -6,14 +6,14 @@ from ..utils import filter_by_query_paramPAGE_SEARCH_FIELDS = ("content", "slug", "title")-def resolve_page(info, page_global_id=None, slug=None):- assert page_global_id or slug, "No page ID or slug provided."+def resolve_page(info, global_page_id=None, slug=None):+ assert global_page_id or slug, "No page ID or slug provided."user = info.context.userif slug is not None:page = models.Page.objects.visible_to_user(user).filter(slug=slug).first()else:- _type, page_pk = graphene.Node.from_global_id(page_global_id)+ _type, page_pk = graphene.Node.from_global_id(global_page_id)page = models.Page.objects.visible_to_user(user).filter(pk=page_pk).first()return pagediff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex fda1e78f57..7a8d1c4e06 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -322,7 +322,7 @@ input AttributeSortingInput {direction: OrderDirection!}-type AttributeStrings implements Node {+type AttributeTranslatableContent implements Node {id: ID!name: String!translation(languageCode: LanguageCodeEnum!): AttributeTranslation@@ -421,7 +421,7 @@ input AttributeValueInput {values: [String]!}-type AttributeValueStrings implements Node {+type AttributeValueTranslatableContent implements Node {id: ID!name: String!translation(languageCode: LanguageCodeEnum!): AttributeValueTranslation@@ -570,7 +570,7 @@ input CategoryInput {backgroundImageAlt: String}-type CategoryStrings implements Node {+type CategoryTranslatableContent implements Node {seoTitle: StringseoDescription: Stringid: ID!@@ -950,7 +950,7 @@ type CollectionReorderProducts {productErrors: [ProductError!]}-type CollectionStrings implements Node {+type CollectionTranslatableContent implements Node {seoTitle: StringseoDescription: Stringid: ID!@@ -1398,7 +1398,7 @@ input DateTimeRangeInput {scalar Decimal-union DefaultTranslationItem = ProductStrings | CollectionStrings | CategoryStrings | AttributeStrings | AttributeValueStrings | ProductVariantStrings | PageStrings | ShippingMethodStrings | SaleStrings | VoucherStrings | MenuItemStrings+union DefaultTranslationItem = ProductTranslatableContent | CollectionTranslatableContent | CategoryTranslatableContent | AttributeTranslatableContent | AttributeValueTranslatableContent | ProductVariantTranslatableContent | PageTranslatableContent | ShippingMethodTranslatableContent | SaleTranslatableContent | VoucherTranslatableContent | MenuItemTranslatableContenttype DigitalContent implements Node {useDefaultSettings: Boolean!@@ -1990,7 +1990,7 @@ input MenuItemMoveInput {sortOrder: Int}-type MenuItemStrings implements Node {+type MenuItemTranslatableContent implements Node {id: ID!name: String!translation(languageCode: LanguageCodeEnum!): MenuItemTranslation@@ -2696,7 +2696,7 @@ input PageInput {seo: SeoInput}-type PageStrings implements Node {+type PageTranslatableContent implements Node {seoTitle: StringseoDescription: Stringid: ID!@@ -2709,7 +2709,7 @@ type PageStrings implements Node {type PageTranslate {errors: [Error!]- page: PageStrings+ page: PageTranslatableContent}type PageTranslation implements Node {@@ -3138,7 +3138,7 @@ type ProductPricingInfo {priceRangeLocalCurrency: TaxedMoneyRange}-type ProductStrings implements Node {+type ProductTranslatableContent implements Node {id: ID!seoTitle: StringseoDescription: String@@ -3397,7 +3397,7 @@ input ProductVariantInput {weight: WeightScalar}-type ProductVariantStrings implements Node {+type ProductVariantTranslatableContent implements Node {id: ID!name: String!translation(languageCode: LanguageCodeEnum!): ProductVariantTranslation@@ -3590,7 +3590,7 @@ type SaleRemoveCatalogues {sale: Sale}-type SaleStrings implements Node {+type SaleTranslatableContent implements Node {id: ID!name: String!translation(languageCode: LanguageCodeEnum!): SaleTranslation@@ -3752,7 +3752,7 @@ type ShippingMethod implements Node {translation(languageCode: LanguageCodeEnum!): ShippingMethodTranslation}-type ShippingMethodStrings implements Node {+type ShippingMethodTranslatableContent implements Node {id: ID!name: String!translation(languageCode: LanguageCodeEnum!): ShippingMethodTranslation@@ -4369,7 +4369,7 @@ type VoucherRemoveCatalogues {voucher: Voucher}-type VoucherStrings implements Node {+type VoucherTranslatableContent implements Node {id: ID!name: Stringtranslation(languageCode: LanguageCodeEnum!): VoucherTranslationdiff --git a/saleor/graphql/translations/schema.py b/saleor/graphql/translations/schema.pyindex 06e961fd3a..2382b8031e 100644--- a/saleor/graphql/translations/schema.py+++ b/saleor/graphql/translations/schema.py@@ -51,21 +51,22 @@ class TranslatableItem(graphene.Union):)-# TODO Consider name of this class, maby we should replace TranslatableItem+# TODO Consider name of this class, we should replace to TranslatableItem after+# `translations` query refactor. Issue #4957class DefaultTranslationItem(graphene.Union):class Meta:types = (- translation_types.ProductStrings,- translation_types.CollectionStrings,- translation_types.CategoryStrings,- translation_types.AttributeStrings,- translation_types.AttributeValueStrings,- translation_types.ProductVariantStrings,- translation_types.PageStrings,- translation_types.ShippingMethodStrings,- translation_types.SaleStrings,- translation_types.VoucherStrings,- translation_types.MenuItemStrings,+ translation_types.ProductTranslatableContent,+ translation_types.CollectionTranslatableContent,+ translation_types.CategoryTranslatableContent,+ translation_types.AttributeTranslatableContent,+ translation_types.AttributeValueTranslatableContent,+ translation_types.ProductVariantTranslatableContent,+ translation_types.PageTranslatableContent,+ translation_types.ShippingMethodTranslatableContent,+ translation_types.SaleTranslatableContent,+ translation_types.VoucherTranslatableContent,+ translation_types.MenuItemTranslatableContent,)@@ -89,7 +90,7 @@ class TranslatableKinds(graphene.Enum):class TranslationQueries(graphene.ObjectType):- # TODO We nead to change output of this query to new types+ # TODO We nead to change output of this query to new types. Issue #4957translations = BaseConnectionField(TranslatableItemConnection,description="Returns a list of all translatable items of a given kind.",@@ -133,30 +134,22 @@ class TranslationQueries(graphene.ObjectType):@permission_required("site.manage_translations")def resolve_translation(self, info, id, kind, **_kwargs):+ # Disable all the no-member violations in this function+ # pylint: disable=no-member_type, kind_id = graphene.Node.from_global_id(id)if not _type == kind:return None- if kind == TranslatableKinds.PRODUCT:- return Product.objects.filter(pk=kind_id).first()- elif kind == TranslatableKinds.COLLECTION:- return Collection.objects.filter(pk=kind_id).first()- elif kind == TranslatableKinds.CATEGORY:- return Category.objects.filter(pk=kind_id).first()- elif kind == TranslatableKinds.ATTRIBUTE:- return Attribute.objects.filter(pk=kind_id).first()- elif kind == TranslatableKinds.ATTRIBUTE_VALUE:- return AttributeValue.objects.filter(pk=kind_id).first()- elif kind == TranslatableKinds.VARIANT:- return ProductVariant.objects.filter(pk=kind_id).first()- elif kind == TranslatableKinds.PAGE:- return Page.objects.filter(pk=kind_id).first()- # TODO No dashbord options to create translation create issue- elif kind == TranslatableKinds.SHIPPING_METHOD:- return ShippingMethod.objects.filter(pk=kind_id).first()- elif kind == TranslatableKinds.SALE:- return Sale.objects.filter(pk=kind_id).first()- elif kind == TranslatableKinds.VOUCHER:- return Voucher.objects.filter(pk=kind_id).first()- # TODO No dashbord options to create translation create issue- elif kind == TranslatableKinds.MENU_ITEM:- return MenuItem.objects.filter(pk=kind_id).first()+ models = {+ TranslatableKinds.PRODUCT.value: Product,+ TranslatableKinds.COLLECTION.value: Collection,+ TranslatableKinds.CATEGORY.value: Category,+ TranslatableKinds.ATTRIBUTE.value: Attribute,+ TranslatableKinds.ATTRIBUTE_VALUE.value: AttributeValue,+ TranslatableKinds.VARIANT.value: ProductVariant,+ TranslatableKinds.PAGE.value: Page,+ TranslatableKinds.SHIPPING_METHOD.value: ShippingMethod,+ TranslatableKinds.SALE.value: Sale,+ TranslatableKinds.VOUCHER.value: Voucher,+ TranslatableKinds.MENU_ITEM.value: MenuItem,+ }+ return models[kind].objects.filter(pk=kind_id).first()diff --git a/saleor/graphql/translations/types.py b/saleor/graphql/translations/types.pyindex 5cfba564e7..073aa20804 100644--- a/saleor/graphql/translations/types.py+++ b/saleor/graphql/translations/types.py@@ -45,7 +45,7 @@ class AttributeValueTranslation(BaseTranslationType):only_fields = ["id", "name"]-class AttributeValueStrings(CountableDjangoObjectType):+class AttributeValueTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(AttributeValueTranslation,language_code=graphene.Argument(@@ -79,7 +79,7 @@ class AttributeTranslation(BaseTranslationType):only_fields = ["id", "name"]-class AttributeStrings(CountableDjangoObjectType):+class AttributeTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(AttributeTranslation,language_code=graphene.Argument(@@ -113,7 +113,7 @@ class ProductVariantTranslation(BaseTranslationType):only_fields = ["id", "name"]-class ProductVariantStrings(CountableDjangoObjectType):+class ProductVariantTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(ProductVariantTranslation,language_code=graphene.Argument(@@ -161,7 +161,7 @@ class ProductTranslation(BaseTranslationType):]-class ProductStrings(CountableDjangoObjectType):+class ProductTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(ProductTranslation,language_code=graphene.Argument(@@ -211,7 +211,7 @@ class CollectionTranslation(BaseTranslationType):]-class CollectionStrings(CountableDjangoObjectType):+class CollectionTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(CollectionTranslation,language_code=graphene.Argument(@@ -263,7 +263,7 @@ class CategoryTranslation(BaseTranslationType):]-class CategoryStrings(CountableDjangoObjectType):+class CategoryTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(CategoryTranslation,language_code=graphene.Argument(@@ -309,7 +309,7 @@ class PageTranslation(BaseTranslationType):]-class PageStrings(CountableDjangoObjectType):+class PageTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(PageTranslation,language_code=graphene.Argument(@@ -355,7 +355,7 @@ class VoucherTranslation(BaseTranslationType):only_fields = ["id", "name"]-class VoucherStrings(CountableDjangoObjectType):+class VoucherTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(VoucherTranslation,language_code=graphene.Argument(@@ -368,7 +368,7 @@ class VoucherStrings(CountableDjangoObjectType):)voucher = graphene.Field("saleor.graphql.discount.types.Voucher",- # TODO consider move description to variable.+ # TODO consider move description to variable. Issue #4957description=("Vouchers allow giving discounts to particular customers on categories, ""collections or specific products. They can be used during checkout by "@@ -393,7 +393,7 @@ class SaleTranslation(BaseTranslationType):only_fields = ["id", "name"]-class SaleStrings(CountableDjangoObjectType):+class SaleTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(SaleTranslation,language_code=graphene.Argument(@@ -436,7 +436,7 @@ class MenuItemTranslation(BaseTranslationType):only_fields = ["id", "name"]-class MenuItemStrings(CountableDjangoObjectType):+class MenuItemTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(MenuItemTranslation,language_code=graphene.Argument(@@ -445,7 +445,7 @@ class MenuItemStrings(CountableDjangoObjectType):required=True,),description=(- "Returns translated Menu item fields " "for the given language code."+ "Returns translated Menu item fields for the given language code."),resolver=resolve_translation,)@@ -473,7 +473,7 @@ class ShippingMethodTranslation(BaseTranslationType):only_fields = ["id", "name"]-class ShippingMethodStrings(CountableDjangoObjectType):+class ShippingMethodTranslatableContent(CountableDjangoObjectType):translation = graphene.Field(ShippingMethodTranslation,language_code=graphene.Argument(diff --git a/tests/api/test_translations.py b/tests/api/test_translations.pyindex 024c10c7e7..373b6246c1 100644--- a/tests/api/test_translations.py+++ b/tests/api/test_translations.py@@ -1371,7 +1371,7 @@ QUERY_TRANSLATION_PRODUCT = """){translation(kind: $kind, id: $id){__typename- ...on ProductStrings{+ ...on ProductTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1433,7 +1433,7 @@ QUERY_TRANSLATION_COLLECTION = """){translation(kind: $kind, id: $id){__typename- ...on CollectionStrings{+ ...on CollectionTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1495,7 +1495,7 @@ QUERY_TRANSLATION_CATEGORY = """){translation(kind: $kind, id: $id){__typename- ...on CategoryStrings{+ ...on CategoryTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1539,7 +1539,7 @@ QUERY_TRANSLATION_ATTRIBUTE = """){translation(kind: $kind, id: $id){__typename- ...on AttributeStrings{+ ...on AttributeTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1584,7 +1584,7 @@ QUERY_TRANSLATION_ATTRIBUTE_VALUE = """){translation(kind: $kind, id: $id){__typename- ...on AttributeValueStrings{+ ...on AttributeValueTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1633,7 +1633,7 @@ QUERY_TRANSLATION_VARIANT = """){translation(kind: $kind, id: $id){__typename- ...on ProductVariantStrings{+ ...on ProductVariantTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1696,7 +1696,7 @@ QUERY_TRANSLATION_PAGE = """){translation(kind: $kind, id: $id){__typename- ...on PageStrings{+ ...on PageTranslatableContent{idtitletranslation(languageCode: $languageCode){@@ -1758,7 +1758,7 @@ QUERY_TRANSLATION_SHIPPING_METHOD = """){translation(kind: $kind, id: $id){__typename- ...on ShippingMethodStrings{+ ...on ShippingMethodTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1817,7 +1817,7 @@ QUERY_TRANSLATION_SALE = """){translation(kind: $kind, id: $id){__typename- ...on SaleStrings{+ ...on SaleTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1870,7 +1870,7 @@ QUERY_TRANSLATION_VOUCHER = """){translation(kind: $kind, id: $id){__typename- ...on VoucherStrings{+ ...on VoucherTranslatableContent{idnametranslation(languageCode: $languageCode){@@ -1923,7 +1923,7 @@ QUERY_TRANSLATION_MENU_ITEM = """){translation(kind: $kind, id: $id){__typename- ...on MenuItemStrings{+ ...on MenuItemTranslatableContent{idnametranslation(languageCode: $languageCode){