Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Keep country in checkout as model field
Mira Szewczyk5d359bc7b293
diff --git a/saleor/checkout/migrations/0022_checkout_country.py b/saleor/checkout/migrations/0022_checkout_country.pynew file mode 100644index 0000000000..e1b81e445e--- /dev/null+++ b/saleor/checkout/migrations/0022_checkout_country.py@@ -0,0 +1,17 @@+# Generated by Django 2.2.7 on 2019-12-19 09:38++import django_countries.fields+from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [("checkout", "0021_django_price_2")]++ operations = [+ migrations.AddField(+ model_name="checkout",+ name="country",+ field=django_countries.fields.CountryField(default="US", max_length=2),+ )+ ]diff --git a/saleor/checkout/models.py b/saleor/checkout/models.pyindex 6366b3f67a..1b39c6e9c0 100644--- a/saleor/checkout/models.py+++ b/saleor/checkout/models.py@@ -9,6 +9,7 @@ from django.core.validators import MinValueValidatorfrom django.db import modelsfrom django.utils.encoding import smart_strfrom django.utils.translation import pgettext_lazy+from django_countries.fields import Country, CountryFieldfrom django_prices.models import MoneyFieldfrom prices import Money@@ -78,6 +79,7 @@ class Checkout(ModelWithMetadata):max_length=settings.DEFAULT_CURRENCY_CODE_LENGTH,default=settings.DEFAULT_CURRENCY,)+ country = CountryField(default=settings.DEFAULT_COUNTRY)discount_amount = models.DecimalField(max_digits=settings.DEFAULT_MAX_DIGITS,@@ -143,23 +145,25 @@ class Checkout(ModelWithMetadata):payments = [payment for payment in self.payments.all() if payment.is_active]return max(payments, default=None, key=attrgetter("pk"))- def set_country(self, country_code: str, commit: bool = False, replace=False):+ def set_country(+ self, country_code: str, commit: bool = False, replace: bool = True+ ):+ """Set country for checkout."""if not replace and self.country is not None:return- self.store_private_meta("Shipping_info", "", {"country_code": country_code})+ self.country = Country(country_code)if commit:- self.save(update_fields=["private_meta"])+ self.save(update_fields=["country"])- @property- def country(self):+ def get_country(self):address = self.shipping_address or self.billing_address- meta_country = self.get_private_meta("Shipping_info", "").get("country_code")+ saved_country = self.countryif address is None or not address.country:- return meta_country+ return saved_country.codecountry_code = address.country.code- if not country_code == meta_country:- self.set_country(country_code, commit=True, replace=True)+ if not country_code == saved_country.code:+ self.set_country(country_code, commit=True)return country_codediff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 1d7eb8617b..1265bc15c8 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -120,7 +120,7 @@ def check_variant_in_stock()if new_quantity > 0 and check_quantity:- check_stock_quantity(variant, checkout.country, new_quantity)+ check_stock_quantity(variant, checkout.get_country(), new_quantity)return new_quantity, line@@ -618,7 +618,7 @@ def create_line_for_order(checkout_line: "CheckoutLine", discounts) -> OrderLinequantity = checkout_line.quantityvariant = checkout_line.variantproduct = variant.product- country = checkout_line.checkout.country+ country = checkout_line.checkout.get_country()check_stock_quantity(variant, country, quantity)product_name = str(product)@@ -747,7 +747,7 @@ def create_order(for line in order_lines: # type: OrderLinevariant = line.variantif variant and variant.track_inventory:- allocate_stock(variant, checkout.country, line.quantity)+ allocate_stock(variant, checkout.get_country(), line.quantity)# Add gift cards to the orderfor gift_card in checkout.gift_cards.select_for_update():diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex bd32dd1acd..512cdf6d15 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -337,7 +337,7 @@ class CheckoutLinesAdd(BaseMutation):variants = cls.get_nodes_or_error(variant_ids, "variant_id", ProductVariant)quantities = [line.get("quantity") for line in lines]- check_lines_quantity(variants, quantities, checkout.country)+ check_lines_quantity(variants, quantities, checkout.get_country())if variants and quantities:for variant, quantity in zip(variants, quantities):diff --git a/tests/api/test_bulk_delete.py b/tests/api/test_bulk_delete.pyindex b4cbc33a6b..fc9c46cd79 100644--- a/tests/api/test_bulk_delete.py+++ b/tests/api/test_bulk_delete.py@@ -201,6 +201,7 @@ def test_delete_categories_with_subcategories_and_products(staff_api_client,category_list,permission_manage_products,+ permission_manage_stocks,product,category,):@@ -225,7 +226,7 @@ def test_delete_categories_with_subcategories_and_products(response = staff_api_client.post_graphql(MUTATION_CATEGORY_BULK_DELETE,variables,- permissions=[permission_manage_products],+ permissions=[permission_manage_products, permission_manage_stocks],)content = get_graphql_content(response)