Refactor raw shifts view to use master3
This commit is contained in:
parent
20bae8e54b
commit
2762230691
|
@ -33,27 +33,10 @@ import humanize
|
||||||
from rattail.db import model
|
from rattail.db import model
|
||||||
from rattail.time import localtime
|
from rattail.time import localtime
|
||||||
|
|
||||||
import formalchemy
|
|
||||||
|
|
||||||
from tailbone import forms
|
|
||||||
from tailbone.views import MasterView2 as MasterView
|
from tailbone.views import MasterView2 as MasterView
|
||||||
|
|
||||||
|
|
||||||
class ShiftLengthField(formalchemy.Field):
|
def render_shift_length(shift, 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):
|
|
||||||
if not shift.start_time or not shift.end_time:
|
if not shift.start_time or not shift.end_time:
|
||||||
return ""
|
return ""
|
||||||
if shift.end_time < shift.start_time:
|
if shift.end_time < shift.start_time:
|
||||||
|
@ -76,6 +59,14 @@ class ScheduledShiftsView(MasterView):
|
||||||
'length',
|
'length',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
form_fields = [
|
||||||
|
'employee',
|
||||||
|
'store',
|
||||||
|
'start_time',
|
||||||
|
'end_time',
|
||||||
|
'length',
|
||||||
|
]
|
||||||
|
|
||||||
def configure_grid(self, g):
|
def configure_grid(self, g):
|
||||||
g.joiners['employee'] = lambda q: q.join(model.Employee).join(model.Person)
|
g.joiners['employee'] = lambda q: q.join(model.Employee).join(model.Person)
|
||||||
g.filters['employee'] = g.make_filter('employee', model.Person.display_name,
|
g.filters['employee'] = g.make_filter('employee', model.Person.display_name,
|
||||||
|
@ -88,16 +79,10 @@ class ScheduledShiftsView(MasterView):
|
||||||
|
|
||||||
g.set_label('employee', "Employee Name")
|
g.set_label('employee', "Employee Name")
|
||||||
|
|
||||||
def configure_fieldset(self, fs):
|
def configure_form(self, f):
|
||||||
fs.append(ShiftLengthField('length'))
|
super(ScheduledShiftsView, self).configure_form(f)
|
||||||
fs.configure(
|
|
||||||
include=[
|
f.set_renderer('length', render_shift_length)
|
||||||
fs.employee,
|
|
||||||
fs.store,
|
|
||||||
fs.start_time,
|
|
||||||
fs.end_time,
|
|
||||||
fs.length,
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
class WorkedShiftsView(MasterView):
|
class WorkedShiftsView(MasterView):
|
||||||
|
@ -116,6 +101,14 @@ class WorkedShiftsView(MasterView):
|
||||||
'length',
|
'length',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
form_fields = [
|
||||||
|
'employee',
|
||||||
|
'store',
|
||||||
|
'start_time',
|
||||||
|
'end_time',
|
||||||
|
'length',
|
||||||
|
]
|
||||||
|
|
||||||
def configure_grid(self, g):
|
def configure_grid(self, g):
|
||||||
super(WorkedShiftsView, self).configure_grid(g)
|
super(WorkedShiftsView, self).configure_grid(g)
|
||||||
|
|
||||||
|
@ -145,21 +138,23 @@ class WorkedShiftsView(MasterView):
|
||||||
date = localtime(self.rattail_config, time).date()
|
date = localtime(self.rattail_config, time).date()
|
||||||
return "WorkedShift: {}, {}".format(shift.employee, date)
|
return "WorkedShift: {}, {}".format(shift.employee, date)
|
||||||
|
|
||||||
def _preconfigure_fieldset(self, fs):
|
def configure_form(self, f):
|
||||||
fs.append(ShiftLengthField('length'))
|
super(WorkedShiftsView, self).configure_form(f)
|
||||||
fs.employee.set(readonly=True, renderer=forms.renderers.EmployeeFieldRenderer)
|
|
||||||
|
|
||||||
def configure_fieldset(self, fs):
|
f.set_readonly('employee')
|
||||||
fs.configure(
|
f.set_renderer('employee', self.render_employee)
|
||||||
include=[
|
|
||||||
fs.employee,
|
f.set_renderer('length', render_shift_length)
|
||||||
fs.store,
|
|
||||||
fs.start_time,
|
|
||||||
fs.end_time,
|
|
||||||
fs.length,
|
|
||||||
])
|
|
||||||
if self.editing:
|
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):
|
def includeme(config):
|
||||||
|
|
Loading…
Reference in a new issue