From 27622306910b597e122f2fe898d91f948be1ec28 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 4 Dec 2017 21:09:44 -0600 Subject: [PATCH] Refactor raw shifts view to use master3 --- tailbone/views/shifts/core.py | 77 ++++++++++++++++------------------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/tailbone/views/shifts/core.py b/tailbone/views/shifts/core.py index ce9f4350..5ffee96c 100644 --- a/tailbone/views/shifts/core.py +++ b/tailbone/views/shifts/core.py @@ -33,27 +33,10 @@ import humanize from rattail.db import model from rattail.time import localtime -import formalchemy - -from tailbone import forms from tailbone.views import MasterView2 as MasterView -class ShiftLengthField(formalchemy.Field): - - def __init__(self, name, **kwargs): - kwargs.setdefault('value', self.shift_length) - super(ShiftLengthField, self).__init__(name, **kwargs) - - def shift_length(self, shift): - if not shift.start_time or not shift.end_time: - return - if shift.end_time < shift.start_time: - return "??" - return humanize.naturaldelta(shift.end_time - shift.start_time) - - -def render_shift_length(shift, column): +def render_shift_length(shift, field): if not shift.start_time or not shift.end_time: return "" if shift.end_time < shift.start_time: @@ -76,6 +59,14 @@ class ScheduledShiftsView(MasterView): 'length', ] + form_fields = [ + 'employee', + 'store', + 'start_time', + 'end_time', + 'length', + ] + def configure_grid(self, g): g.joiners['employee'] = lambda q: q.join(model.Employee).join(model.Person) g.filters['employee'] = g.make_filter('employee', model.Person.display_name, @@ -88,16 +79,10 @@ class ScheduledShiftsView(MasterView): g.set_label('employee', "Employee Name") - def configure_fieldset(self, fs): - fs.append(ShiftLengthField('length')) - fs.configure( - include=[ - fs.employee, - fs.store, - fs.start_time, - fs.end_time, - fs.length, - ]) + def configure_form(self, f): + super(ScheduledShiftsView, self).configure_form(f) + + f.set_renderer('length', render_shift_length) class WorkedShiftsView(MasterView): @@ -116,6 +101,14 @@ class WorkedShiftsView(MasterView): 'length', ] + form_fields = [ + 'employee', + 'store', + 'start_time', + 'end_time', + 'length', + ] + def configure_grid(self, g): super(WorkedShiftsView, self).configure_grid(g) @@ -145,21 +138,23 @@ class WorkedShiftsView(MasterView): date = localtime(self.rattail_config, time).date() return "WorkedShift: {}, {}".format(shift.employee, date) - def _preconfigure_fieldset(self, fs): - fs.append(ShiftLengthField('length')) - fs.employee.set(readonly=True, renderer=forms.renderers.EmployeeFieldRenderer) + def configure_form(self, f): + super(WorkedShiftsView, self).configure_form(f) - def configure_fieldset(self, fs): - fs.configure( - include=[ - fs.employee, - fs.store, - fs.start_time, - fs.end_time, - fs.length, - ]) + f.set_readonly('employee') + f.set_renderer('employee', self.render_employee) + + f.set_renderer('length', render_shift_length) if self.editing: - del fs.length + f.remove('length') + + def render_employee(self, shift, field): + employee = shift.employee + if not employee: + return "" + text = six.text_type(employee) + url = self.request.route_url('employees.view', uuid=employee.uuid) + return tags.link_to(text, url) def includeme(config):