def _get_field_or_error(self, field_name): """ Check that the attempted filter field is valid :raises InvalidFilterError: If the filter field is not valid """ predeclared_fields = self.serializer_class._declared_fields initialized_fields = self.get_serializer().fields if hasattr( self, 'get_serializer') else {} serializer_fields = predeclared_fields.copy() # Merges fields that were declared on serializer with fields that may have been dynamically added serializer_fields.update(initialized_fields) if field_name not in serializer_fields: raise InvalidFilterError( detail="'{0}' is not a valid field for this endpoint.".format( field_name)) if field_name not in getattr(self.serializer_class, 'filterable_fields', set()): raise InvalidFilterFieldError(parameter='filter', value=field_name) field = serializer_fields[field_name] # You cannot filter on deprecated fields. if isinstance(field, ShowIfVersion) and utils.is_deprecated( self.request.version, field.min_version, field.max_version): raise InvalidFilterFieldError(parameter='filter', value=field_name) return serializer_fields[field_name]
def _get_field_or_error(self, field_name): """ Check that the attempted filter field is valid :raises InvalidFilterError: If the filter field is not valid """ if field_name not in self.serializer_class._declared_fields: raise InvalidFilterError(detail="'{0}' is not a valid field for this endpoint.".format(field_name)) if field_name not in getattr(self.serializer_class, 'filterable_fields', set()): raise InvalidFilterFieldError(parameter='filter', value=field_name) return self.serializer_class._declared_fields[field_name]
def convert_special_params_to_odm_query(self, field_name, query_params, key, data): if isinstance(data, list): if utils.is_falsy(query_params[key]): # Use `or` when looking for not-preprints, to include both no file and is_orphaned sub_query = functools.reduce(operator.or_, [ MQ(item['source_field_name'], item['op'], item['value']) for item in data ]) else: sub_query = functools.reduce(operator.and_, [ MQ(item['source_field_name'], item['op'], item['value']) for item in data ]) return sub_query else: raise InvalidFilterError('Expected type list for field {}, got {}'.format(field_name, type(data)))
def _get_field_or_error(self, field_name): """ Check that the attempted filter field is valid :raises InvalidFilterError: If the filter field is not valid """ serializer_class = self.serializer_class if field_name not in serializer_class._declared_fields: raise InvalidFilterError(detail="'{0}' is not a valid field for this endpoint.".format(field_name)) if field_name not in getattr(serializer_class, 'filterable_fields', set()): raise InvalidFilterFieldError(parameter='filter', value=field_name) field = serializer_class._declared_fields[field_name] # You cannot filter on deprecated fields. if isinstance(field, ShowIfVersion) and utils.is_deprecated(self.request.version, field.min_version, field.max_version): raise InvalidFilterFieldError(parameter='filter', value=field_name) return serializer_class._declared_fields[field_name]