Esempio n. 1
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)
Esempio n. 2
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
Esempio n. 3
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),
     }
Esempio n. 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