Esempio n. 1
0
 def test_valid_numbers_are_valid(self):
     numbers = [PhoneNumber.from_string(number_string)
                for number_string in equal_number_strings + [valid_number]]
     self.assertTrue(all([number.is_valid() for number in numbers]))
     numbers = [PhoneNumber.from_string(number_string, region=region)
                for region, number_string in local_numbers]
     self.assertTrue(all([number.is_valid() for number in numbers]))
Esempio n. 2
0
 def test_valid_numbers_are_valid(self):
     numbers = [PhoneNumber.from_string(number_string)
                for number_string in equal_number_strings + [valid_number]]
     self.assertTrue(all([number.is_valid() for number in numbers]))
     numbers = [PhoneNumber.from_string(number_string, region=region)
                for region, number_string in local_numbers]
     self.assertTrue(all([number.is_valid() for number in numbers]))
Esempio n. 3
0
    def clean_phone_number(self):
        number = self.cleaned_data["phone_number"]

        # empty
        if number in validators.EMPTY_VALUES:
            return None

        # Check for an international phone format
        try:
            phone_number = PhoneNumber.from_string(number)
        except phonenumbers.NumberParseException:
            # Try hinting with the shipping country
            country = self.get_country()

            if not country:
                # There is no shipping country, not a valid international
                # number
                raise ValidationError(_(u"This is not a valid international phone format."))

            region_code = self.get_region_code(country)
            # The PhoneNumber class does not allow specifying
            # the region. So we drop down to the underlying phonenumbers
            # library, which luckily allows parsing into a PhoneNumber
            # instance
            try:
                phone_number = PhoneNumber.from_string(number, region=region_code)
                if not phone_number.is_valid():
                    raise ValidationError(_(u"This is not a valid local phone format for %s.") % country)
            except phonenumbers.NumberParseException:
                # Not a valid local or international phone number
                raise ValidationError(_(u"This is not a valid local or international phone" u" format."))

        return phone_number
Esempio n. 4
0
    def clean_phone_number(self):
        number = self.cleaned_data['phone_number']

        # empty
        if number in validators.EMPTY_VALUES:
            return None

        # Check for an international phone format
        try:
            phone_number = PhoneNumber.from_string(number)
        except phonenumbers.NumberParseException:
            # Try hinting with the shipping country
            if hasattr(self.instance, 'country'):
                country = self.instance.country
            elif hasattr(self.fields.get('country'), 'queryset'):
                country = self.fields['country'].queryset[0]
            else:
                country = None

            if not country:
                # There is no shipping country, not a valid international
                # number
                raise ValidationError(
                    _(u'This is not a valid international phone format.'))

            country = self.cleaned_data.get('country', country)

            region_code = country.iso_3166_1_a2
            # The PhoneNumber class does not allow specifying
            # the region. So we drop down to the underlying phonenumbers
            # library, which luckily allows parsing into a PhoneNumber
            # instance
            try:
                phone_number = PhoneNumber.from_string(number,
                                                       region=region_code)
                if not phone_number.is_valid():
                    raise ValidationError(
                        _(u'This is not a valid local phone format for %s.') %
                        country)
            except phonenumbers.NumberParseException:
                # Not a valid local or international phone number
                raise ValidationError(
                    _(u'This is not a valid local or international phone'
                      u' format.'))

        return phone_number
Esempio n. 5
0
    def clean_phone_number(self):
        number = self.cleaned_data['phone_number']

        # empty
        if number in validators.EMPTY_VALUES:
            return None

        # Check for an international phone format
        try:
            phone_number = PhoneNumber.from_string(number)
        except phonenumbers.NumberParseException:
            # Try hinting with the shipping country
            if hasattr(self.instance, 'country'):
                country = self.instance.country
            elif hasattr(self.fields.get('country'), 'queryset'):
                country = self.fields['country'].queryset[0]
            else:
                country = None

            if not country:
                # There is no shipping country, not a valid international
                # number
                raise ValidationError(
                    _(u'This is not a valid international phone format.'))

            country = self.cleaned_data.get('country', country)

            region_code = country.iso_3166_1_a2
            # The PhoneNumber class does not allow specifying
            # the region. So we drop down to the underlying phonenumbers
            # library, which luckily allows parsing into a PhoneNumber
            # instance
            try:
                phone_number = PhoneNumber.from_string(number,
                                                       region=region_code)
                if not phone_number.is_valid():
                    raise ValidationError(
                        _(u'This is not a valid local phone format for %s.')
                        % country)
            except phonenumbers.NumberParseException:
                # Not a valid local or international phone number
                raise ValidationError(
                    _(u'This is not a valid local or international phone'
                      u' format.'))

        return phone_number
Esempio n. 6
0
    def clean_phone_number(self):
        number = self.cleaned_data['phone_number']

        # empty
        if number in validators.EMPTY_VALUES:
            return None

        # Check for an international phone format
        try:
            phone_number = PhoneNumber.from_string(number)
        except phonenumbers.NumberParseException:
            # Try hinting with the shipping country
            country = self.get_country()

            if not country:
                # There is no shipping country, not a valid international
                # number
                raise ValidationError(
                    _(u'This is not a valid international phone format.'))

            region_code = self.get_region_code(country)
            # The PhoneNumber class does not allow specifying
            # the region. So we drop down to the underlying phonenumbers
            # library, which luckily allows parsing into a PhoneNumber
            # instance
            try:
                phone_number = PhoneNumber.from_string(number,
                                                       region=region_code)
                if not phone_number.is_valid():
                    raise ValidationError(
                        _(u'This is not a valid local phone format for %s.') %
                        country)
            except phonenumbers.NumberParseException:
                # Not a valid local or international phone number
                raise ValidationError(
                    _(u'This is not a valid local or international phone'
                      u' format.'))

        return phone_number
Esempio n. 7
0
class UserAddressFactory(factory.DjangoModelFactory):
    title = "Dr"
    first_name = "Barry"
    last_name = 'Barrington'
    line1 = "1 King Road"
    line4 = "London"
    postcode = "SW1 9RE"
    phone_number = PhoneNumber.from_string('+49 351 3296645')
    country = factory.SubFactory(CountryFactory)
    user = factory.SubFactory(UserFactory)

    class Meta:
        model = get_model('address', 'UserAddress')
Esempio n. 8
0
 def test_can_assign_phone_number(self):
     phone = MandatoryPhoneNumber()
     phone.phone_number = PhoneNumber.from_string(valid_number)
     self.assertEqual(type(phone.phone_number), PhoneNumber)
     self.assertEqual(phone.phone_number.as_e164, valid_number)
Esempio n. 9
0
 def test_invalid_numbers_are_invalid(self):
     numbers = [PhoneNumber.from_string(number_string)
                for number_string in invalid_numbers]
     self.assertTrue(all([not number.is_valid() for number in numbers]))
Esempio n. 10
0
 def test_can_assign_phone_number(self):
     phone = MandatoryPhoneNumber()
     phone.phone_number = PhoneNumber.from_string(valid_number)
     self.assertEqual(type(phone.phone_number), PhoneNumber)
     self.assertEqual(phone.phone_number.as_e164, valid_number)
Esempio n. 11
0
 def test_invalid_numbers_are_invalid(self):
     numbers = [PhoneNumber.from_string(number_string)
                for number_string in invalid_numbers]
     self.assertTrue(all([not number.is_valid() for number in numbers]))