Improve "employees" list when viewing a department, for buefy themes

This commit is contained in:
Lance Edgar 2021-09-24 19:16:23 -04:00
parent 3b6b1aa5b6
commit ec5ff8a788
2 changed files with 72 additions and 12 deletions

View file

@ -3,7 +3,7 @@
<%def name="page_content()">
${parent.page_content()}
% if not use_buefy:
<h2>Employees</h2>
% if employees:
@ -12,7 +12,16 @@
% else:
<p>No employees are assigned to this department.</p>
% endif
% endif
</%def>
<%def name="modify_this_page_vars()">
${parent.modify_this_page_vars()}
<script type="text/javascript">
${form.component_studly}Data.employeesData = ${json.dumps(employees_data)|n}
</script>
</%def>
${parent.body()}

View file

@ -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):