Exemple #1
0
    def get_or_instantiate_for_type(type, area_code, number):
        
        country_code = Phone.COUNTRY_CODE_CHOICES_BRAZIL[0]        
        

        with is_valid_brazilian_area_code(area_code) as code:
            if code.is_valid:

                if type == Phone.TYPE_CHOICES_TELEPHONE[0]:
                    with is_valid_brazilian_telephone_number(number) as phone:
                        if phone.is_valid:
                            hash = Phone.make_hash(country_code, code.number, phone.number)
                            instance = None

                            try: 
                                instance = Phone.objects.only('id', 'carrier_id').get(hash=hash)
                                return dict_to_struct({'instance': instance, 'exists': True })
                            except Phone.DoesNotExist:
                                instance = Phone(hash=hash, type=type, country_code=country_code, area_code=code.number, number=phone.number)
                                return dict_to_struct({'instance': instance, 'exists': False })

                elif type == Phone.TYPE_CHOICES_CELLPHONE[0]:
                    with is_valid_brazilian_cellphone_number(number) as phone:
                        if phone.is_valid:
                            hash = Phone.make_hash(country_code, code.number, phone.number)
                            instance = None

                            try: 
                                instance = Phone.objects.only('id', 'carrier_id').get(hash=hash)                            
                                return dict_to_struct({'instance': instance, 'exists': True })
                            except Phone.DoesNotExist:
                                instance = Phone(hash=hash, type=type, country_code=country_code, area_code=code.number, number=phone.number)
                                return dict_to_struct({'instance': instance, 'exists': False })


        raise InvalidContactException(_(u'O número informado não parece ser um telefone/celular válido'))
def get_n_phone_instances(n, carriers):

    types = []
    carriers = carriers or create_and_get_n_carrier_objects(20)
    numbers = []
    areacodes = []
    hashes = []
    phones = []

    for i in xrange(n):
        types.append(random.choice(Phone.TYPE_CHOICES)[0])
        carriers.append(random.choice(carriers))
        numbers.append(get_random_string(length=9))
        areacodes.append(random.choice(Phone.AREACODE_CHOICES)[0])
        hashes.append(Phone.make_hash(types[i], areacodes[i], numbers[i]))
        phones.append(Phone(type=types[i], carrier=carriers[i], number=numbers[
                      i], areacode=areacodes[i], hash=hashes[i]))

    return (hashes, numbers, areacodes, phones)