diff --git a/tailbone/views/employees.py b/tailbone/views/employees.py index 488e1f65..dac93e67 100644 --- a/tailbone/views/employees.py +++ b/tailbone/views/employees.py @@ -60,6 +60,7 @@ class EmployeesView(MasterView): 'phone', 'email', 'status', + 'username', ] form_fields = [ @@ -73,6 +74,7 @@ class EmployeesView(MasterView): 'full_time', 'full_time_start', 'id', + 'users', 'stores', 'departments', ] @@ -90,15 +92,25 @@ class EmployeesView(MasterView): factory=grids.filters.AlchemyPhoneNumberFilter) g.set_sorter('phone', model.EmployeePhoneNumber.number) + # email g.joiners['email'] = lambda q: q.outerjoin(model.EmployeeEmailAddress, sa.and_( model.EmployeeEmailAddress.parent_uuid == model.Employee.uuid, model.EmployeeEmailAddress.preference == 1)) + g.filters['email'] = g.make_filter('email', model.EmployeeEmailAddress.address, + label="Email Address") + # first/last name g.filters['first_name'] = g.make_filter('first_name', model.Person.first_name) g.filters['last_name'] = g.make_filter('last_name', model.Person.last_name) - g.filters['email'] = g.make_filter('email', model.EmployeeEmailAddress.address, - label="Email Address") + # username + if self.request.has_perm('users.view'): + g.set_joiner('username', lambda q: q.outerjoin(model.User)) + g.set_filter('username', model.User.username) + g.set_sorter('username', model.User.username) + g.set_renderer('username', self.grid_render_username) + else: + g.hide_column('username') # id if self.request.has_perm('{}.edit'.format(route_prefix)): @@ -142,6 +154,12 @@ class EmployeesView(MasterView): q = q.filter(model.Employee.status == self.enum.EMPLOYEE_STATUS_CURRENT) return q + def grid_render_username(self, employee, field): + person = employee.person if employee else None + if not person: + return "" + return ", ".join([u.username for u in person.users]) + def grid_extra_class(self, employee, i): if employee.status == self.enum.EMPLOYEE_STATUS_FORMER: return 'warning' @@ -161,6 +179,7 @@ class EmployeesView(MasterView): employee = f.model_instance f.set_renderer('person', self.render_person) + f.set_renderer('users', self.render_users) f.set_renderer('stores', self.render_stores) f.set_label('stores', "Stores") # TODO: should not be necessary diff --git a/tailbone/views/master.py b/tailbone/views/master.py index d4850ef4..10bd5c26 100644 --- a/tailbone/views/master.py +++ b/tailbone/views/master.py @@ -947,6 +947,18 @@ class MasterView(View): url = self.request.route_url('users.view', uuid=user.uuid) return tags.link_to(text, url) + def render_users(self, obj, field): + users = obj.users + if not users: + return "" + + items = [] + for user in users: + text = user.username + url = self.request.route_url('users.view', uuid=user.uuid) + items.append(HTML.tag('li', c=[tags.link_to(text, url)])) + return HTML.tag('ul', c=items) + def render_customer(self, obj, field): customer = getattr(obj, field) if not customer: