Improve "length" (hours) column for Worked Shifts grid

web display shows "pretty" hours (e.g. 7:30) whereas the Excel export shows
"decimal" hours (7.50)
This commit is contained in:
Lance Edgar 2018-10-09 16:19:55 -05:00
parent 94ba18eaee
commit f36c1fbc3f

View file

@ -29,10 +29,10 @@ from __future__ import unicode_literals, absolute_import
import datetime
import six
import humanize
from rattail.db import model
from rattail.time import localtime
from rattail.util import pretty_hours, hours_as_decimal
from webhelpers2.html import tags
@ -44,7 +44,7 @@ def render_shift_length(shift, field):
return ""
if shift.end_time < shift.start_time:
return "??"
return humanize.naturaldelta(shift.end_time - shift.start_time)
return pretty_hours(shift.end_time - shift.start_time)
class ScheduledShiftsView(MasterView):
@ -169,20 +169,34 @@ class WorkedShiftsView(MasterView):
i = fields.index('employee_uuid')
fields.insert(i + 1, 'employee_name')
# add hours
fields.append('hours')
return fields
def get_xlsx_row(self, shift, fields):
row = super(WorkedShiftsView, self).get_xlsx_row(shift, fields)
# add custom fields
row['employee_name'] = shift.employee.person.display_name
# localize start and end times (Excel requires time with no zone)
if shift.punch_in:
row['punch_in'] = localtime(self.rattail_config, shift.punch_in, from_utc=True, tzinfo=False)
if shift.punch_out:
row['punch_out'] = localtime(self.rattail_config, shift.punch_out, from_utc=True, tzinfo=False)
# add employee name
row['employee_name'] = shift.employee.person.display_name
# add hours
if shift.punch_in and shift.punch_out:
if shift.punch_in <= shift.punch_out:
row['hours'] = hours_as_decimal(shift.punch_out - shift.punch_in)
else:
row['hours'] = "??"
elif shift.punch_in or shift.punch_out:
row['hours'] = "??"
else:
row['hours'] = None
return row