From 69a5eed83bb8d1762ceabfca68060e0fa104c776 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 7 Mar 2015 14:19:19 -0600 Subject: [PATCH] Add shared GPC search filter, use it for product batch rows. --- tailbone/grids/search.py | 30 +++++++++++++++++++++++++++++- tailbone/views/batch.py | 5 +++-- tailbone/views/grids/alchemy.py | 3 +++ tailbone/views/products.py | 25 +------------------------ tailbone/views/vendors/catalogs.py | 12 +++++------- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/tailbone/grids/search.py b/tailbone/grids/search.py index de519628..2b0c01df 100644 --- a/tailbone/grids/search.py +++ b/tailbone/grids/search.py @@ -37,9 +37,11 @@ from pyramid.renderers import render from pyramid_simpleform import Form from pyramid_simpleform.renderers import FormRenderer -from rattail.core import Object from edbob.util import prettify +from rattail.core import Object +from rattail.gpc import GPC + class SearchFilter(Object): """ @@ -228,6 +230,32 @@ def filter_ilike_and_soundex(field): 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): """ Returns a configuration dictionary for a search form. diff --git a/tailbone/views/batch.py b/tailbone/views/batch.py index 5c52a7f8..1252ec00 100644 --- a/tailbone/views/batch.py +++ b/tailbone/views/batch.py @@ -891,8 +891,9 @@ class ProductBatchRowGrid(BatchRowGrid): classes should *not* override this, but :meth:`filter_map_extras()` instead. """ - return self.make_filter_map(exact=['upc', 'status_code'], - ilike=['brand_name', 'description', 'size']) + return self.make_filter_map(exact=['status_code'], + ilike=['brand_name', 'description', 'size'], + upc=self.filter_gpc(self.row_class.upc)) def filter_config(self): """ diff --git a/tailbone/views/grids/alchemy.py b/tailbone/views/grids/alchemy.py index 389f1332..ee564b8d 100644 --- a/tailbone/views/grids/alchemy.py +++ b/tailbone/views/grids/alchemy.py @@ -147,6 +147,9 @@ class SearchableAlchemyGridView(PagedAlchemyGridView): def filter_ilike_and_soundex(self, 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): return grids.search.get_filter_map(self.mapped_class, **kwargs) diff --git a/tailbone/views/products.py b/tailbone/views/products.py index 681d4dd1..7ebc35ad 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -123,32 +123,9 @@ class ProductsGrid(SearchableAlchemyGridView): } 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( ilike=['description', 'size'], - upc=filter_upc(), + upc=self.filter_gpc(model.Product.upc), brand=self.filter_ilike(Brand.name), family=self.filter_ilike(model.Family.name), department=self.filter_ilike(Department.name), diff --git a/tailbone/views/vendors/catalogs.py b/tailbone/views/vendors/catalogs.py index 5f0be4bf..0d5aa8d9 100644 --- a/tailbone/views/vendors/catalogs.py +++ b/tailbone/views/vendors/catalogs.py @@ -36,7 +36,7 @@ from rattail.util import load_object import formalchemy 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): @@ -140,7 +140,7 @@ class VendorCatalogCrud(FileBatchCrud): return True -class VendorCatalogRowGrid(BatchRowGrid): +class VendorCatalogRowGrid(ProductBatchRowGrid): """ Grid view for vendor catalog rows. """ @@ -148,11 +148,9 @@ class VendorCatalogRowGrid(BatchRowGrid): route_prefix = 'vendors.catalogs' def filter_map_extras(self): - return {'ilike': ['upc', 'brand_name', 'description', 'size', 'vendor_code']} - - def filter_config_extras(self): - return {'filter_label_upc': "UPC", - 'filter_label_brand_name': "Brand"} + map_ = super(VendorCatalogRowGrid, self).filter_map_extras() + map_.setdefault('ilike', []).append('vendor_code') + return map_ def configure_grid(self, g): g.configure(