Exemple #1
0
 def test_not_strict_add(self):
     """
     Test that for not strict lookup a new value is added
     """
     # choose a non_strict_lookup
     f = self.not_strict_lookup_fields[0]
     original_values = util_model.get_field_lookup_values(f)
     original_size = len(original_values)
     new_value = "test38738"
     to_lookup_raise(f, new_value, commit=True)
     # the new value should have been added
     values = util_model.get_field_lookup_values(f)
     self.assertEqual(original_size + 1, len(values))
     self.assertTrue(new_value in values)
Exemple #2
0
 def test_not_strict_blank(self):
     """
     A blank value for a non strict lookup should not be added
     """
     # choose a non_strict_lookup
     f = self.not_strict_lookup_fields[0]
     original_values = util_model.get_field_lookup_values(f)
     original_size = len(original_values)
     blanks = ["", None, "   "]
     for blank in blanks:
         lookup = to_lookup_raise(f, blank, commit=True)
         values = util_model.get_field_lookup_values(f)
         self.assertEqual(original_size, len(values))
         self.assertTrue(blank not in values)
Exemple #3
0
    def test_strict_code_or_value_case_insensitive(self):
        """
        If strict it validates against the value OR the code
        """
        for field in self.strict_lookup_fields:
            lookup_values = util_model.get_field_lookup_values(field)
            lookup_codes = [code for code in util_model.get_field_lookup_codes(field) if code]
            for value in lookup_values:
                valid, message = validate_lookup(field, value)
                self.assertTrue(valid)
                self.assertIsNone(message)
                # test case insensitive
                valid, message = validate_lookup(field, value.capitalize().swapcase())
                self.assertTrue(valid)
                self.assertIsNone(message)
            for value in lookup_codes:
                self.assertTrue(validate_lookup(field, value)[0])
                # test case insensitive
                self.assertTrue(validate_lookup(field, value.capitalize().swapcase())[0])
                # test blank is not accepted
                valid, message = validate_lookup(field, "")
                self.assertFalse(valid)
                # test that there's a message
                self.assertTrue(message)

                # test that boolean, none, int doesn't break anything
                for weird in ["", 12, None, True, {}, []]:
                    valid, message = validate_lookup(field, weird)
                    self.assertFalse(valid)
                    # test that there's a message
                    self.assertTrue(message)

                # random
                randoms = [str(random.randint(1783, 6900)), "dhgd6sdkcdfdee765&"]
                for r in randoms:
                    valid, message = validate_lookup(field, r)
                    self.assertFalse(valid)
                    # test that there's a message
                    self.assertTrue(message)
Exemple #4
0
def to_lookup_raise(field, value, commit=True, default=None):
    """
    Rules:
        validate only for 'strict' lookups
        validate against code or values (case insensitive)
        deprecated lookups are rejected.
    """
    if is_blank(value):
        return default
    if not util_model.is_lookup_field(field):
        raise FieldErrorException("{field} is not a lookup".format(field=field))
    lookup_model = field.related_model
    value = str(value) if value is not None else None  # operate on string only
    # search for lookup code first and value after (case insensitive)
    lookup = lookup_model.objects.filter(code__iexact=value).first() \
             or lookup_model.objects.filter(value__iexact=value).first()
    if lookup is None:
        # can't find. If lookup is strict, it's an error
        if util_model.is_strict_lookup_field(field):
            codes = [str(c) for c in util_model.get_field_lookup_codes(field)]
            values = [str(v) for v in util_model.get_field_lookup_values(field)]
            accepted_values = codes + values
            message = "{value} is not an authorized lookup value for {field}. Should be one of: {values}" \
                .format(value=value, field=field.verbose_name, values=accepted_values)
            raise FieldErrorException(message)
        elif value is not None and len(value.strip()) > 0:
            # if not strict we add a new lookup in the value.
            lookup = lookup_model(value=value)
            if commit:
                lookup.save()
    elif lookup.deprecated:
        message = "{value} is a deprecated value for {field}" \
            .format(value=value, field=field.verbose_name)
        raise FieldErrorException(message)

    return lookup