How to use the phonenumbers.phonenumberutil 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 regel / loudml / loudml-import / loudml / phone_rates.py View on Github external
def parse_number(self, number):
        try:
            return self._parse_number(number)
        except phonenumbers.phonenumberutil.NumberParseException as exn:
            logging.error("invalid number: %s", number)
            return {
                'premium': False,
                'mobile': False,
                'international': False,
                'phonenumber': number,
                'region': 'INVALID',
                'country': 'INVALID'
            }
github pennlabs / penn-courses / backend / courses / models.py View on Github external
def save(self, *args, **kwargs):
        if self.phone is not None and self.phone.strip() == "":
            self.phone = None
        if self.phone is not None:
            try:
                phone_number = phonenumbers.parse(self.phone, "US")
                self.phone = phonenumbers.format_number(
                    phone_number, phonenumbers.PhoneNumberFormat.E164
                )
            except phonenumbers.phonenumberutil.NumberParseException:
                raise ValidationError("Invalid phone number (this should have been caught already)")
        super(UserProfile, self).save(*args, **kwargs)
github 475Cumulus / TBone / tbone / data / fields / phone_number.py View on Github external
def __eq__(self, other):
        '''
        Override parent equality because we store only string representation
        of phone number, so we must compare only this string representation
        '''
        if (isinstance(other, PhoneNumber) or
                isinstance(other, phonenumbers.phonenumber.PhoneNumber) or
                isinstance(other, str)):

            fmt = self.FORMATS['E164']
            if isinstance(other, str):
                # convert string to phonenumbers.phonenumber.PhoneNumber instance
                try:
                    other = phonenumbers.phonenumberutil.parse(other)
                except phonenumbers.phonenumberutil.NumberParseException:
                    # Conversion is not possible, thus not equal
                    return False
            other_string = phonenumbers.format_number(other, fmt)
            return self.format_as(fmt) == other_string
        else:
            return False
github flectra-hq / flectra / addons / sms / wizard / send_sms.py View on Github external
def _sms_sanitization(self, partner, field_name):
        number = partner[field_name]
        if number and _sms_phonenumbers_lib_imported:
            country = self._phone_get_country(partner)
            country_code = country.code if country else None
            try:
                phone_nbr = phonenumbers.parse(number, region=country_code, keep_raw_input=True)
            except phonenumbers.phonenumberutil.NumberParseException:
                return number
            if not phonenumbers.is_possible_number(phone_nbr) or not phonenumbers.is_valid_number(phone_nbr):
                return number
            phone_fmt = phonenumbers.PhoneNumberFormat.E164
            return phonenumbers.format_number(phone_nbr, phone_fmt)
        else:
            return number