How to use the phonenumbers.NumberParseException function in phonenumbers

To help you get started, we’ve selected a few phonenumbers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tracon / kompassi / core / models / person.py View on Github external
def get_normalized_phone_number(
        self,
        region=settings.KOMPASSI_PHONENUMBERS_DEFAULT_REGION,
        format=settings.KOMPASSI_PHONENUMBERS_DEFAULT_FORMAT
    ):
        """
        Returns the phone number of this Person in a normalized format. If the phone number is invalid,
        this is logged, and the invalid phone number is returned as-is.
        """

        try:
            return format_phone_number(self.phone, region=region, format=format)
        except phonenumbers.NumberParseException:
            logger.exception('Person %s has invalid phone number: %s', self, self.phone)
            return self.phone
github mirumee / saleor / saleor / account / i18n.py View on Github external
def clean_phone_for_country(phone, country):
    error = _("The phone number entered is not valid.")
    error_code = "invalid_phone_number"
    if phone:
        try:
            phone = PhoneNumber.from_string(phone, country)
        except NumberParseException:
            raise forms.ValidationError(error, code=error_code)
        else:
            if not is_possible_number(phone):
                raise forms.ValidationError(error, code=error_code)
    return phone
github rapidpro / casepro / casepro / contacts / models.py View on Github external
def validate_phone(cls, number):
        """
        Validates the given phone number which should be in E164 format.
        """
        try:
            parsed = phonenumbers.parse(number)
        except phonenumbers.NumberParseException as e:
            raise InvalidURN(str(e))

        if number != phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.E164):
            raise InvalidURN("Phone numbers must be in E164 format")

        if not phonenumbers.is_possible_number(parsed) or not phonenumbers.is_valid_number(parsed):
            raise InvalidURN("Phone numbers must be in E164 format")

        return True
github democraciaconcodigos / escrutinio-social / fiscales / forms.py View on Github external
def clean_telefono(self):
        valor = self.cleaned_data['telefono']
        try:
            valor = validar_telefono(valor)
        except (AttributeError, phonenumbers.NumberParseException):
            raise forms.ValidationError('No es un teléfono válido')
        return valor
github kvh / match / match / datatypes.py View on Github external
def parse_to_object(self, number):
        """Returns None if not a valid phone number"""
        if not number:
            return None
        number = number.strip()
        try:
            parsed_number = phonenumbers.parse(number, self.default_region)
            if not phonenumbers.is_valid_number(parsed_number):
                return None
            return parsed_number
        except phonenumbers.NumberParseException:
            return None
github django-oscar / django-oscar / src / oscar / views / generic.py View on Github external
# There is no shipping country, not a valid international
                # number
                raise ValidationError(
                    _(u'This is not a valid international phone format.'))

            # 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
github closeio / flask-common / flask_common / mongo / fields / phone.py View on Github external
def validate(self, value):
        if not self.required and not value:
            return None

        error_msg = 'Phone number is not valid. Please use the international format like +16505551234'
        try:
            number = PhoneField._parse(value)

            if self._strict_validation and not phonenumbers.is_valid_number(
                number
            ):
                raise phonenumbers.NumberParseException(
                    phonenumbers.NumberParseException.NOT_A_NUMBER, error_msg
                )

        except phonenumbers.NumberParseException:
            self.error(error_msg)
github Bouke / django-two-factor-auth / two_factor / migrations / 0003_auto_20150817_1733.py View on Github external
def migrate_phone_numbers(apps, schema_editor):
    PhoneDevice = apps.get_model("two_factor", "PhoneDevice")
    for device in PhoneDevice.objects.all():
        username = device.user.get_username()
        try:
            number = phonenumbers.parse(device.number)
            if not phonenumbers.is_valid_number(number):
                logger.info("User '%s' has an invalid phone number '%s'." % (username, device.number))
            device.number = phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164)
            device.save()
        except phonenumbers.NumberParseException as e:
            # Do not modify/delete the device, as it worked before. However this might result in issues elsewhere,
            # so do log a warning.
            logger.warning("User '%s' has an invalid phone number '%s': %s. Please resolve this issue, "
                           "as it might result in errors." % (username, device.number, e))
github django-oscar / django-oscar / oscar / views / generic.py View on Github external
# number
                raise ValidationError(
                    _(u'This is not a valid international phone format.'))

            # 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