From 6db88edb6882ec308f9372c002b047cf9cb2fed6 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 11 Apr 2015 00:23:31 -0500 Subject: [PATCH] Add autocomplete view for current employees. --- tailbone/views/employees.py | 45 +++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/tailbone/views/employees.py b/tailbone/views/employees.py index 042c3a50..3b630f51 100644 --- a/tailbone/views/employees.py +++ b/tailbone/views/employees.py @@ -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-2015 Lance Edgar # # This file is part of Rattail. # @@ -21,13 +20,19 @@ # along with Rattail. If not, see . # ################################################################################ - """ Employee Views """ +from __future__ import unicode_literals + from sqlalchemy import and_ +from rattail.db import model +from rattail import enum + +from tailbone.views import AutocompleteView + from . import SearchableAlchemyGridView, CrudView from ..grids.search import EnumSearchFilter from ..forms import AssociationProxyField, EnumFieldRenderer @@ -36,6 +41,7 @@ from rattail.db.model import ( from rattail.enum import EMPLOYEE_STATUS, EMPLOYEE_STATUS_CURRENT + class EmployeesGrid(SearchableAlchemyGridView): mapped_class = Employee @@ -151,12 +157,29 @@ class EmployeeCrud(CrudView): return fs +class EmployeesAutocomplete(AutocompleteView): + """ + Autocomplete view for the Employee model, but restricted to return only + results for current employees. + """ + mapped_class = model.Person + fieldname = 'display_name' + + def filter_query(self, q): + return q.join(model.Employee)\ + .filter(model.Employee.status == enum.EMPLOYEE_STATUS_CURRENT) + + def value(self, person): + return person.employee.uuid + + def add_routes(config): - config.add_route('employees', '/employees') - config.add_route('employee.create', '/employees/new') - config.add_route('employee.read', '/employees/{uuid}') - config.add_route('employee.update', '/employees/{uuid}/edit') - config.add_route('employee.delete', '/employees/{uuid}/delete') + config.add_route('employees', '/employees') + config.add_route('employee.create', '/employees/new') + config.add_route('employees.autocomplete', '/employees/autocomplete') + config.add_route('employee.read', '/employees/{uuid}') + config.add_route('employee.update', '/employees/{uuid}/edit') + config.add_route('employee.delete', '/employees/{uuid}/delete') def includeme(config): @@ -176,3 +199,7 @@ def includeme(config): permission='employees.update') config.add_view(EmployeeCrud, attr='delete', route_name='employee.delete', permission='employees.delete') + + # autocomplete + config.add_view(EmployeesAutocomplete, route_name='employees.autocomplete', + renderer='json', permission='employees.list')