Skip to content

Commit

Permalink
Merge pull request #4732 from mirumee/4622/Add_languagecode_parameter
Browse files Browse the repository at this point in the history
Add translations to countries in shop query
  • Loading branch information
maarcingebala committed Sep 11, 2019
2 parents 2a6f85b + 428b753 commit c42b24f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add redirectUrl to staff and user create mutations - #4717 by @fowczarek
- Filtering: use search in a consistent way. Add sort by product type name and publication status to products query. - #4715 by @fowczarek
- Migrated the old product attributes mapping to M2M - #4663 by @NyanKiyoshi
- Add translations to countries in shop query - #4732 by @fowczarek

## 2.8.0

Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3303,7 +3303,7 @@ type ShippingZoneUpdate {
type Shop {
geolocalization: Geolocalization
authorizationKeys: [AuthorizationKey]!
countries: [CountryDisplay]!
countries(languageCode: LanguageCodeEnum): [CountryDisplay]!
currencies: [String]!
defaultCurrency: String!
defaultCountry: CountryDisplay
Expand Down
20 changes: 13 additions & 7 deletions saleor/graphql/shop/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import graphene
from django.conf import settings
from django.utils import translation
from django_countries import countries
from django_prices_vatlayer.models import VAT
from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
Expand Down Expand Up @@ -71,6 +72,10 @@ class Shop(graphene.ObjectType):
)
countries = graphene.List(
CountryDisplay,
language_code=graphene.Argument(
LanguageCodeEnum,
description="A language code to return the translation for.",
),
description="List of countries available in the shop.",
required=True,
)
Expand Down Expand Up @@ -149,14 +154,15 @@ def resolve_authorization_keys(_, _info):
return site_models.AuthorizationKey.objects.all()

@staticmethod
def resolve_countries(_, _info):
def resolve_countries(_, _info, language_code=None):
taxes = {vat.country_code: vat for vat in VAT.objects.all()}
return [
CountryDisplay(
code=country[0], country=country[1], vat=taxes.get(country[0])
)
for country in countries
]
with translation.override(language_code):
return [
CountryDisplay(
code=country[0], country=country[1], vat=taxes.get(country[0])
)
for country in countries
]

@staticmethod
def resolve_currencies(_, _info):
Expand Down
35 changes: 30 additions & 5 deletions tests/api/test_shop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import graphene
import pytest
from django_countries import countries

from saleor.core.permissions import MODELS_PERMISSIONS
Expand Down Expand Up @@ -30,21 +31,45 @@ def test_query_authorization_keys(
assert data["authorizationKeys"][0]["key"] == authorization_key.key


def test_query_countries(user_api_client):
query = """
COUNTRIES_QUERY = """
query {
shop {
countries {
countries%(attributes)s {
code
country
}
}
}
"""
response = user_api_client.post_graphql(query)
"""


def test_query_countries(user_api_client):
response = user_api_client.post_graphql(COUNTRIES_QUERY % {"attributes": ""})
content = get_graphql_content(response)
data = content["data"]["shop"]
assert len(data["countries"]) == len(countries)


@pytest.mark.parametrize(
"language_code, expected_value",
(
("", "Afghanistan"),
("(languageCode: EN)", "Afghanistan"),
("(languageCode: PL)", "Afganistan"),
("(languageCode: DE)", "Afghanistan"),
),
)
def test_query_countries_with_translation(
language_code, expected_value, user_api_client
):
response = user_api_client.post_graphql(
COUNTRIES_QUERY % {"attributes": language_code}
)
content = get_graphql_content(response)
data = content["data"]["shop"]
assert len(data["countries"]) == len(countries)
assert data["countries"][0]["code"] == "AF"
assert data["countries"][0]["country"] == expected_value


def test_query_currencies(user_api_client, settings):
Expand Down

0 comments on commit c42b24f

Please sign in to comment.