Add shared GPC search filter, use it for product batch rows.

This commit is contained in:
Lance Edgar 2015-03-07 14:19:19 -06:00
parent 51e4eda662
commit 69a5eed83b
5 changed files with 41 additions and 34 deletions

View file

@ -37,9 +37,11 @@ from pyramid.renderers import render
from pyramid_simpleform import Form from pyramid_simpleform import Form
from pyramid_simpleform.renderers import FormRenderer from pyramid_simpleform.renderers import FormRenderer
from rattail.core import Object
from edbob.util import prettify from edbob.util import prettify
from rattail.core import Object
from rattail.gpc import GPC
class SearchFilter(Object): class SearchFilter(Object):
""" """
@ -228,6 +230,32 @@ def filter_ilike_and_soundex(field):
return filters return filters
def filter_gpc(field):
"""
Returns a filter suitable for a GPC field.
"""
def filter_is(q, v):
if not v:
return q
try:
return q.filter(field.in_((
GPC(v), GPC(v, calc_check_digit='upc'))))
except ValueError:
return q
def filter_not(q, v):
if not v:
return q
try:
return q.filter(~field.in_((
GPC(v), GPC(v, calc_check_digit='upc'))))
except ValueError:
return q
return {'is': filter_is, 'nt': filter_not}
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

@ -891,8 +891,9 @@ class ProductBatchRowGrid(BatchRowGrid):
classes should *not* override this, but :meth:`filter_map_extras()` classes should *not* override this, but :meth:`filter_map_extras()`
instead. instead.
""" """
return self.make_filter_map(exact=['upc', 'status_code'], return self.make_filter_map(exact=['status_code'],
ilike=['brand_name', 'description', 'size']) ilike=['brand_name', 'description', 'size'],
upc=self.filter_gpc(self.row_class.upc))
def filter_config(self): def filter_config(self):
""" """

View file

@ -147,6 +147,9 @@ class SearchableAlchemyGridView(PagedAlchemyGridView):
def filter_ilike_and_soundex(self, field): def filter_ilike_and_soundex(self, field):
return grids.search.filter_ilike_and_soundex(field) return grids.search.filter_ilike_and_soundex(field)
def filter_gpc(self, field):
return grids.search.filter_gpc(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)

View file

@ -123,32 +123,9 @@ class ProductsGrid(SearchableAlchemyGridView):
} }
def filter_map(self): def filter_map(self):
def filter_upc():
def filter_is(q, v):
if not v:
return q
try:
return q.filter(Product.upc.in_((
GPC(v), GPC(v, calc_check_digit='upc'))))
except ValueError:
return q
def filter_not(q, v):
if not v:
return q
try:
return q.filter(~Product.upc.in_((
GPC(v), GPC(v, calc_check_digit='upc'))))
except ValueError:
return q
return {'is': filter_is, 'nt': filter_not}
return self.make_filter_map( return self.make_filter_map(
ilike=['description', 'size'], ilike=['description', 'size'],
upc=filter_upc(), upc=self.filter_gpc(model.Product.upc),
brand=self.filter_ilike(Brand.name), brand=self.filter_ilike(Brand.name),
family=self.filter_ilike(model.Family.name), family=self.filter_ilike(model.Family.name),
department=self.filter_ilike(Department.name), department=self.filter_ilike(Department.name),

View file

@ -36,7 +36,7 @@ from rattail.util import load_object
import formalchemy import formalchemy
from tailbone.db import Session from tailbone.db import Session
from tailbone.views.batch import FileBatchGrid, FileBatchCrud, BatchRowGrid, BatchRowCrud, defaults from tailbone.views.batch import FileBatchGrid, FileBatchCrud, ProductBatchRowGrid, BatchRowCrud, defaults
class VendorCatalogGrid(FileBatchGrid): class VendorCatalogGrid(FileBatchGrid):
@ -140,7 +140,7 @@ class VendorCatalogCrud(FileBatchCrud):
return True return True
class VendorCatalogRowGrid(BatchRowGrid): class VendorCatalogRowGrid(ProductBatchRowGrid):
""" """
Grid view for vendor catalog rows. Grid view for vendor catalog rows.
""" """
@ -148,11 +148,9 @@ class VendorCatalogRowGrid(BatchRowGrid):
route_prefix = 'vendors.catalogs' route_prefix = 'vendors.catalogs'
def filter_map_extras(self): def filter_map_extras(self):
return {'ilike': ['upc', 'brand_name', 'description', 'size', 'vendor_code']} map_ = super(VendorCatalogRowGrid, self).filter_map_extras()
map_.setdefault('ilike', []).append('vendor_code')
def filter_config_extras(self): return map_
return {'filter_label_upc': "UPC",
'filter_label_brand_name': "Brand"}
def configure_grid(self, g): def configure_grid(self, g):
g.configure( g.configure(