biweeklybudget.flaskapp.views.searchableajaxview module

class biweeklybudget.flaskapp.views.searchableajaxview.SearchableAjaxView[source]

Bases: flask.views.MethodView

MethodView with helper methods for searching via DataTables ajax.


Given a 1-dimensional dict of request parameters like those used by DataTables (i.e. keys like columns[2][search][value]), return a multidimensional dict representation of the same.

Returns:deep/nested dict
Return type:dict

Given a string portion of something in the argument dict, return it as the correct type.

Parameters:a (str) – args dict key or value
Returns:a in the proper type
_filterhack(qs, s, args)[source]

DataTables 1.10.12 has built-in support for filtering based on a value in a specific column; when this is done, the filter value is set in columns[N][search][value] where N is the column number. However, the python datatables package used here only supports the global search[value] input, not the per-column one.

However, the DataTable search is implemented by passing a callable to table.searchable() which takes two arguments, the current Query that’s being built, and the user’s search[value] input; this must then return a Query object with the search applied.

In python datatables 0.4.9, this code path is triggered on if callable(self.search_func) and search.get("value", None):

As such, we can “trick” the table to use per-column searching (currently only if global searching is not being used) by examining the per-column search values in the request, and setting the search function to one (this method) that uses those values instead of the global search[value].

  • qs (sqlalchemy.orm.query.Query) – Query currently being built
  • s (str) – user search value
  • args (dict) – args

Query with searching applied

Return type:


Determine if we have a column filter/search in effect, and if so, should use _filterhack() as our search function.

Parameters:args (dict) – current request arguments
Returns:whether or not request asks for column filtering
Return type:bool

Render and return JSON response for GET.

methods = {'GET'}