Exemplo n.º 1
0
 def test_canonicalize_phone_number(self):
     test_data = [
         ('+218 (0)12-345-6789', '218123456789'),
         ('+218 12-345-6789', '218123456789'),
         ('0923456789', '218923456789'),
         ('0987654321', '218987654321'),
         ('+88216-12340058', '8821612340058'),
         ('+88216-1234005', '882161234005'),
     ]
     for input, expected_result in test_data:
         actual_result = canonicalize_phone_number(input)
         self.assertEqual(expected_result, actual_result,
                          msg="%s was formatted as %s but should have been %s" %
                              (input, actual_result, expected_result))
Exemplo n.º 2
0
 def test_canonicalize_phone_number(self):
     test_data = [
         ('+218 (0)12-345-6789', '218123456789'),
         ('+218 12-345-6789', '218123456789'),
         ('0923456789', '218923456789'),
         ('0987654321', '218987654321'),
         ('+88216-12340058', '8821612340058'),
         ('+88216-1234005', '882161234005'),
     ]
     for input, expected_result in test_data:
         actual_result = canonicalize_phone_number(input)
         self.assertEqual(
             expected_result,
             actual_result,
             msg="%s was formatted as %s but should have been %s" %
             (input, actual_result, expected_result))
Exemplo n.º 3
0
    def __init__(self, *args, **kwargs):
        """Remove nondigits from phone_number during init of the edit form.

        Have to do it before the validation, because it'll run the field
        validation before we can get control, and numbers with non-digits
        won't pass.
        """
        if kwargs['data']:
            # This is a POST, leave the data as-is.
            pass
        else:
            if 'instance' in kwargs:
                # User is editing an existing number.
                kwargs['data'] = {}
                kwargs['data']['phone_number'] = \
                    canonicalize_phone_number(kwargs['instance'].phone_number)
            # else:
                # User is adding a new number; nothing to reformat.

        super(BlackWhiteListedNumberEditForm, self).__init__(*args, **kwargs)
Exemplo n.º 4
0
    def __init__(self, *args, **kwargs):
        """Remove nondigits from phone_number during init of the edit form.

        Have to do it before the validation, because it'll run the field
        validation before we can get control, and numbers with non-digits
        won't pass.
        """
        if kwargs['data']:
            # This is a POST, leave the data as-is.
            pass
        else:
            if 'instance' in kwargs:
                # User is editing an existing number.
                kwargs['data'] = {}
                kwargs['data']['phone_number'] = \
                    canonicalize_phone_number(kwargs['instance'].phone_number)
            # else:
            # User is adding a new number; nothing to reformat.

        super(BlackWhiteListedNumberEditForm, self).__init__(*args, **kwargs)
Exemplo n.º 5
0
def process_blackwhitelisted_numbers_file(model, import_file):
    """Process a text file with one phone number per line, adding each phone number to the model.

    The model must be one of Blacklist or Whitelist. The import_file must be an open file object.
    """
    imported = skipped = 0
    errors = []
    for line_number, line in enumerate(import_file.read().splitlines()):
        phone_number = canonicalize_phone_number(line)
        if phone_number:
            if model.objects.filter(phone_number=phone_number).exists():
                skipped += 1
            else:
                obj = model(phone_number=phone_number)
                try:
                    obj.full_clean()
                except ValidationError:
                    errors.append(str(line_number + 1))
                else:
                    obj.save()
                    imported += 1
    return (imported, skipped, errors)