From 685b391dd29e885987dddaae2275ca229fef1870 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 20 Sep 2013 14:54:27 -0700 Subject: [PATCH] Added `products.search` route/view. This is for simple AJAX uses. --- tailbone/views/products.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tailbone/views/products.py b/tailbone/views/products.py index d4ac3b30..c9fb6663 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -47,6 +47,7 @@ from rattail.db.model import ( Product, ProductPrice, ProductCost, ProductCode, Brand, Vendor, Department, Subdepartment, LabelProfile) from rattail.gpc import GPC +from rattail.db.api import get_product_by_upc from ..db import Session from ..forms import AutocompleteFieldRenderer, GPCFieldRenderer, PriceFieldRenderer @@ -250,6 +251,30 @@ class ProductCrud(CrudView): return fs +def products_search(request): + """ + Locate a product(s) by UPC. + + Eventually this should be more generic, or at least offer more fields for + search. For now it operates only on the ``Product.upc`` field. + """ + + product = None + upc = request.GET.get('upc') + if upc: + product = get_product_by_upc(Session, upc) + if not product: + # Try again, assuming caller did not include check digit. + upc = GPC(upc, calc_check_digit='upc') + product = get_product_by_upc(Session, upc) + if product: + product = { + 'uuid': product.uuid, + 'full_description': product.full_description, + } + return {'product': product} + + def print_labels(request): profile = request.params.get('profile') profile = Session.query(LabelProfile).get(profile) if profile else None @@ -346,6 +371,7 @@ class CreateProductsBatch(ProductsGrid): def add_routes(config): config.add_route('products', '/products') + config.add_route('products.search', '/products/search') config.add_route('products.print_labels', '/products/labels') config.add_route('products.create_batch', '/products/batch') config.add_route('product.create', '/products/new') @@ -376,3 +402,5 @@ def includeme(config): permission='products.update') config.add_view(ProductCrud, attr='delete', route_name='product.delete', permission='products.delete') + config.add_view(products_search, route_name='products.search', + renderer='json', permission='products.list')