def filter_field(key, fields, func=None): """ Run a field through the given filter, or default if none is provided. If field doesn't exist, it will attempt to return the default value for that field. :param key: the field name to use :param fields: a dictionary of available fields in the form ``name: value`` :returns: Filtered value :raises: KeyError in the event the field can't be found among the provided ones or the defaults """ try: val = fields[key] except KeyError: default_fields = getattr(settings, 'PUBLISH_FIELD_DEFAULTS', {}) FIELD_DEFAULTS.update(default_fields) try: val = FIELD_DEFAULTS[key] except KeyError: raise ConfigurationError('You must supply the `%s` field in the' ' article, via settings.PUBLISH_FIELD_DEFAULTS, or' ' via command line (where applicable)' % key) default_filters = getattr(settings, 'PUBLISH_FILTER_DEFAULTS', {}) FILTER_DEFAULTS.update(default_filters) f = func or FILTER_DEFAULTS[key] return f(val)