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 amodel_propertyto 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.
- filter_greater_equal(query, value)[source]¶
Filter data with a greater than or 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.
- class wuttaweb.grids.filters.DateAlchemyFilter(*args, **kwargs)[source]¶
SQLAlchemy filter option for a
Datecolumn.Subclass of
AlchemyFilter.This filter class has custom logic to coerce the input value, but does not have custom filter logic beyond that.
- coerce_value(value)[source]¶
Convert the given value to a proper
datetime.dateobject, if applicable.If the input value is already a date object, it is returned as-is.
Otherwise it is assumed to be a string in
%Y-%m-%dformat, and will be converted to a date object.If the conversion fails, or no value is provided,
Noneis returned.
- class wuttaweb.grids.filters.DateTimeAlchemyFilter(*args, **kwargs)[source]¶
SQLAlchemy filter option for a
DateTimecolumn.Subclass of
DateAlchemyFilter.This filter class has custom logic to coerce the input value, inherited from parent class. It also has custom filter logic for most verbs (not/equal, greater/less than etc.).
Please note that this class assumes the underlying data uses “naive UTC” values. It therefore will convert to/from local time zone accordingly, to ensure user gets the data they expect. For more info see DateTime Behavior.
- filter_equal(query, value)[source]¶
Find all records with datetime values which fall on the given date.
- filter_greater_equal(query, value)[source]¶
Find all records with datetime values which fall on or after the given date.
- filter_greater_than(query, value)[source]¶
Find all records with datetime values which fall after the given date.
- filter_less_equal(query, value)[source]¶
Find all records with datetime values which fall on or before the given date.
- filter_less_than(query, value)[source]¶
Find all records with datetime values which fall before the given date.
- filter_not_equal(query, value)[source]¶
Find all records with datetime values which fall outside the given date.
- get_end_datetime(value, as_utc=True)[source]¶
Calculate the “end” timestamp for the given date value.
Due to the nature of queries involving this “end” boundary, the return value will be the “first possible moment” of the day after the given date.
- Parameters:
value –
datetime.dateinstanceas_utc – Indicates the return value should be naive/UTC; set this to
Falseto get the aware/local value.
- Returns:
datetime.datetimeinstance
- get_start_datetime(value, as_utc=True)[source]¶
Calculate the “start” timestamp for the given date value.
The return value will be the “first possible moment” of the given date.
- Parameters:
value –
datetime.dateinstanceas_utc – Indicates the return value should be naive/UTC; set this to
Falseto get the aware/local value.
- Returns:
datetime.datetimeinstance
- class wuttaweb.grids.filters.GridFilter(request, key, label=None, verbs=None, choices=None, nullable=None, 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.
nullable – Boolean indicating whether the filter should include
is_nullandis_not_nullverbs. If not specified, the column will be inspected (if possible) and use its nullable flag.**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.
- choices¶
OrderedDict of possible values for the filter.
This is safe to read from, but use
set_choices()to update it.
- default_active¶
Boolean indicating whether the filter should be active by default, i.e. when first displaying the grid.
See also
default_verbanddefault_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_activeis 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_activeis 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
verband/orvalueinstead.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. ifvalueis empty, in which case this may return the original data set unchanged.- Returns:
The (possibly) filtered data set.
- coerce_value(value)[source]¶
Coerce the given value to the correct type/format for use with the filter. This is where e.g. a boolean or date filter should convert input string to
boolordatevalue.This is (usually) called from a filter method, when applying the filter. See also
apply_filter().Default logic on the base class returns value as-is; subclass may override as needed.
- Parameters:
value – Input string provided by the user via the filter form submission.
- Returns:
Value of the appropriate type, depending on the filter subclass.
- filter_is_any(data, value)[source]¶
This is a no-op which always ignores the value and returns the data as-is.
- 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.Enumclasssimple 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:
- set_choices(choices)[source]¶
Set the value choices for the filter.
If
choicesis non-empty, it is passed tonormalize_choices()and the result is assigned tochoices. Also, thedata_typeis set to'choice'so the UI will present the value input as a dropdown.But if
choicesis empty,choicesis set to an empty dict, anddata_typeis 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.