rattail.autocomplete.base

Autocomplete handlers - base class

class rattail.autocomplete.base.Autocompleter(config)[source]

Base class and partial default implementation for autocomplete handlers. It is expected that all autocomplete handlers will ultimately inherit from this base class, therefore it defines the implementation “interface” loosely speaking. Custom autocomplete handlers are welcome to supplement or override this as needed, and in fact must do so for certain aspects.

autocompleter_key

The key indicates what “type” of autocompleter this is. It should be a string, e.g. 'products'. It will generally correspond to the route names used in Tailbone, though not always.

max_results

If set, should return no more than this many results. The base default is set, to 100. Set to None to disable limiting the number of results.

The reason to limit results is to avoid situations where the query returns many thousands of records, so that’s slow anyway, but then the browser may well freeze up trying to process it.

Note that your query probably should be sorted somehow, so that the e.g. “first 100” results are more relevant.

Any subclass is free to override this, but you an also set it directly on an instance, e.g.:

autocompleter = app.get_autocompleter('products')
autocompleter.max_results = 250
results = autocompleter.autocomplete(session, "apple cider vinegar")
autocomplete(session, term, **kwargs)[source]

The main reason this class exists. This method accepts a term (string) argument and will return a sequence of matching results.

filter_autocomplete_query(session, query, term)[source]

Apply the actual “search” filtering and return the query.

get_autocomplete_data(session, term, **kwargs)[source]

Collect data for matching results, based on the given search term. This method basically does 2 things:

First it calls make_autocomplete_query() to get the final query, then it invokes the query.

When invoking the query it will “usually” limit the number of results, based on max_results.

get_autocomplete_results(data)[source]

Format the data into a final results set for return to the caller.

make_autocomplete_query(session, term, **kwargs)[source]

Build the complete query from which to obtain search results.

make_base_query(session)[source]

Create and return the base (“unfiltered”) query from which search results will ultimately be obtained.

prepare_autocomplete_term(term, **kwargs)[source]

If necessary, massage the incoming search term for use with the autocomplete query.

restrict_autocomplete_query(session, query, **kwargs)[source]

Optionally restrict (“pre-filter”) the query according to any applicable business logic.

class rattail.autocomplete.base.PhoneMagicMixin[source]

Mixin for adding “phone number magic” to an otherwise sort of normal autocompleter.

The “magic” is that this will try to search for either phone number or contact name. If the search term includes at least 4 digits then it is considered to be a phone number search; otherwise it will be considered a name search.

get_autocomplete_results(data)[source]

We override the formatting of results, because we want the autocomplete results themselves, to appear as “<name> <phone>” in the dropdown user sees, but we also want to include the display key which contains just the name.

The reason for this has to do with how the tailbone-autocomplete comonent works. The display (name only)` will be shown on the button after selection is made.

get_phone_search_term(term)[source]

Try to figure out if the given search term represents a whole or partial phone number, and if so return just the digits.

make_autocomplete_query(session, term, **kwargs)[source]

This is where the magic happens. We override this to check if the search term resembles a phone number and if so, do a phone number search; otherwise a name search.