Repository: django-oscar/django-oscar

Issue:
Fix Phone number in template does not translate bug.
The Phone number in shipping_address.html template does not translate.


Discussion:
# [Codecov](https://codecov.io/gh/django-oscar/django-oscar/pull/2414?src=pr&el=h1) Report
> Merging [#2414](https://codecov.io/gh/django-oscar/django-oscar/pull/2414?src=pr&el=desc) into [master](https://codecov.io/gh/django-oscar/django-oscar/commit/6b873d0ef968a364dbf3fe88d106628f55f826ea?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.


```diff
@@           Coverage Diff           @@
##           master    #2414   +/-   ##
=======================================
  Coverage   83.52%   83.52%           
=======================================
  Files         326      326           
  Lines       15560    15560           
=======================================
  Hits        12996    12996           
  Misses       2564     2564
```

| [Impacted Files](https://codecov.io/gh/django-oscar/django-oscar/pull/2414?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/oscar/views/generic.py](https://codecov.io/gh/django-oscar/django-oscar/pull/2414?src=pr&el=tree#diff-c3JjL29zY2FyL3ZpZXdzL2dlbmVyaWMucHk=) | `75.42% <100%> (ø)` | :arrow_up: |


Resolving patch:
diff --git a/src/oscar/views/generic.py b/src/oscar/views/generic.py
--- a/src/oscar/views/generic.py
+++ b/src/oscar/views/generic.py
@@ -163,17 +163,21 @@ def __init__(self, *args, **kwargs):
         # number is entered. We add the field in the init since on Python 2
         # using forms.Form as base class results in errors when using this
         # class as mixin.
+        field_kwargs = {
+            'required': False,
+            'help_text': '',
+            'max_length': 32,
+            'label': _('Phone number'),
+        }
 
-        # If the model field already exists, copy some properties from it
-        try:
-            number_required = self.fields['phone_number'].required
-            number_help_text = self.fields['phone_number'].help_text
-        except KeyError:
-            number_required = False
-            number_help_text = ''
-
-        self.fields['phone_number'] = forms.CharField(
-            max_length=32, required=number_required, help_text=number_help_text)
+        # If the model field already exists, copy existing properties from it
+        for key in field_kwargs:
+            try:
+                field_kwargs[key] = getattr(self.fields['phone_number'], key)
+            except (KeyError, AttributeError):
+                pass
+
+        self.fields['phone_number'] = forms.CharField(**field_kwargs)
 
     def get_country(self):
         # If the form data contains valid country information, we use that.


Test patch:
diff --git a/tests/integration/core/test_mixins.py b/tests/integration/core/test_mixins.py
new file mode 100644
--- /dev/null
+++ b/tests/integration/core/test_mixins.py
@@ -0,0 +1,32 @@
+from django import forms
+from django.test import TestCase
+
+from oscar.apps.address.models import UserAddress
+from oscar.views.generic import PhoneNumberMixin
+
+
+class PhoneNumberMixinTestCase(TestCase):
+
+    def test_mixin_adds_phone_number_field(self):
+
+        class TestForm(PhoneNumberMixin, forms.Form):
+            pass
+
+        form = TestForm()
+        self.assertIn('phone_number', form.fields)
+
+    def test_mixin_retains_existing_field_properties(self):
+
+        class TestForm(PhoneNumberMixin, forms.ModelForm):
+
+            class Meta:
+                model = UserAddress
+                fields = ['country', 'phone_number']
+                # Override default label and help text
+                labels = {"phone_number": "Special number"}
+                help_texts = {"phone_number": "Special help text"}
+
+        form = TestForm()
+        field = form.fields['phone_number']
+        self.assertEqual(field.label, "Special number")
+        self.assertEqual(field.help_text, "Special help text")