Avoid deprecated pretty_hours() function

This commit is contained in:
Lance Edgar 2023-10-04 11:56:50 -05:00
parent 0b7791070f
commit f3dddf0e40
3 changed files with 29 additions and 25 deletions

View file

@ -32,7 +32,7 @@ import sqlalchemy as sa
from sqlalchemy import orm from sqlalchemy import orm
from rattail.db.types import GPCType from rattail.db.types import GPCType
from rattail.util import prettify, pretty_boolean, pretty_quantity, pretty_hours from rattail.util import prettify, pretty_boolean, pretty_quantity
from rattail.time import localtime from rattail.time import localtime
import webhelpers2_grid import webhelpers2_grid
@ -541,7 +541,8 @@ class Grid(object):
value = self.obtain_value(obj, field) value = self.obtain_value(obj, field)
if value is None: if value is None:
return "" return ""
return pretty_hours(hours=value) app = self.request.rattail_config.get_app()
return app.render_duration(hours=value)
def set_url(self, url): def set_url(self, url):
self.url = url self.url = url

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2022 Lance Edgar # Copyright © 2010-2023 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -24,31 +24,32 @@
Views for employee shifts Views for employee shifts
""" """
from __future__ import unicode_literals, absolute_import
import datetime import datetime
import six
from rattail.db import model from rattail.db import model
from rattail.time import localtime from rattail.time import localtime
from rattail.util import pretty_hours, hours_as_decimal from rattail.util import hours_as_decimal
from webhelpers2.html import tags, HTML from webhelpers2.html import tags, HTML
from tailbone.views import MasterView from tailbone.views import MasterView
def render_shift_length(shift, field): class ShiftViewMixin:
if not shift.start_time or not shift.end_time:
return "" def render_shift_length(self, shift, field):
if shift.end_time < shift.start_time: if not shift.start_time or not shift.end_time:
return "??" return ""
length = shift.end_time - shift.start_time if shift.end_time < shift.start_time:
return HTML.tag('span', title="{} hrs".format(hours_as_decimal(length)), c=[pretty_hours(length)]) return "??"
app = self.get_rattail_app()
length = shift.end_time - shift.start_time
return HTML.tag('span',
title="{} hrs".format(hours_as_decimal(length)),
c=[app.render_duration(delta=length)])
class ScheduledShiftView(MasterView): class ScheduledShiftView(MasterView, ShiftViewMixin):
""" """
Master view for employee scheduled shifts. Master view for employee scheduled shifts.
""" """
@ -78,20 +79,20 @@ class ScheduledShiftView(MasterView):
g.set_sort_defaults('start_time', 'desc') g.set_sort_defaults('start_time', 'desc')
g.set_renderer('length', render_shift_length) g.set_renderer('length', self.render_shift_length)
g.set_label('employee', "Employee Name") g.set_label('employee', "Employee Name")
def configure_form(self, f): def configure_form(self, f):
super(ScheduledShiftView, self).configure_form(f) super(ScheduledShiftView, self).configure_form(f)
f.set_renderer('length', render_shift_length) f.set_renderer('length', self.render_shift_length)
# TODO: deprecate / remove this # TODO: deprecate / remove this
ScheduledShiftsView = ScheduledShiftView ScheduledShiftsView = ScheduledShiftView
class WorkedShiftView(MasterView): class WorkedShiftView(MasterView, ShiftViewMixin):
""" """
Master view for employee worked shifts. Master view for employee worked shifts.
""" """
@ -136,7 +137,7 @@ class WorkedShiftView(MasterView):
# (but we'll still have to set this) # (but we'll still have to set this)
g.set_sort_defaults('start_time', 'desc') g.set_sort_defaults('start_time', 'desc')
g.set_renderer('length', render_shift_length) g.set_renderer('length', self.render_shift_length)
g.set_label('employee', "Employee Name") g.set_label('employee', "Employee Name")
g.set_label('store', "Store Name") g.set_label('store', "Store Name")
@ -154,7 +155,7 @@ class WorkedShiftView(MasterView):
f.set_readonly('employee') f.set_readonly('employee')
f.set_renderer('employee', self.render_employee) f.set_renderer('employee', self.render_employee)
f.set_renderer('length', render_shift_length) f.set_renderer('length', self.render_shift_length)
if self.editing: if self.editing:
f.remove('length') f.remove('length')
@ -162,7 +163,7 @@ class WorkedShiftView(MasterView):
employee = shift.employee employee = shift.employee
if not employee: if not employee:
return "" return ""
text = six.text_type(employee) text = str(employee)
url = self.request.route_url('employees.view', uuid=employee.uuid) url = self.request.route_url('employees.view', uuid=employee.uuid)
return tags.link_to(text, url) return tags.link_to(text, url)

View file

@ -31,7 +31,7 @@ import sqlalchemy as sa
from rattail import enum from rattail import enum
from rattail.db import model, api from rattail.db import model, api
from rattail.time import localtime, make_utc, get_sunday from rattail.time import localtime, make_utc, get_sunday
from rattail.util import pretty_hours, hours_as_decimal from rattail.util import hours_as_decimal
import colander import colander
from deform import widget as dfwidget from deform import widget as dfwidget
@ -401,6 +401,8 @@ class TimeSheetView(View):
Fetch all shift data of the given model class (``cls``), according to Fetch all shift data of the given model class (``cls``), according to
the given params. The cached shift data is attached to each employee. the given params. The cached shift data is attached to each employee.
""" """
app = self.get_rattail_app()
# TODO: a bit hacky, this? display hours as HH:MM by default, but # TODO: a bit hacky, this? display hours as HH:MM by default, but
# check config in order to display as HH.HH for certain users # check config in order to display as HH.HH for certain users
hours_style = 'pretty' hours_style = 'pretty'
@ -465,7 +467,7 @@ class TimeSheetView(View):
hours = empday['{}_hours'.format(shift_type)] hours = empday['{}_hours'.format(shift_type)]
if hours: if hours:
if hours_style == 'pretty': if hours_style == 'pretty':
display = pretty_hours(hours) display = app.render_duration(hours=hours)
else: # decimal else: # decimal
display = str(hours_as_decimal(hours)) display = str(hours_as_decimal(hours))
if empday['hours_incomplete']: if empday['hours_incomplete']:
@ -476,7 +478,7 @@ class TimeSheetView(View):
hours = getattr(employee, '{}_hours'.format(shift_type)) hours = getattr(employee, '{}_hours'.format(shift_type))
if hours: if hours:
if hours_style == 'pretty': if hours_style == 'pretty':
display = pretty_hours(hours) display = app.render_duration(hours=hours)
else: # decimal else: # decimal
display = str(hours_as_decimal(hours)) display = str(hours_as_decimal(hours))
if hours_incomplete: if hours_incomplete: