def testMatchesMultiplePhoneNumbersSeparatedByPhoneNumberPunctuation(self):
        text = "Call 650-253-4561 -- 455-234-3451"
        region = "US"
        number1 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region),
                              national_number=6502534561)
        match1 = PhoneNumberMatch(5, "650-253-4561", number1)
        number2 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region),
                              national_number=4552343451)
        match2 = PhoneNumberMatch(21, "455-234-3451", number2)

        matches = PhoneNumberMatcher(text, region)
        self.assertEqual(match1, matches.next())
        self.assertEqual(match2, matches.next())
    def testMatchesMultiplePhoneNumbersSeparatedByPhoneNumberPunctuation(self):
        text = "Call 650-253-4561 -- 455-234-3451"
        region = "US"
        number1 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region),
                              national_number=6502534561L)
        match1 = PhoneNumberMatch(5, "650-253-4561", number1)
        number2 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region),
                              national_number=4552343451L)
        match2 = PhoneNumberMatch(21, "455-234-3451", number2)

        matches = PhoneNumberMatcher(text, region)
        self.assertEqual(match1, matches.next())
        self.assertEqual(match2, matches.next())
Example #3
0
def get_address_form(data,
                     country_code,
                     initial=None,
                     instance=None,
                     **kwargs):
    country_form = AddressMetaForm(data, initial=initial)
    preview = False
    if country_form.is_valid():
        country_code = country_form.cleaned_data['country']
        preview = country_form.cleaned_data['preview']

    if initial is None and country_code:
        initial = {}
    if country_code:
        initial['phone'] = '+{}'.format(country_code_for_region(country_code))

    address_form_class = get_address_form_class(country_code)

    if not preview and instance is not None:
        address_form_class = get_address_form_class(instance.country.code)
        address_form = address_form_class(data, instance=instance, **kwargs)
    else:
        initial_address = (initial if not preview else
                           data.dict() if data is not None else data)
        address_form = address_form_class(not preview and data or None,
                                          initial=initial_address,
                                          **kwargs)
    return address_form, preview
Example #4
0
def get_address_form(data,
                     country_code,
                     initial=None,
                     instance=None,
                     **kwargs):
    country_form = AddressMetaForm(data, initial=initial)
    preview = False
    if country_form.is_valid():
        country_code = country_form.cleaned_data['country']
        preview = country_form.cleaned_data['preview']

    if initial is None and country_code:
        initial = {}
    if country_code:
        initial['phone'] = '+{}'.format(country_code_for_region(country_code))

    address_form_class = get_address_form_class(country_code)

    if not preview and instance is not None:
        address_form_class = get_address_form_class(instance.country.code)
        address_form = address_form_class(data, instance=instance, **kwargs)
    else:
        initial_address = (initial if not preview else
                           data.dict() if data is not None else data)
        address_form = address_form_class(not preview and data or None,
                                          initial=initial_address,
                                          **kwargs)

    if hasattr(address_form.fields['country_area'], 'choices'):
        choices = address_form.fields['country_area'].choices
        choices = [(choice[1], choice[1]) for choice in choices]
        address_form.fields['country_area'].choices = choices
    return address_form, preview
Example #5
0
def get_address_form(data, country_code, initial=None, instance=None, **kwargs):
    country_form = AddressMetaForm(data, initial=initial)
    preview = False
    if country_form.is_valid():
        country_code = country_form.cleaned_data["country"]
        preview = country_form.cleaned_data["preview"]

    if initial is None and country_code:
        initial = {}
    if country_code:
        initial["phone"] = "+{}".format(country_code_for_region(country_code))

    address_form_class = get_address_form_class(country_code)

    if not preview and instance is not None:
        address_form_class = get_address_form_class(instance.country.code)
        address_form = address_form_class(data, instance=instance, **kwargs)
    else:
        initial_address = (
            initial if not preview else data.dict() if data is not None else data
        )
        address_form = address_form_class(
            not preview and data or None, initial=initial_address, **kwargs
        )

    if hasattr(address_form.fields["country_area"], "choices"):
        choices = address_form.fields["country_area"].choices
        choices = [(choice[1], choice[1]) for choice in choices]
        address_form.fields["country_area"].choices = choices
    return address_form, preview
Example #6
0
def get_address_form(
        data, country_code, initial=None, instance=None, **kwargs):
    country_form = AddressMetaForm(data, initial=initial)
    preview = False
    if country_form.is_valid():
        country_code = country_form.cleaned_data['country']
        preview = country_form.cleaned_data['preview']

    if initial is None and country_code:
        initial = {}
    if country_code:
        initial['phone'] = '+{}'.format(country_code_for_region(country_code))

    address_form_class = get_address_form_class(country_code)

    if not preview and instance is not None:
        address_form_class = get_address_form_class(
            instance.country.code)
        address_form = address_form_class(
            data, instance=instance, **kwargs)
    else:
        initial_address = (
            initial if not preview
            else data.dict() if data is not None else data)
        address_form = address_form_class(
            not preview and data or None,
            initial=initial_address,
            **kwargs)
    return address_form, preview
    def testSequences(self):
        # Test multiple occurrences.
        text = "Call 033316005  or 032316005!"
        region = "NZ"

        number1 = PhoneNumber()
        number1.country_code = phonenumberutil.country_code_for_region(region)
        number1.national_number = 33316005
        match1 = PhoneNumberMatch(5, "033316005", number1)

        number2 = PhoneNumber()
        number2.country_code = phonenumberutil.country_code_for_region(region)
        number2.national_number = 32316005
        match2 = PhoneNumberMatch(19, "032316005", number2)

        matcher = PhoneNumberMatcher(text, region, Leniency.POSSIBLE, sys.maxint)

        self.assertEquals(match1, matcher.next())
        self.assertEquals(match2, matcher.next())
        self.assertFalse(matcher.has_next())
Example #8
0
def int_calling_code(country_name):
    try:
        alpha2 = pc.countries.get(name=country_name).alpha_2
        country_code = country_code_for_region(alpha2)
    except:
        try:
            alpha2 = pc.countries.get(
                name=incorrect_country_name_resolution[country_name]).alpha_2
            country_code = country_code_for_region(alpha2)
        except:
            try:
                right_one = [
                    cn for cn in iso_country_list if country_name in cn
                ][0]
                alpha2 = pc.countries.get(name=right_one).alpha_2
                country_code = country_code_for_region(alpha2)
            except:
                country_code = country_code_for_region(
                    new_countries[country_name])
    return str(country_code)
    def testSequences(self):
        # Test multiple occurrences.
        text = "Call 033316005  or 032316005!"
        region = "NZ"

        number1 = PhoneNumber()
        number1.country_code = phonenumberutil.country_code_for_region(region)
        number1.national_number = 33316005
        match1 = PhoneNumberMatch(5, "033316005", number1)

        number2 = PhoneNumber()
        number2.country_code = phonenumberutil.country_code_for_region(region)
        number2.national_number = 32316005
        match2 = PhoneNumberMatch(19, "032316005", number2)

        matcher = PhoneNumberMatcher(text, region, Leniency.POSSIBLE, 65535)

        self.assertEqual(match1, matcher.next())
        self.assertEqual(match2, matcher.next())
        self.assertFalse(matcher.has_next())