Add customer phone autocomplete and customer "info" AJAX view.

This autocomplete view is a little different than the typical ones used
prior, and required some refactoring of the base autocomplete view as well
as the autocomplete template.
This commit is contained in:
Lance Edgar 2014-07-13 12:43:58 -07:00
parent bfd1b034ee
commit f9d22f59f2
4 changed files with 125 additions and 32 deletions

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar
# Copyright © 2010-2014 Lance Edgar
#
# This file is part of Rattail.
#
@ -26,14 +25,20 @@
Autocomplete View
"""
from .core import View
from ..db import Session
__all__ = ['AutocompleteView']
from tailbone.views.core import View
from tailbone.db import Session
class AutocompleteView(View):
"""
Base class for generic autocomplete views.
"""
def prepare_term(self, term):
"""
If necessary, massage the incoming search term for use with the query.
"""
return term
def filter_query(self, q):
return q
@ -51,11 +56,21 @@ class AutocompleteView(View):
def display(self, instance):
return getattr(instance, self.fieldname)
def value(self, instance):
"""
Determine the data value for a query result instance.
"""
return instance.uuid
def __call__(self):
term = self.request.params.get('term')
"""
View implementation.
"""
term = self.request.params.get(u'term') or u''
term = term.strip()
if term:
term = term.strip()
term = self.prepare_term(term)
if not term:
return []
results = self.query(term).all()
return [{'label': self.display(x), 'value': x.uuid} for x in results]
return [{u'label': self.display(x), u'value': self.value(x)} for x in results]