Repository: saleor/saleor

Issue:
Save language the order was placed in
Multilanguage sites will need a way to determine the language to send emails in. It's better to follow whatever language the user chose instead of trying to figure it out based on the country.



Discussion:

Resolving patch:
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -6,6 +6,7 @@
 from django.db import transaction
 from django.forms.models import model_to_dict
 from django.utils.encoding import smart_text
+from django.utils.translation import get_language
 from prices import FixedDiscount, Price
 
 from ..cart.models import Cart
@@ -211,8 +212,9 @@ def is_shipping_same_as_billing(self):
     def _add_to_user_address_book(self, address, is_billing=False,
                                   is_shipping=False):
         if self.user.is_authenticated():
-            store_user_address(self.user, address, shipping=is_shipping,
-                               billing=is_billing)
+            store_user_address(
+                self.user, address, shipping=is_shipping,
+                billing=is_billing)
 
     def _get_address_copy(self, address):
         address.user = None
@@ -245,6 +247,7 @@ def create_order(self):
             self.billing_address, is_billing=True)
 
         order_data = {
+            'language_code': get_language(),
             'billing_address': billing_address,
             'shipping_address': shipping_address,
             'tracking_client_id': self.tracking_code,
diff --git a/saleor/order/migrations/0016_order_language_code.py b/saleor/order/migrations/0016_order_language_code.py
new file mode 100644
--- /dev/null
+++ b/saleor/order/migrations/0016_order_language_code.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.5 on 2017-02-14 16:24
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('order', '0015_auto_20170206_0407'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='order',
+            name='language_code',
+            field=models.CharField(default='en-us', max_length=35),
+        ),
+    ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -59,6 +59,7 @@ class Order(models.Model, ItemSet, index.Indexed):
     user = models.ForeignKey(
         settings.AUTH_USER_MODEL, blank=True, null=True, related_name='orders',
         verbose_name=pgettext_lazy('Order field', 'user'))
+    language_code = models.CharField(max_length=35, default=settings.LANGUAGE_CODE)
     tracking_client_id = models.CharField(
         pgettext_lazy('Order field', 'tracking client id'),
         max_length=36, blank=True, editable=False)


Test patch:
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -1,7 +1,5 @@
 from django.core.urlresolvers import reverse
 
-from saleor.order.models import Order
-from saleor.userprofile.models import User
 from tests.utils import get_redirect_location
 
 
@@ -172,7 +170,7 @@ def test_summary_without_shipping_method(request_cart_with_item, client, monkeyp
     user tries to get summary step without saved shipping method -
      if is redirected to shipping method step
     """
-    #address test return true
+    # address test return true
     monkeypatch.setattr('saleor.checkout.core.Checkout.email',
                         True)
 
@@ -306,3 +304,43 @@ def test_remove_voucher(client, request_cart_with_item, shipping_method, voucher
                                    follow=True, HTTP_REFERER=url)
     assert voucher_response.status_code == 200
     assert voucher_response.context['checkout'].voucher_code is None
+
+
+def test_language_is_saved_in_order(authorized_client, billing_address, customer_user,  # pylint: disable=R0913, R0914
+                                    request_cart_with_item, settings, shipping_method):
+    """
+    authorized user change own email after checkout - if is not changed in order
+    """
+    # Prepare some data
+    user_language = 'fr'
+    settings.LANGUAGE_CODE = 'en'
+    customer_user.addresses.add(billing_address)
+    request_cart_with_item.user = customer_user
+    request_cart_with_item.save()
+
+    # Enter checkout
+    # Checkout index redirects directly to shipping address step
+    shipping_address = authorized_client.get(reverse('checkout:index'),
+                                             follow=True, HTTP_ACCEPT_LANGUAGE=user_language)
+
+    # Enter shipping address data
+    shipping_data = {'address': billing_address.pk}
+    shipping_method_page = authorized_client.post(shipping_address.request['PATH_INFO'],
+                                                  data=shipping_data, follow=True,
+                                                  HTTP_ACCEPT_LANGUAGE=user_language)
+
+    # Select shipping method
+    shipping_method_data = {'method': shipping_method.pk}
+    shipping_method_response = authorized_client.post(shipping_method_page.request['PATH_INFO'],
+                                                      data=shipping_method_data, follow=True,
+                                                      HTTP_ACCEPT_LANGUAGE=user_language)
+
+    # Summary page asks for Billing address, default is the same as shipping
+    payment_method_data = {'address': 'shipping_address'}
+    payment_method_page = authorized_client.post(shipping_method_response.request['PATH_INFO'],
+                                                 data=payment_method_data, follow=True,
+                                                 HTTP_ACCEPT_LANGUAGE=user_language)
+
+    # After summary step, order is created and it waits for payment
+    order = payment_method_page.context['order']
+    assert order.language_code == user_language