def extract_phone(self, field_value): """ Parses the input string to create a PhoneNumber entity with all the possible values. Args: field_value -> The value of the field where the information will be collected from """ try: parsed = phonenumbers.parse(field_value) except phonenumbers.NumberParseException as e: # The phone number couldn't be parsed return None # Checks if it could be a phone number, but not if it could be real if phonenumbers.is_possible_number(parsed): nsn = str(phonenumbers.national_significant_number(parsed)) ndc_len = phonenumbers.length_of_national_destination_code(parsed) if ndc_len > 0: area_code = nsn[:ndc_len] lastnumbers = nsn[ndc_len:] else: area_code = None lastnumbers = nsn return ents.PhoneNumber(phonenumber=field_value, countrycode=parsed.country_code, areacode=area_code if area_code else None, lastnumbers=lastnumbers) return None
def profile_phone_number_to_raw_number(cls, exam_profile): """ Get just the number for a profile's phone number Args: exam_profile (exams.models.ExamProfile): the ExamProfile being written Returns: str: full phone number minus the country code """ phone_number = cls._parse_phone_number(exam_profile.profile.phone_number) return phonenumbers.national_significant_number(phone_number)
def profile_phone_number_to_raw_number(cls, exam_profile): """ Get just the number for a profile's phone number Args: exam_profile (exams.models.ExamProfile): the ExamProfile being written Returns: str: full phone number minus the country code """ phone_number = cls._parse_phone_number( exam_profile.profile.phone_number) return phonenumbers.national_significant_number(phone_number)
def decompress(self, value): """ If an incomplete phone number (e.g. without country prefix) is currently entered, the default implementation just discards the value and shows nothing at all. Let's rather show something invalid, so the user is prompted to fix it, instead of silently deleting data. """ if value: if type(value) == PhoneNumber: if value.country_code and value.national_number: return [ "+%d" % value.country_code, national_significant_number(value), ] return [None, str(value)] elif "." in value: return value.split(".") else: return [None, value] return [None, ""]
def get_nsn(self): self.phonenumber_details[ 'nsn'] = phonenumbers.national_significant_number( self.phonenumber_object) return self.phonenumber_details['nsn']
def get_countrycode(self): self.phonenumber_details['code'] =self.phonenumber[:len(self.phonenumber)-len(str(phonenumbers.national_significant_number(self.phonenumber_object)))] return self.phonenumber_details['code']
def get_nsn(self): self.phonenumber_details['nsn'] = phonenumbers.national_significant_number(self.phonenumber_object) return self.phonenumber_details['nsn']