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
def to_choice_raise(field, value): """ Rules: validate only against the display_name, the second part of a Django choice (internal, display_name) case insensitive """ if not util_model.has_choices(field): raise FieldErrorException("The field {field} has no choices".format(field.verbose_name)) if is_blank(value): return field.default value = str(value) choices = util_model.get_field_choices(field) return to_model_choice(choices, value)
def to_choice_raise(field, value): """ Rules: validate only against the display_name, the second part of a Django choice (internal, display_name) case insensitive """ if not util_model.has_choices(field): raise FieldErrorException("The field {field} has no choices".format(field.verbose_name)) if is_blank(value): return field.default value = str(value) choices = util_model.get_field_choices(field) choice = next((c[0] for c in choices if c[1].lower() == value.lower()), None) if choice is None: message = "{value} not an authorized choice. Should be one of: {values}" \ .format(value=value, values=[str(c[1]) for c in choices]) raise FieldErrorException(message) return choice