tailbone/rattail/pyramid/views/employees.py
2012-08-16 10:31:24 -07:00

98 lines
3.2 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``rattail.pyramid.views.employees`` -- Employee Views
"""
from sqlalchemy import and_
import edbob
from edbob.pyramid.forms import AssociationProxyField
from edbob.pyramid.views import SearchableAlchemyGridView
import rattail
class EmployeesGrid(SearchableAlchemyGridView):
mapped_class = rattail.Employee
config_prefix = 'employees'
sort = 'first_name'
def join_map(self):
return {
'phone':
lambda q: q.outerjoin(rattail.EmployeePhoneNumber, and_(
rattail.EmployeePhoneNumber.parent_uuid == rattail.Employee.uuid,
rattail.EmployeePhoneNumber.preference == 1)),
}
def filter_map(self):
return self.make_filter_map(
first_name=self.filter_ilike(edbob.Person.first_name),
last_name=self.filter_ilike(edbob.Person.last_name),
phone=self.filter_ilike(rattail.EmployeePhoneNumber.number))
def filter_config(self):
return self.make_filter_config(
include_filter_first_name=True,
filter_type_first_name='lk',
include_filter_last_name=True,
filter_type_last_name='lk',
filter_label_phone="Phone Number")
def sort_map(self):
return self.make_sort_map(
first_name=self.sorter(edbob.Person.first_name),
last_name=self.sorter(edbob.Person.last_name),
phone=self.sorter(rattail.EmployeePhoneNumber.number))
def query(self):
q = self.make_query()
q = q.join(edbob.Person)
if not self.request.has_perm('employees.edit'):
q = q.filter(rattail.Employee.status == rattail.EMPLOYEE_STATUS_CURRENT)
return q
def grid(self):
g = self.make_grid()
g.append(AssociationProxyField('first_name'))
g.append(AssociationProxyField('last_name'))
g.configure(
include=[
g.first_name,
g.last_name,
g.phone.label("Phone Number"),
],
readonly=True)
return g
def includeme(config):
config.add_route('employees', '/employees')
config.add_view(EmployeesGrid, route_name='employees',
renderer='/employees/index.mako',
permission='employees.list')