Add products autocomplete view.

This commit is contained in:
Lance Edgar 2015-07-20 09:51:08 -05:00
parent e0cb47d03a
commit d0a977d64b

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2014 Lance Edgar
# Copyright © 2010-2015 Lance Edgar
#
# This file is part of Rattail.
#
@ -29,6 +29,7 @@ from __future__ import unicode_literals
import os
import re
from sqlalchemy import orm
from sqlalchemy import and_
from sqlalchemy.orm import joinedload, aliased
@ -52,7 +53,7 @@ from rattail.db.api import get_product_by_upc
from rattail.db.util import configure_session
from rattail.pod import get_image_url, get_image_path
from tailbone.views import SearchableAlchemyGridView, CrudView
from tailbone.views import SearchableAlchemyGridView, CrudView, AutocompleteView
from tailbone.views.continuum import VersionView, version_defaults
from tailbone.forms import EnumFieldRenderer, DateTimeFieldRenderer
from tailbone.db import Session
@ -344,6 +345,27 @@ class ProductVersionView(VersionView):
return super(ProductVersionView, self).details()
class ProductsAutocomplete(AutocompleteView):
"""
Autocomplete view for products.
"""
mapped_class = model.Product
fieldname = 'description'
def query(self, term):
q = Session.query(model.Product).outerjoin(model.Brand)
q = q.filter(or_(
model.Brand.name.ilike('%{0}%'.format(term)),
model.Product.description.ilike('%{0}%'.format(term))))
if not self.request.has_perm('products.view_deleted'):
q = q.filter(model.Product.deleted == False)
q = q.order_by(model.Brand.name, model.Product.description)
q = q.options(orm.joinedload(model.Product.brand))
return q
def display(self, product):
return product.full_description
def products_search(request):
"""
@ -470,6 +492,7 @@ class CreateProductsBatch(ProductsGrid):
def add_routes(config):
config.add_route('products', '/products')
config.add_route('products.autocomplete', '/products/autocomplete')
config.add_route('products.search', '/products/search')
config.add_route('products.print_labels', '/products/labels')
config.add_route('products.create_batch', '/products/batch')
@ -485,6 +508,11 @@ def includeme(config):
config.add_view(ProductsGrid, route_name='products',
renderer='/products/index.mako',
permission='products.list')
config.add_view(ProductsAutocomplete, route_name='products.autocomplete',
renderer='json',
permission='products.list')
config.add_view(print_labels, route_name='products.print_labels',
renderer='json', permission='products.print_labels')
config.add_view(CreateProductsBatch, route_name='products.create_batch',