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
|
|
|
"""
|
|
|
|
|
2012-08-06 17:05:09 -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
|
2013-05-21 23:51:41 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
2012-08-06 17:05:09 -05:00
|
|
|
class EmployeesGrid(SearchableAlchemyGridView):
|
2012-07-26 16:04:03 -05:00
|
|
|
|
2013-05-21 23:51:41 -05:00
|
|
|
mapped_class = Employee
|
2012-08-16 12:31:24 -05:00
|
|
|
config_prefix = 'employees'
|
2012-08-06 17:05:09 -05:00
|
|
|
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':
|
2013-05-21 23:51:41 -05:00
|
|
|
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):
|
2013-05-07 20:01:58 -05:00
|
|
|
kwargs = dict(
|
2013-05-21 23:51:41 -05:00
|
|
|
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))
|
2013-05-07 20:01:58 -05:00
|
|
|
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
|
|
|
|
2012-08-06 17:05:09 -05:00
|
|
|
def filter_config(self):
|
2013-05-07 20:01:58 -05:00
|
|
|
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,
|
2012-08-06 17:05:09 -05:00
|
|
|
filter_type_last_name='lk',
|
2013-05-21 23:51:41 -05:00
|
|
|
filter_label_phone="Phone Number",
|
|
|
|
filter_label_email="Email Address")
|
2013-05-07 20:01:58 -05:00
|
|
|
if self.request.has_perm('employees.edit'):
|
|
|
|
kwargs.update(dict(
|
|
|
|
filter_label_id="ID",
|
|
|
|
include_filter_status=True,
|
|
|
|
filter_type_status='is',
|
2013-05-21 23:51:41 -05:00
|
|
|
filter_factory_status=EnumSearchFilter(EMPLOYEE_STATUS),
|
|
|
|
status=EMPLOYEE_STATUS_CURRENT))
|
2013-05-07 20:01:58 -05:00
|
|
|
return self.make_filter_config(**kwargs)
|
2012-07-26 16:04:03 -05:00
|
|
|
|
|
|
|
def sort_map(self):
|
|
|
|
return self.make_sort_map(
|
2013-05-21 23:51:41 -05:00
|
|
|
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
|
|
|
|
2012-08-06 17:05:09 -05:00
|
|
|
def query(self):
|
|
|
|
q = self.make_query()
|
2013-05-21 23:51:41 -05:00
|
|
|
q = q.join(Person)
|
2012-07-26 16:04:03 -05:00
|
|
|
if not self.request.has_perm('employees.edit'):
|
2013-05-21 23:51:41 -05:00
|
|
|
q = q.filter(Employee.status == EMPLOYEE_STATUS_CURRENT)
|
2012-07-26 16:04:03 -05:00
|
|
|
return q
|
|
|
|
|
2012-08-06 17:05:09 -05:00
|
|
|
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=[
|
2013-05-07 20:01:58 -05:00
|
|
|
g.id.label("ID"),
|
2012-07-26 16:04:03 -05:00
|
|
|
g.first_name,
|
|
|
|
g.last_name,
|
2012-08-06 17:05:09 -05:00
|
|
|
g.phone.label("Phone Number"),
|
2013-05-21 23:51:41 -05:00
|
|
|
g.email.label("Email Address"),
|
|
|
|
g.status.with_renderer(EnumFieldRenderer(EMPLOYEE_STATUS)),
|
2012-07-26 16:04:03 -05:00
|
|
|
],
|
|
|
|
readonly=True)
|
2013-05-21 23:51:41 -05:00
|
|
|
|
|
|
|
# Hide ID and Status fields for unprivileged users.
|
2013-05-07 20:01:58 -05:00
|
|
|
if not self.request.has_perm('employees.edit'):
|
|
|
|
del g.id
|
|
|
|
del g.status
|
2013-05-21 23:51:41 -05:00
|
|
|
|
|
|
|
if self.request.has_perm('employees.read'):
|
2013-07-22 23:32:54 -05:00
|
|
|
g.viewable = True
|
|
|
|
g.view_route_name = 'employee.read'
|
2013-05-21 23:51:41 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2013-05-21 23:51:41 -05:00
|
|
|
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):
|
2013-05-21 23:51:41 -05:00
|
|
|
add_routes(config)
|
2012-08-16 12:31:24 -05:00
|
|
|
|
|
|
|
config.add_view(EmployeesGrid, route_name='employees',
|
|
|
|
renderer='/employees/index.mako',
|
|
|
|
permission='employees.list')
|
2013-05-21 23:51:41 -05:00
|
|
|
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')
|