wuttaweb.grids.filters

Grid Filters

class wuttaweb.grids.filters.AlchemyFilter(*args, **kwargs)[source]

Filter option for a grid with SQLAlchemy query data.

This is a subclass of GridFilter. It requires a model_property to know how to filter the query.

Parameters:
  • model_property – Property of a model class, representing the column by which to filter. For instance, model.Person.full_name.

  • nullable – Boolean indicating whether the filter should include is_null and is_not_null verbs. If not specified, the column will be inspected and use its nullable flag.

coerce_value(value)[source]

Coerce the given value to the correct type/format for use with the filter.

Default logic returns value as-is; subclass may override.

filter_equal(query, value)[source]

Filter data with an equal (=) condition.

filter_greater_equal(query, value)[source]

Filter data with a greater than or equal (>=) condition.

filter_greater_than(query, value)[source]

Filter data with a greater than (>) condition.

filter_is_not_null(query, value)[source]

Filter data with an IS NOT NULL query. The value is ignored.

filter_is_null(query, value)[source]

Filter data with an IS NULL query. The value is ignored.

filter_less_equal(query, value)[source]

Filter data with a less than or equal (<=) condition.

filter_less_than(query, value)[source]

Filter data with a less than (<) condition.

filter_not_equal(query, value)[source]

Filter data with a not equal (!=) condition.

class wuttaweb.grids.filters.BooleanAlchemyFilter(*args, **kwargs)[source]

SQLAlchemy filter option for a boolean data column.

Subclass of AlchemyFilter.

filter_is_false(query, value)[source]

Filter data with an “is false” condition. The value is ignored.

filter_is_false_null(query, value)[source]

Filter data with “is false or null” condition. The value is ignored.

filter_is_true(query, value)[source]

Filter data with an “is true” condition. The value is ignored.

class wuttaweb.grids.filters.DateAlchemyFilter(*args, **kwargs)[source]

SQLAlchemy filter option for a sqlalchemy.types.Date column.

Subclass of AlchemyFilter.

class wuttaweb.grids.filters.GridFilter(request, key, label=None, verbs=None, choices={}, default_active=False, default_verb=None, default_value=None, **kwargs)[source]

Filter option for a grid. Represents both the “features” as well as “state” for the filter.

Parameters:
  • request – Current request object.

  • model_property – Property of a model class, representing the column by which to filter. For instance, model.Person.full_name.

  • **kwargs – Any additional kwargs will be set as attributes on the filter instance.

Filter instances have the following attributes:

key

Unique key for the filter. This often corresponds to a “column name” for the grid, but not always.

label

Display label for the filter field.

data_type

Simplistic “data type” which the filter supports. So far this will be one of:

  • 'string'

  • 'date'

  • 'choice'

Note that this mainly applies to the “value input” used by the filter. There is no data type for boolean since it does not need a value input; the verb is enough.

active

Boolean indicating whether the filter is currently active.

See also verb and value.

verb

Verb for current filter, if active is true.

See also value.

choices

OrderedDict of possible values for the filter.

This is safe to read from, but use set_choices() to update it.

value

Value for current filter, if active is true.

See also verb.

default_active

Boolean indicating whether the filter should be active by default, i.e. when first displaying the grid.

See also default_verb and default_value.

default_verb

Filter verb to use by default. This will be auto-selected when the filter is first activated, or when first displaying the grid if default_active is true.

See also default_value.

default_value

Filter value to use by default. This will be auto-populated when the filter is first activated, or when first displaying the grid if default_active is true.

See also default_verb.

apply_filter(data, verb=None, value=<object object>)[source]

Filter the given data set according to a verb/value pair.

If verb and/or value are not specified, will use verb and/or value instead.

This method does not directly filter the data; rather it delegates (based on verb) to some other method. The latter may choose not to filter the data, e.g. if value is empty, in which case this may return the original data set unchanged.

Returns:

The (possibly) filtered data set.

filter_is_any(data, value)[source]

This is a no-op which always ignores the value and returns the data as-is.

get_default_verb()[source]

Returns the default verb for the filter.

get_valueless_verbs()[source]

Returns a list of verb names which do not need a value.

get_verb_labels()[source]

Returns a dict of all defined verb labels.

get_verbs()[source]

Returns the list of verbs supported by the filter.

normalize_choices(choices)[source]

Normalize a collection of “choices” to standard OrderedDict.

This is called automatically by set_choices().

Parameters:

choices

A collection of “choices” in one of the following formats:

  • enum.Enum class

  • simple list, each value of which should be a string, which is assumed to be able to serve as both key and value (ordering of choices will be preserved)

  • simple dict, keys and values of which will define the choices (note that the final choices will be sorted by key!)

  • OrderedDict, keys and values of which will define the choices (ordering of choices will be preserved)

Return type:

collections.OrderedDict

set_choices(choices)[source]

Set the value choices for the filter.

If choices is non-empty, it is passed to normalize_choices() and the result is assigned to choices. Also, the data_type is set to 'choice' so the UI will present the value input as a dropdown.

But if choices is empty, choices is set to an empty dict, and data_type is set (back) to 'string'.

Parameters:

choices – Collection of “choices” or None.

class wuttaweb.grids.filters.IntegerAlchemyFilter(*args, **kwargs)[source]

SQLAlchemy filter option for an integer data column.

Subclass of NumericAlchemyFilter.

class wuttaweb.grids.filters.NumericAlchemyFilter(*args, **kwargs)[source]

SQLAlchemy filter option for a numeric data column.

Subclass of AlchemyFilter.

class wuttaweb.grids.filters.StringAlchemyFilter(*args, **kwargs)[source]

SQLAlchemy filter option for a text data column.

Subclass of AlchemyFilter.

filter_contains(query, value)[source]

Filter data with an ILIKE condition.

filter_does_not_contain(query, value)[source]

Filter data with a NOT ILIKE condition.