Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Unify warehouse address API (#7481)
Damian Wysocki5af29dadd000
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex df1a9d6f03..f03f675b5f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -262,6 +262,10 @@ All notable, unreleased changes to this project will be documented in this file.- Return empty results when filtering by non-existing attribute - #7025 by @maarcingebala- Add new authorization header `Authorization-Bearer` - #6998 by @korycins- Add field `paymentMethodType` to `Payment` object - #7073 by @korycins+- Unify Warehouse Address API - #7481 by @d-wysocki+ - deprecate `companyName` on `Warehouse` type+ - remove `companyName` on `WarehouseInput` type+ - remove `WarehouseAddressInput` on `WarehouseUpdateInput` and `WarehouseCreateInput`, and change it to `AddressInput`- Fix passing incorrect customer email to payment gateways - #7486 by @korycins# 2.11.1diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex e6e33dbb39..f058b5bc28 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -5915,23 +5915,12 @@ type Warehouse implements Node & ObjectWithMetadata {id: ID!name: String!slug: String!- companyName: String!shippingZones(before: String, after: String, first: Int, last: Int): ShippingZoneCountableConnection!address: Address!email: String!privateMetadata: [MetadataItem]!metadata: [MetadataItem]!-}--input WarehouseAddressInput {- streetAddress1: String!- streetAddress2: String- city: String!- cityArea: String- postalCode: String- country: CountryCode!- countryArea: String- phone: String+ companyName: String! @deprecated(reason: "Use address.CompanyName. This field will be removed in Saleor 4.0.")}type WarehouseCountableConnection {@@ -5953,10 +5942,9 @@ type WarehouseCreate {input WarehouseCreateInput {slug: String- companyName: Stringemail: Stringname: String!- address: WarehouseAddressInput!+ address: AddressInput!shippingZones: [ID]}@@ -6015,10 +6003,9 @@ type WarehouseUpdate {input WarehouseUpdateInput {slug: String- companyName: Stringemail: Stringname: String- address: WarehouseAddressInput+ address: AddressInput}type Webhook implements Node {diff --git a/saleor/graphql/warehouse/filters.py b/saleor/graphql/warehouse/filters.pyindex 6c615718fd..30a1d5dc93 100644--- a/saleor/graphql/warehouse/filters.py+++ b/saleor/graphql/warehouse/filters.py@@ -13,7 +13,7 @@ def prefech_qs_for_filter(qs):def filter_search_warehouse(qs, _, value):search_fields = ["name",- "company_name",+ "address__company_name","email","address__street_address_1","address__street_address_2",@@ -33,7 +33,7 @@ def filter_search_stock(qs, _, value):"product_variant__product__name","product_variant__name","warehouse__name",- "warehouse__company_name",+ "warehouse__address__company_name",]if value:qs = qs.select_related("product_variant", "warehouse").prefetch_related(diff --git a/saleor/graphql/warehouse/tests/test_warehouse.py b/saleor/graphql/warehouse/tests/test_warehouse.pyindex 0233205bd2..010b94425e 100644--- a/saleor/graphql/warehouse/tests/test_warehouse.py+++ b/saleor/graphql/warehouse/tests/test_warehouse.py@@ -509,13 +509,13 @@ def test_mutation_create_warehouse("input": {"name": "Test warehouse","slug": "test-warhouse",- "companyName": "Amazing Company Inc","email": "[email protected]","address": {"streetAddress1": "Teczowa 8","city": "Wroclaw","country": "PL","postalCode": "53-601",+ "companyName": "Amazing Company Inc",},"shippingZones": [graphene.Node.to_global_id("ShippingZone", shipping_zone.id)@@ -537,6 +537,7 @@ def test_mutation_create_warehouse()assert created_warehouse["name"] == warehouse.nameassert created_warehouse["slug"] == warehouse.slug+ assert created_warehouse["companyName"] == warehouse.address.company_namedef test_mutation_create_warehouse_does_not_create_when_name_is_empty_string(@@ -546,13 +547,13 @@ def test_mutation_create_warehouse_does_not_create_when_name_is_empty_string("input": {"name": " ","slug": "test-warhouse",- "companyName": "Amazing Company Inc","email": "[email protected]","address": {"streetAddress1": "Teczowa 8","city": "Wroclaw","country": "PL","postalCode": "53-601",+ "companyName": "Amazing Company Inc",},"shippingZones": [graphene.Node.to_global_id("ShippingZone", shipping_zone.id)@@ -580,12 +581,12 @@ def test_create_warehouse_creates_address(variables = {"input": {"name": "Test warehouse",- "companyName": "Amazing Company Inc","email": "[email protected]","address": {"streetAddress1": "Teczowa 8","city": "Wroclaw","country": "PL",+ "companyName": "Amazing Company Inc","postalCode": "53-601",},"shippingZones": [@@ -608,6 +609,7 @@ def test_create_warehouse_creates_address(warehouse_data = content["data"]["createWarehouse"]["warehouse"]assert warehouse_data["address"]["id"] == address_idassert address.street_address_1 == "Teczowa 8"+ assert address.company_name == "Amazing Company Inc"assert address.city == "WROCLAW"@@ -652,10 +654,9 @@ def test_mutation_update_warehouse(warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)warehouse_old_name = warehouse.namewarehouse_slug = warehouse.slug- warehouse_old_company_name = warehouse.company_namevariables = {"id": warehouse_id,- "input": {"name": "New name", "companyName": "New name for company"},+ "input": {"name": "New name"},}staff_api_client.post_graphql(MUTATION_UPDATE_WAREHOUSE,@@ -664,10 +665,8 @@ def test_mutation_update_warehouse()warehouse.refresh_from_db()assert not (warehouse.name == warehouse_old_name)- assert not (warehouse.company_name == warehouse_old_company_name)assert warehouse.name == "New name"assert warehouse.slug == warehouse_slug- assert warehouse.company_name == "New name for company"def test_mutation_update_warehouse_can_update_address(@@ -680,10 +679,10 @@ def test_mutation_update_warehouse_can_update_address("id": warehouse_id,"input": {"name": warehouse.name,- "companyName": "","address": {"streetAddress1": "Teczowa 8","streetAddress2": "Ground floor",+ "companyName": "","city": address.city,"country": address.country.code,"postalCode": "53-601",@@ -869,12 +868,12 @@ def test_shipping_zone_can_be_assigned_only_to_one_warehouse(variables = {"input": {"name": "Warehouse #q",- "companyName": "Big Company","email": "[email protected]","address": {"streetAddress1": "Teczowa 8","city": "Wroclaw","country": "PL",+ "companyName": "Big Company","postalCode": "53-601",},"shippingZones": [used_shipping_zone_id],diff --git a/saleor/graphql/warehouse/types.py b/saleor/graphql/warehouse/types.pyindex bdad63eef4..46648170bf 100644--- a/saleor/graphql/warehouse/types.py+++ b/saleor/graphql/warehouse/types.py@@ -5,34 +5,24 @@ from django.db.models.functions import Coalescefrom ...core.permissions import OrderPermissions, ProductPermissionsfrom ...core.tracing import traced_resolverfrom ...warehouse import models-from ..account.enums import CountryCodeEnum+from ..account.dataloaders import AddressByIdLoaderfrom ..channel import ChannelContextfrom ..core.connection import CountableDjangoObjectTypefrom ..decorators import one_of_permissions_requiredfrom ..meta.types import ObjectWithMetadata-class WarehouseAddressInput(graphene.InputObjectType):- street_address_1 = graphene.String(description="Address.", required=True)- street_address_2 = graphene.String(description="Address.")- city = graphene.String(description="City.", required=True)- city_area = graphene.String(description="District.")- postal_code = graphene.String(description="Postal code.")- country = CountryCodeEnum(description="Country.", required=True)- country_area = graphene.String(description="State or province.")- phone = graphene.String(description="Phone number.")--class WarehouseInput(graphene.InputObjectType):slug = graphene.String(description="Warehouse slug.")- company_name = graphene.String(description="Company name.")email = graphene.String(description="The email address of the warehouse.")class WarehouseCreateInput(WarehouseInput):name = graphene.String(description="Warehouse name.", required=True)- address = WarehouseAddressInput(- description="Address of the warehouse.", required=True+ address = graphene.Field(+ "saleor.graphql.account.types.AddressInput",+ description="Address of the warehouse.",+ required=True,)shipping_zones = graphene.List(graphene.ID, description="Shipping zones supported by the warehouse."@@ -41,12 +31,22 @@ class WarehouseCreateInput(WarehouseInput):class WarehouseUpdateInput(WarehouseInput):name = graphene.String(description="Warehouse name.", required=False)- address = WarehouseAddressInput(- description="Address of the warehouse.", required=False+ address = graphene.Field(+ "saleor.graphql.account.types.AddressInput",+ description="Address of the warehouse.",+ required=False,)class Warehouse(CountableDjangoObjectType):+ company_name = graphene.String(+ required=True,+ description="Warehouse company name.",+ deprecation_reason=(+ "Use address.CompanyName. This field will be removed in Saleor 4.0."+ ),+ )+class Meta:description = "Represents warehouse."model = models.Warehouse@@ -55,7 +55,6 @@ class Warehouse(CountableDjangoObjectType):"id","name","slug",- "company_name","shipping_zones","address","email",@@ -71,6 +70,17 @@ class Warehouse(CountableDjangoObjectType):]return shipping_zones+ @staticmethod+ def resolve_company_name(root, info, *_args, **_kwargs):+ def _resolve_company_name(address):+ return address.company_name++ return (+ AddressByIdLoader(info.context)+ .load(root.address_id)+ .then(_resolve_company_name)+ )+class Stock(CountableDjangoObjectType):quantity = graphene.Int(diff --git a/saleor/warehouse/migrations/0014_remove_warehouse_company_name.py b/saleor/warehouse/migrations/0014_remove_warehouse_company_name.pynew file mode 100644index 0000000000..9ec38f60a2--- /dev/null+++ b/saleor/warehouse/migrations/0014_remove_warehouse_company_name.py@@ -0,0 +1,31 @@+# Generated by Django 3.2.2 on 2021-06-14 07:42++from django.db import migrations+++def migrate_warehouse_address(apps, _schema_editor):+ Warehouse = apps.get_model("warehouse", "Warehouse")++ for warehouse in Warehouse.objects.filter(+ company_name__isnull=False, address__company_name=""+ ).iterator():+ address = warehouse.address+ address.company_name = warehouse.company_name+ address.save(update_fields=["company_name"])+++class Migration(migrations.Migration):++ dependencies = [+ ("warehouse", "0013_auto_20210308_1135"),+ ]++ operations = [+ migrations.RunPython(+ migrate_warehouse_address, reverse_code=migrations.RunPython.noop+ ),+ migrations.RemoveField(+ model_name="warehouse",+ name="company_name",+ ),+ ]diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.pyindex 269c6856a8..81b2e5d9da 100644--- a/saleor/warehouse/models.py+++ b/saleor/warehouse/models.py@@ -30,7 +30,6 @@ class Warehouse(ModelWithMetadata):id = models.UUIDField(default=uuid.uuid4, primary_key=True)name = models.CharField(max_length=250)slug = models.SlugField(max_length=255, unique=True, allow_unicode=True)- company_name = models.CharField(blank=True, max_length=255)shipping_zones = models.ManyToManyField(ShippingZone, blank=True, related_name="warehouses")