tailbone/tailbone/views/employees.py

179 lines
6.5 KiB
Python
Raw Normal View History

2012-07-26 16:04:03 -05:00
#!/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/>.
#
################################################################################
"""
2013-09-01 09:27:47 -05:00
Employee Views
2012-07-26 16:04:03 -05:00
"""
from sqlalchemy import and_
2013-09-01 09:27:47 -05:00
from . import SearchableAlchemyGridView, CrudView
from ..grids.search import EnumSearchFilter
from ..forms import AssociationProxyField, EnumFieldRenderer
from rattail.db.model import (
Employee, EmployeePhoneNumber, EmployeeEmailAddress, Person)
from rattail.enum import EMPLOYEE_STATUS, EMPLOYEE_STATUS_CURRENT
2012-07-26 16:04:03 -05:00
class EmployeesGrid(SearchableAlchemyGridView):
2012-07-26 16:04:03 -05:00
mapped_class = Employee
config_prefix = 'employees'
sort = 'first_name'
2012-07-26 16:04:03 -05:00
2012-08-10 16:18:28 -05:00
def join_map(self):
return {
'phone':
lambda q: q.outerjoin(EmployeePhoneNumber, and_(
EmployeePhoneNumber.parent_uuid == Employee.uuid,
EmployeePhoneNumber.preference == 1)),
'email':
lambda q: q.outerjoin(EmployeeEmailAddress, and_(
EmployeeEmailAddress.parent_uuid == Employee.uuid,
EmployeeEmailAddress.preference == 1)),
2012-08-10 16:18:28 -05:00
}
2012-07-26 16:04:03 -05:00
def filter_map(self):
kwargs = dict(
first_name=self.filter_ilike(Person.first_name),
last_name=self.filter_ilike(Person.last_name),
phone=self.filter_ilike(EmployeePhoneNumber.number),
email=self.filter_ilike(EmployeeEmailAddress.address))
if self.request.has_perm('employees.edit'):
kwargs.update(dict(
exact=['id', 'status']))
return self.make_filter_map(**kwargs)
2012-07-26 16:04:03 -05:00
def filter_config(self):
kwargs = dict(
2012-07-26 16:04:03 -05:00
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",
filter_label_email="Email Address")
if self.request.has_perm('employees.edit'):
kwargs.update(dict(
filter_label_id="ID",
include_filter_status=True,
filter_type_status='is',
filter_factory_status=EnumSearchFilter(EMPLOYEE_STATUS),
status=EMPLOYEE_STATUS_CURRENT))
return self.make_filter_config(**kwargs)
2012-07-26 16:04:03 -05:00
def sort_map(self):
return self.make_sort_map(
first_name=self.sorter(Person.first_name),
last_name=self.sorter(Person.last_name),
phone=self.sorter(EmployeePhoneNumber.number),
email=self.sorter(EmployeeEmailAddress.address))
2012-07-26 16:04:03 -05:00
def query(self):
q = self.make_query()
q = q.join(Person)
2012-07-26 16:04:03 -05:00
if not self.request.has_perm('employees.edit'):
q = q.filter(Employee.status == EMPLOYEE_STATUS_CURRENT)
2012-07-26 16:04:03 -05:00
return q
def grid(self):
g = self.make_grid()
2012-07-26 16:04:03 -05:00
g.append(AssociationProxyField('first_name'))
g.append(AssociationProxyField('last_name'))
g.configure(
include=[
g.id.label("ID"),
2012-07-26 16:04:03 -05:00
g.first_name,
g.last_name,
g.phone.label("Phone Number"),
g.email.label("Email Address"),
g.status.with_renderer(EnumFieldRenderer(EMPLOYEE_STATUS)),
2012-07-26 16:04:03 -05:00
],
readonly=True)
# Hide ID and Status fields for unprivileged users.
if not self.request.has_perm('employees.edit'):
del g.id
del g.status
if self.request.has_perm('employees.read'):
g.viewable = True
g.view_route_name = 'employee.read'
if self.request.has_perm('employees.update'):
g.editable = True
g.edit_route_name = 'employee.update'
if self.request.has_perm('employees.delete'):
g.deletable = True
g.delete_route_name = 'employee.delete'
2012-07-26 16:04:03 -05:00
return g
class EmployeeCrud(CrudView):
mapped_class = Employee
home_route = 'employees'
def fieldset(self, model):
fs = self.make_fieldset(model)
fs.append(AssociationProxyField('first_name'))
fs.append(AssociationProxyField('last_name'))
fs.append(AssociationProxyField('display_name'))
fs.configure(
include=[
fs.id.label("ID"),
fs.first_name,
fs.last_name,
fs.phone.label("Phone Number").readonly(),
fs.email.label("Email Address").readonly(),
fs.status.with_renderer(EnumFieldRenderer(EMPLOYEE_STATUS)),
])
return fs
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')
2012-07-26 16:04:03 -05:00
def includeme(config):
add_routes(config)
config.add_view(EmployeesGrid, route_name='employees',
renderer='/employees/index.mako',
permission='employees.list')
config.add_view(EmployeeCrud, attr='create', route_name='employee.create',
renderer='/employees/crud.mako',
permission='employees.create')
config.add_view(EmployeeCrud, attr='read', route_name='employee.read',
renderer='/employees/crud.mako',
permission='employees.read')
config.add_view(EmployeeCrud, attr='update', route_name='employee.update',
renderer='/employees/crud.mako',
permission='employees.update')
config.add_view(EmployeeCrud, attr='delete', route_name='employee.delete',
permission='employees.delete')