Beispiel #1
0
 def _create_data_validation(self, field):
     allow_blank = not util_model.is_mandatory(field)
     dv = None
     if util_model.is_lookup_field(field) and util_model.has_related_objects(field):
         # here we expect that the lookup has been registered as named range.
         # The name of the lookup is the lookup model name (see _write_lookups)
         strict = util_model.is_strict_lookup_field(field)
         lookup_name = util_model.get_field_lookup_model_name(field)
         dv = util_xls.create_list_validation(lookup_name, strict=strict, allow_blank=allow_blank)
     elif util_model.has_choices(field):
         # Should we also add the choices in the Lookups sheet?
         values = [str(choice[1]) for choice in util_model.get_field_choices(field)]
         strict = True
         dv = util_xls.create_list_validation(values, strict=strict, allow_blank=allow_blank)
     elif util_model.is_boolean_field(field):
         allow_blank = True  # blank is False
         values = ['Y', 'N']
         strict = False
         dv = util_xls.create_list_validation(values, strict=strict, allow_blank=allow_blank)
     # species. Hack! Last minute update. We want species data validation on animals only
     elif util_model.is_species_observation_field(field)\
             and self.file_species is not None \
             and field.model._meta.app_label == 'animals':
         # we expect here that a range call species has been registered (see _write_species)
         strict = False
         dv = util_xls.create_list_validation('species', strict=strict, allow_blank=allow_blank)
     return dv
Beispiel #2
0
 def setUp(self):
     self.model = Site
     self.all_lookup_fields = [f for f in self.model._meta.fields if util_model.is_lookup_field(f)]
     self.strict_lookup_fields = [f for f in self.all_lookup_fields if util_model.is_strict_lookup_field(f)]
     self.not_strict_lookup_fields = [f for f in self.all_lookup_fields if not util_model.is_strict_lookup_field(f)]
     self.assertTrue(len(self.strict_lookup_fields) > 0)
     self.assertTrue(len(self.not_strict_lookup_fields) > 0)
Beispiel #3
0
 def test_lookups_fields_on_delete_protection(self):
     """
     Test that all the lookups foreign keys are set on_delete=PROTECTED
     """
     lookups_fields = [f for f in self.project_fields if is_lookup_field(f)]
     not_protected = [f for f in lookups_fields if f.remote_field.on_delete != models.PROTECT]
     self.assertTrue(len(not_protected) == 0,
                     msg="{} not protected on delete lookup field(s)."
                         " Should be 0. {}".format(len(not_protected),
                                                   str([(f.name, f.model) for f in not_protected])))
Beispiel #4
0
 def get_field_data(field_):
     return {
         'name': field_.name,
         'is_datasheet_field': utils_model.is_template_field(field_),
         'datasheet_name': utils_model.get_datasheet_field_name(field_),
         'is_lookup': utils_model.is_lookup_field(field_),
         'is_mandatory': utils_model.is_mandatory(field_),
         'is_species_name': utils_model.is_species_observation_field(field_),
         'is_strict_lookup': utils_model.is_strict_lookup_field(field_),
         'is_boolean': utils_model.is_boolean_field(field_),
         'is_extra_species_attribute': utils_model.is_species_observation_extra_attribute(field),
     }
Beispiel #5
0
 def test_non_mandatory_blank_lookup(self):
     """
     Test that a non mandatory lookup with a blank value returns None and doesn't throw an exception
     """
     non_mandatory_lookup_fields = [f for f in self.non_mandatory_fields if util_model.is_lookup_field(f)]
     blanks = [None, '', "  "]
     for f in non_mandatory_lookup_fields:
         for v in blanks:
             try:
                 self.assertEqual(None, to_field_value_raise(f, v))
             except Exception as e:
                 self.assertTrue(False,
                                 msg="A blank value for non mandatory lookup should return None and not throw an exception")
Beispiel #6
0
 def _build_lookup_validation_list(field, sort=True):
     values = []
     if util_model.is_lookup_field(field):
         # we use the code if there is a code or the value
         for value, code in util_model.get_field_lookups(field):
             if code:
                 # BIOSYS-132: if the code is a string that is an integer add it as integer instead of string
                 # to allow input with keyboard
                 if SiteVisitDatasheetWriter._is_integer(code):
                     values.append(int(code))
                 else:
                     values.append(str(code))
             else:
                 values.append(str(value))
     if sort:
         values.sort()
     return values
Beispiel #7
0
def to_field_value_raise(field, value, commit=True, site_visit=None, row_data=None):
    if is_blank(value):
        if util_model.is_mandatory(field):
            message = "Mandatory field with no data {field}.".format(field=field.verbose_name)
            raise ValidationException(message)
    # Every next conversion functions should handle a blank/None value
    if util_model.is_lookup_field(field):
        return to_lookup_raise(field, value, commit=commit)
    if util_model.has_choices(field):
        return to_choice_raise(field, value)
    if util_model.is_boolean_field(field):
        return to_boolean_raise(value)
    if util_model.is_integer_field(field):
        return to_integer_raise(value)
    if util_model.is_float_field(field):
        return to_float_raise(value)
    if util_model.is_date_field(field):
        return to_date_raise(value)
    if util_model.is_string_field(field):
        return to_string(value)
    if util_model.is_species_observation_field(field):
        return to_species_observation_raise(value, site_visit, commit=commit, row_data=row_data)
    return value
Beispiel #8
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