Parcourir la source

Allow customizing the logic to get the country for hinting

master
Markus Bertheau il y a 11 ans
Parent
révision
d1281ce19b
1 fichiers modifiés avec 14 ajouts et 9 suppressions
  1. 14
    9
      oscar/views/generic.py

+ 14
- 9
oscar/views/generic.py Voir le fichier

156
 
156
 
157
     phone_number = forms.CharField(max_length=32, required=False)
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
     def clean_phone_number(self):
171
     def clean_phone_number(self):
160
         number = self.cleaned_data['phone_number']
172
         number = self.cleaned_data['phone_number']
161
 
173
 
168
             phone_number = PhoneNumber.from_string(number)
180
             phone_number = PhoneNumber.from_string(number)
169
         except phonenumbers.NumberParseException:
181
         except phonenumbers.NumberParseException:
170
             # Try hinting with the shipping country
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
             if not country:
185
             if not country:
179
                 # There is no shipping country, not a valid international
186
                 # There is no shipping country, not a valid international
181
                 raise ValidationError(
188
                 raise ValidationError(
182
                     _(u'This is not a valid international phone format.'))
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
             # The PhoneNumber class does not allow specifying
192
             # The PhoneNumber class does not allow specifying
188
             # the region. So we drop down to the underlying phonenumbers
193
             # the region. So we drop down to the underlying phonenumbers
189
             # library, which luckily allows parsing into a PhoneNumber
194
             # library, which luckily allows parsing into a PhoneNumber

Chargement…
Annuler
Enregistrer