django_ufilter.utils module

class django_ufilter.utils.FilterSpec(components, lookup, value, is_negated=False, filter_callable=None)[source]

Bases: object

Class for describing filter specification.

The main job of the FilterSet is to parse the submitted lookups into a list of filter specs. A list of these specs is then used by the filter backend to actually filter given queryset. That’s what FilterSpec provides - a way to portably define filter specification to be used by a filter backend.

The reason why filtering is decoupled from the FilterSet is because this allows to implement filter backends not related to Django.

components

A list of strings which are names of the keys/attributes to be used in filtering of the queryset. For example lookup config with key user__profile__email will have components of ['user', 'profile', 'email'].

Type

list

lookup

Name of the lookup how final key/attribute from components should be compared. For example lookup config with key user__profile__email__contains will have a lookup contains.

Type

str

value

Value of the filter.

is_negated

Whether this filter should be negated. By default its False.

Type

bool, optional

filter_callable

Callable which should be used for filtering this filter spec. This is primaliry meant to be used by CallableFilter.

Type

func, optional

property is_callable

Property for getting whether this filter specification is for a custom filter callable

class django_ufilter.utils.LookupConfig(key, data)[source]

Bases: object

Lookup configuration which is used by FilterSet to create a FilterSpec.

The main purpose of this config is to allow the use if recursion in FilterSet. Each lookup key (the keys in the querystring) is parsed into a nested one-key dictionary which lookup config stores.

For example the querystring:

?user__profile__email__endswith=gmail.com

is parsed into the following config:

{
    'user': {
        'profile': {
            'email': {
                'endswith': 'gmail.com'
            }
        }
    }
}
key

Full lookup key from the querystring. For example user__profile__email__endswith

Type

str

data

Either:

  • nested dictionary where the key is the next key within the lookup chain and value is another LookupConfig

  • the filtering value as provided in the querystring value

Type

dict, str

Parameters
  • key (str) – Full lookup key from the querystring.

  • data (dict, str) – A regular vanilla Python dictionary. This class automatically converts nested dictionaries to instances of LookupConfig. Alternatively a filtering value as provided in the querystring.

as_dict()[source]

Converts the nested LookupConfig to a regular dict.

is_key_value()[source]

Check if this LookupConfig is not a nested LookupConfig but instead the value is a non-dict value.

property name

If the data is nested LookupConfig, this gets its first lookup key.

property value

If the data is nested LookupConfig, this gets its first lookup value which could either be another LookupConfig or actual filtering value.

class django_ufilter.utils.SubClassDict[source]

Bases: dict

Special-purpose dict with special getter for looking up values by finding matching subclasses.

This is better illustrated in an example:

>>> class Klass(object): pass
>>> class Foo(object): pass
>>> class Bar(Foo): pass
>>> mapping = SubClassDict({
...     Foo: 'foo',
...     Klass: 'klass',
... })
>>> print(mapping.get(Klass))
klass
>>> print(mapping.get(Foo))
foo
>>> print(mapping.get(Bar))
foo
get(k, d=None)[source]

If no value is found by using Python’s default implementation, try to find the value where the key is a base class of the provided search class.

django_ufilter.utils.dict_pop(key, d)[source]

Pop key from dictionary and return updated dictionary

django_ufilter.utils.dictify(obj)[source]

Convert any object to a dictionary.

If the given object is already an instance of a dict, it is directly returned. If not, then all the public attributes of the object are returned as a dict.

django_ufilter.utils.suppress(e)[source]

Suppress given exception type

For example:

>>> with suppress(ValueError):
...     print('test')
...     raise ValueError
test