Expose user reference(s) for employees

This commit is contained in:
Lance Edgar 2020-08-09 14:39:31 -05:00
parent 163134326a
commit ca31af196f
2 changed files with 33 additions and 2 deletions

View file

@ -60,6 +60,7 @@ class EmployeesView(MasterView):
'phone', 'phone',
'email', 'email',
'status', 'status',
'username',
] ]
form_fields = [ form_fields = [
@ -73,6 +74,7 @@ class EmployeesView(MasterView):
'full_time', 'full_time',
'full_time_start', 'full_time_start',
'id', 'id',
'users',
'stores', 'stores',
'departments', 'departments',
] ]
@ -90,15 +92,25 @@ class EmployeesView(MasterView):
factory=grids.filters.AlchemyPhoneNumberFilter) factory=grids.filters.AlchemyPhoneNumberFilter)
g.set_sorter('phone', model.EmployeePhoneNumber.number) g.set_sorter('phone', model.EmployeePhoneNumber.number)
# email
g.joiners['email'] = lambda q: q.outerjoin(model.EmployeeEmailAddress, sa.and_( g.joiners['email'] = lambda q: q.outerjoin(model.EmployeeEmailAddress, sa.and_(
model.EmployeeEmailAddress.parent_uuid == model.Employee.uuid, model.EmployeeEmailAddress.parent_uuid == model.Employee.uuid,
model.EmployeeEmailAddress.preference == 1)) 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['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['last_name'] = g.make_filter('last_name', model.Person.last_name)
g.filters['email'] = g.make_filter('email', model.EmployeeEmailAddress.address, # username
label="Email Address") 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 # id
if self.request.has_perm('{}.edit'.format(route_prefix)): 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) q = q.filter(model.Employee.status == self.enum.EMPLOYEE_STATUS_CURRENT)
return q 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): def grid_extra_class(self, employee, i):
if employee.status == self.enum.EMPLOYEE_STATUS_FORMER: if employee.status == self.enum.EMPLOYEE_STATUS_FORMER:
return 'warning' return 'warning'
@ -161,6 +179,7 @@ class EmployeesView(MasterView):
employee = f.model_instance employee = f.model_instance
f.set_renderer('person', self.render_person) f.set_renderer('person', self.render_person)
f.set_renderer('users', self.render_users)
f.set_renderer('stores', self.render_stores) f.set_renderer('stores', self.render_stores)
f.set_label('stores', "Stores") # TODO: should not be necessary f.set_label('stores', "Stores") # TODO: should not be necessary

View file

@ -947,6 +947,18 @@ class MasterView(View):
url = self.request.route_url('users.view', uuid=user.uuid) url = self.request.route_url('users.view', uuid=user.uuid)
return tags.link_to(text, url) 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): def render_customer(self, obj, field):
customer = getattr(obj, field) customer = getattr(obj, field)
if not customer: if not customer: