Add shared GPC search filter, use it for product batch rows.
This commit is contained in:
		
							parent
							
								
									51e4eda662
								
							
						
					
					
						commit
						69a5eed83b
					
				
					 5 changed files with 41 additions and 34 deletions
				
			
		|  | @ -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. | ||||
|  |  | |||
|  | @ -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): | ||||
|         """ | ||||
|  |  | |||
|  | @ -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) | ||||
| 
 | ||||
|  |  | |||
|  | @ -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), | ||||
|  |  | |||
							
								
								
									
										12
									
								
								tailbone/views/vendors/catalogs.py
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										12
									
								
								tailbone/views/vendors/catalogs.py
									
										
									
									
										vendored
									
									
								
							|  | @ -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( | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lance Edgar
						Lance Edgar