From ec5ff8a788e20faf8cd6dbed4ee93e1bece0c850 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 24 Sep 2021 19:16:23 -0400 Subject: [PATCH] Improve "employees" list when viewing a department, for buefy themes --- tailbone/templates/departments/view.mako | 11 +++- tailbone/views/departments.py | 73 ++++++++++++++++++++---- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/tailbone/templates/departments/view.mako b/tailbone/templates/departments/view.mako index f3887af7..20c2a266 100644 --- a/tailbone/templates/departments/view.mako +++ b/tailbone/templates/departments/view.mako @@ -3,7 +3,7 @@ <%def name="page_content()"> ${parent.page_content()} - + % if not use_buefy:

Employees

% if employees: @@ -12,7 +12,16 @@ % else:

No employees are assigned to this department.

% endif + % endif +<%def name="modify_this_page_vars()"> + ${parent.modify_this_page_vars()} + + ${parent.body()} diff --git a/tailbone/views/departments.py b/tailbone/views/departments.py index d242b51d..d66a3f22 100644 --- a/tailbone/views/departments.py +++ b/tailbone/views/departments.py @@ -31,6 +31,7 @@ import six from rattail.db import model from deform import widget as dfwidget +from webhelpers2.html import HTML from tailbone import grids from tailbone.views import MasterView, AutocompleteView @@ -59,6 +60,7 @@ class DepartmentView(MasterView): 'personnel', 'exempt_from_gross_sales', 'allow_product_deletions', + 'employees', ] def configure_grid(self, g): @@ -73,23 +75,72 @@ class DepartmentView(MasterView): def configure_form(self, f): super(DepartmentView, self).configure_form(f) + use_buefy = self.get_use_buefy() + f.remove_field('subdepartments') - f.remove_field('employees') + + if not use_buefy or self.creating or self.editing: + f.remove('employees') + else: + f.set_renderer('employees', self.render_employees) + f.set_type('product', 'boolean') f.set_type('personnel', 'boolean') + def render_employees(self, department, field): + route_prefix = self.get_route_prefix() + permission_prefix = self.get_permission_prefix() + + factory = self.get_grid_factory() + g = factory( + key='{}.employees'.format(route_prefix), + data=[], + columns=[ + 'first_name', + 'last_name', + ], + sortable=True, + sorters={'first_name': True, 'last_name': True}, + ) + + if self.request.has_perm('employees.view'): + g.main_actions.append(self.make_action('view', icon='eye')) + if self.request.has_perm('employees.edit'): + g.main_actions.append(self.make_action('edit', icon='edit')) + + return HTML.literal( + g.render_buefy_table_element(data_prop='employeesData')) + def template_kwargs_view(self, **kwargs): + kwargs = super(DepartmentView, self).template_kwargs_view(**kwargs) + use_buefy = self.get_use_buefy() department = kwargs['instance'] - if department.employees: - employees = sorted(department.employees, key=six.text_type) - actions = [ - grids.GridAction('view', icon='zoomin', - url=lambda r, i: self.request.route_url('employees.view', uuid=r.uuid)) - ] - kwargs['employees'] = grids.Grid(None, employees, ['display_name'], request=self.request, - model_class=model.Employee, main_actions=actions) - else: - kwargs['employees'] = None + department_employees = sorted(department.employees, key=six.text_type) + + if use_buefy: + employees = [] + for employee in department_employees: + person = employee.person + employees.append({ + 'uuid': employee.uuid, + 'first_name': person.first_name, + 'last_name': person.last_name, + '_action_url_view': self.request.route_url('employees.view', uuid=employee.uuid), + '_action_url_edit': self.request.route_url('employees.edit', uuid=employee.uuid), + }) + kwargs['employees_data'] = employees + + else: # not buefy + if department.employees: + actions = [ + grids.GridAction('view', icon='zoomin', + url=lambda r, i: self.request.route_url('employees.view', uuid=r.uuid)) + ] + kwargs['employees'] = grids.Grid(None, department_employees, ['display_name'], request=self.request, + model_class=model.Employee, main_actions=actions) + else: + kwargs['employees'] = None + return kwargs def before_delete(self, department):