Add experimental soundex filter support to the Customers grid.

This commit is contained in:
Lance Edgar 2014-06-12 20:41:25 -07:00
parent 8f1e34c73c
commit 54bb9e2869
3 changed files with 44 additions and 11 deletions

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python # -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar # Copyright © 2010-2014 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -26,7 +25,7 @@
Grid Search Filters Grid Search Filters
""" """
from sqlalchemy import or_ from sqlalchemy import func, or_
from webhelpers.html import tags from webhelpers.html import tags
from webhelpers.html import literal from webhelpers.html import literal
@ -55,6 +54,8 @@ class SearchFilter(Object):
('nt', "is not"), ('nt', "is not"),
('lk', "contains"), ('lk', "contains"),
('nl', "doesn't contain"), ('nl', "doesn't contain"),
(u'sx', u"sounds like"),
(u'nx', u"doesn't sound like"),
] ]
options = [] options = []
filter_map = self.search.filter_map[self.name] filter_map = self.search.filter_map[self.name]
@ -179,6 +180,34 @@ def filter_ilike(field):
return {'lk': ilike, 'nl': not_ilike} return {'lk': ilike, 'nl': not_ilike}
def filter_soundex(field):
"""
Returns a filter map entry which leverages the `soundex()` SQL function.
"""
def soundex(query, value):
if value:
query = query.filter(func.soundex(field) == func.soundex(value))
return query
def not_soundex(query, value):
if value:
query = query.filter(func.soundex(field) != func.soundex(value))
return query
return {u'sx': soundex, u'nx': not_soundex}
def filter_ilike_and_soundex(field):
"""
Returns a filter map which provides both the `ilike` and `soundex`
features.
"""
filters = filter_ilike(field)
filters.update(filter_soundex(field))
return filters
def get_filter_config(prefix, request, filter_map, **kwargs): def get_filter_config(prefix, request, filter_map, **kwargs):
""" """
Returns a configuration dictionary for a search form. Returns a configuration dictionary for a search form.

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python # -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar # Copyright © 2010-2014 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -58,7 +57,7 @@ class CustomersGrid(SearchableAlchemyGridView):
def filter_map(self): def filter_map(self):
return self.make_filter_map( return self.make_filter_map(
exact=['id'], exact=['id'],
ilike=['name'], name=self.filter_ilike_and_soundex(model.Customer.name),
email=self.filter_ilike(model.CustomerEmailAddress.address), email=self.filter_ilike(model.CustomerEmailAddress.address),
phone=self.filter_ilike(model.CustomerPhoneNumber.number)) phone=self.filter_ilike(model.CustomerPhoneNumber.number))

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python # -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar # Copyright © 2010-2014 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -139,6 +138,12 @@ class SearchableAlchemyGridView(PagedAlchemyGridView):
def filter_ilike(self, field): def filter_ilike(self, field):
return grids.search.filter_ilike(field) return grids.search.filter_ilike(field)
def filter_soundex(self, field):
return grids.search.filter_soundex(field)
def filter_ilike_and_soundex(self, field):
return grids.search.filter_ilike_and_soundex(field)
def make_filter_map(self, **kwargs): def make_filter_map(self, **kwargs):
return grids.search.get_filter_map(self.mapped_class, **kwargs) return grids.search.get_filter_map(self.mapped_class, **kwargs)