def parse(klass, row, carrier, areacode, phone_type): row = list(row) # Make row mutable person = Person() person.name = normalize_text(row[2]) address = Address() address.location = normalize_text(row[3]) address.neighborhood = normalize_text(row[4]) address.city = normalize_text(row[5]) address.state = normalize_text(row[6]) phone = Phone() phone.carrier = carrier phone.areacode = areacode phone.type = phone_type try: # document - try CPF person.document = validate_cpf(row[8][-11:]) person.nature = Person.NATURE_CHOICES_PHYSICAL[0] except CPFValidationError: # document - try CNPJ person.document = validate_cnpj(row[8][-14:]) person.nature = Person.NATURE_CHOICES_LEGAL[0] address.zipcode = validate_zipcode(row[7]) phone.number = validate_phone_number(row[1]) return klass(row, person, address, phone)
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)