Explorar el Código

Allow customizing the logic to get the country for hinting

master
Markus Bertheau hace 11 años
padre
commit
d1281ce19b
Se han modificado 1 ficheros con 14 adiciones y 9 borrados
  1. 14
    9
      oscar/views/generic.py

+ 14
- 9
oscar/views/generic.py Ver fichero

@@ -156,6 +156,18 @@ class PhoneNumberMixin(object):
156 156
 
157 157
     phone_number = forms.CharField(max_length=32, required=False)
158 158
 
159
+    def get_country(self):
160
+        if hasattr(self.instance, 'country'):
161
+            return self.instance.country
162
+
163
+        if hasattr(self.fields.get('country'), 'queryset'):
164
+            return self.fields['country'].queryset[0]
165
+
166
+        return self.cleaned_data.get('country')
167
+
168
+    def get_region_code(self, country):
169
+        return country.iso_3166_1_a2
170
+
159 171
     def clean_phone_number(self):
160 172
         number = self.cleaned_data['phone_number']
161 173
 
@@ -168,12 +180,7 @@ class PhoneNumberMixin(object):
168 180
             phone_number = PhoneNumber.from_string(number)
169 181
         except phonenumbers.NumberParseException:
170 182
             # Try hinting with the shipping country
171
-            if hasattr(self.instance, 'country'):
172
-                country = self.instance.country
173
-            elif hasattr(self.fields.get('country'), 'queryset'):
174
-                country = self.fields['country'].queryset[0]
175
-            else:
176
-                country = None
183
+            country = self.get_country()
177 184
 
178 185
             if not country:
179 186
                 # There is no shipping country, not a valid international
@@ -181,9 +188,7 @@ class PhoneNumberMixin(object):
181 188
                 raise ValidationError(
182 189
                     _(u'This is not a valid international phone format.'))
183 190
 
184
-            country = self.cleaned_data.get('country', country)
185
-
186
-            region_code = country.iso_3166_1_a2
191
+            region_code = self.get_region_code(country)
187 192
             # The PhoneNumber class does not allow specifying
188 193
             # the region. So we drop down to the underlying phonenumbers
189 194
             # library, which luckily allows parsing into a PhoneNumber

Loading…
Cancelar
Guardar