def main(fn): from portfoliyo.formats import normalize_phone with open(fn, 'rb') as fh: reader = csv.reader(fh) names_by_phone = {} # maps (name, phone) to full person data people = {} for row in reader: first, last, region, phone, email, role = map(clean, row[:6]) name = '%s %s' % (first, last) validated_phone = normalize_phone(phone) if validated_phone is None: sys.stderr.write( "%s, %s\n" % (name, phone)) continue if phone in names_by_phone and names_by_phone[phone] != name: sys.stderr.write( 'Name mismatch: %s vs %s\n' % (name, names_by_phone[phone])) continue names_by_phone[phone] = name groups = {"Region %s" % region, role} uid = (name, validated_phone) if uid in people: people[uid]['groups'].update(groups) else: people[uid] = { 'name': name, 'phone': validated_phone, 'groups': groups} writer = csv.DictWriter(sys.stdout, ['name', 'phone', 'groups']) for row in people.values(): row['groups'] = '::'.join(row['groups']) writer.writerow(row)
def clean_phone(self): """Ensure phone number is valid.""" phone = self.cleaned_data.get('phone', "") if not phone and not self.fields['phone'].required: return phone phone = formats.normalize_phone(phone) if phone is None: raise forms.ValidationError( "Please supply a valid US or Canada mobile number.") return phone
def test_invalid_phone_numbers(raw): """Returns None for unparseable phone numbers.""" assert formats.normalize_phone(raw) is None
def test_valid_phone_numbers(raw): """Returns valid phone numbers re-formatted to E.164 format.""" assert formats.normalize_phone(raw) == '+13214567890'