2017-07-07 09:13:53 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
2016-04-25 19:40:29 -05:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2017-07-06 23:47:56 -05:00
|
|
|
# Copyright © 2010-2017 Lance Edgar
|
2016-04-25 19:40:29 -05:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
2017-07-06 23:47:56 -05:00
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
2016-04-25 19:40:29 -05:00
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2017-07-06 23:47:56 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2016-04-25 19:40:29 -05:00
|
|
|
#
|
2017-07-06 23:47:56 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
2016-04-25 19:40:29 -05:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Views for employee shifts
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals, absolute_import
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
import humanize
|
|
|
|
|
|
|
|
from rattail.db import model
|
2017-02-04 12:32:21 -06:00
|
|
|
from rattail.time import localtime
|
2016-04-25 19:40:29 -05:00
|
|
|
|
|
|
|
import formalchemy
|
|
|
|
|
2017-02-04 12:32:21 -06:00
|
|
|
from tailbone import forms
|
2017-07-07 09:13:53 -05:00
|
|
|
from tailbone.views import MasterView2 as MasterView
|
2016-04-25 19:40:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2016-05-10 13:08:32 -05:00
|
|
|
if not shift.start_time or not shift.end_time:
|
2016-04-25 19:40:29 -05:00
|
|
|
return
|
2016-05-10 13:08:32 -05:00
|
|
|
if shift.end_time < shift.start_time:
|
2016-04-25 19:40:29 -05:00
|
|
|
return "??"
|
2016-05-10 13:08:32 -05:00
|
|
|
return humanize.naturaldelta(shift.end_time - shift.start_time)
|
2016-04-25 19:40:29 -05:00
|
|
|
|
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
def render_shift_length(shift, column):
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2016-04-28 18:16:26 -05:00
|
|
|
class ScheduledShiftsView(MasterView):
|
|
|
|
"""
|
|
|
|
Master view for employee scheduled shifts.
|
|
|
|
"""
|
|
|
|
model_class = model.ScheduledShift
|
|
|
|
url_prefix = '/shifts/scheduled'
|
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
grid_columns = [
|
|
|
|
'employee',
|
|
|
|
'store',
|
|
|
|
'start_time',
|
|
|
|
'end_time',
|
|
|
|
'length',
|
|
|
|
]
|
|
|
|
|
2016-04-28 18:16:26 -05:00
|
|
|
def configure_grid(self, g):
|
2016-10-12 14:16:06 -05:00
|
|
|
g.joiners['employee'] = lambda q: q.join(model.Employee).join(model.Person)
|
|
|
|
g.filters['employee'] = g.make_filter('employee', model.Person.display_name,
|
2017-07-07 09:13:53 -05:00
|
|
|
default_active=True, default_verb='contains')
|
2016-10-12 14:16:06 -05:00
|
|
|
|
2016-04-28 18:16:26 -05:00
|
|
|
g.default_sortkey = 'start_time'
|
|
|
|
g.default_sortdir = 'desc'
|
2017-07-07 09:13:53 -05:00
|
|
|
|
|
|
|
g.set_renderer('length', render_shift_length)
|
|
|
|
|
|
|
|
g.set_label('employee', "Employee Name")
|
2016-04-28 18:16:26 -05:00
|
|
|
|
|
|
|
def configure_fieldset(self, fs):
|
2016-05-10 13:08:32 -05:00
|
|
|
fs.append(ShiftLengthField('length'))
|
2016-04-28 18:16:26 -05:00
|
|
|
fs.configure(
|
|
|
|
include=[
|
|
|
|
fs.employee,
|
|
|
|
fs.store,
|
|
|
|
fs.start_time,
|
|
|
|
fs.end_time,
|
2016-05-10 13:08:32 -05:00
|
|
|
fs.length,
|
2016-04-28 18:16:26 -05:00
|
|
|
])
|
|
|
|
|
|
|
|
|
2016-04-25 19:40:29 -05:00
|
|
|
class WorkedShiftsView(MasterView):
|
|
|
|
"""
|
|
|
|
Master view for employee worked shifts.
|
|
|
|
"""
|
|
|
|
model_class = model.WorkedShift
|
|
|
|
url_prefix = '/shifts/worked'
|
2017-07-15 02:46:39 -05:00
|
|
|
has_versions = True
|
2016-04-25 19:40:29 -05:00
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
grid_columns = [
|
|
|
|
'employee',
|
|
|
|
'store',
|
|
|
|
'start_time',
|
|
|
|
'end_time',
|
|
|
|
'length',
|
|
|
|
]
|
|
|
|
|
|
|
|
def configure_grid(self, g):
|
|
|
|
super(WorkedShiftsView, self).configure_grid(g)
|
|
|
|
|
2017-02-04 12:32:21 -06:00
|
|
|
g.joiners['employee'] = lambda q: q.join(model.Employee).join(model.Person)
|
2017-07-07 09:13:53 -05:00
|
|
|
g.filters['employee'] = g.make_filter('employee', model.Person.display_name)
|
2017-02-04 12:32:21 -06:00
|
|
|
g.sorters['employee'] = g.make_sorter(model.Person.display_name)
|
|
|
|
|
|
|
|
g.joiners['store'] = lambda q: q.join(model.Store)
|
2017-07-07 09:13:53 -05:00
|
|
|
g.filters['store'] = g.make_filter('store', model.Store.name)
|
2017-02-04 12:32:21 -06:00
|
|
|
g.sorters['store'] = g.make_sorter(model.Store.name)
|
|
|
|
|
2016-05-10 13:08:32 -05:00
|
|
|
# TODO: these sorters should be automatic once we fix the schema
|
|
|
|
g.sorters['start_time'] = g.make_sorter(model.WorkedShift.punch_in)
|
|
|
|
g.sorters['end_time'] = g.make_sorter(model.WorkedShift.punch_out)
|
|
|
|
g.default_sortkey = 'start_time'
|
2016-04-25 19:40:29 -05:00
|
|
|
g.default_sortdir = 'desc'
|
2017-02-04 12:32:21 -06:00
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
g.set_renderer('length', render_shift_length)
|
2017-02-04 12:32:21 -06:00
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
g.set_label('employee', "Employee Name")
|
|
|
|
g.set_label('store', "Store Name")
|
|
|
|
g.set_label('punch_in', "Start Time")
|
|
|
|
g.set_label('punch_out', "End Time")
|
2016-04-25 19:40:29 -05:00
|
|
|
|
2017-02-04 12:32:21 -06:00
|
|
|
def get_instance_title(self, shift):
|
|
|
|
time = shift.start_time or shift.end_time
|
|
|
|
date = localtime(self.rattail_config, time).date()
|
|
|
|
return "WorkedShift: {}, {}".format(shift.employee, date)
|
|
|
|
|
|
|
|
def _preconfigure_fieldset(self, fs):
|
2016-04-25 19:40:29 -05:00
|
|
|
fs.append(ShiftLengthField('length'))
|
2017-02-04 12:32:21 -06:00
|
|
|
fs.employee.set(readonly=True, renderer=forms.renderers.EmployeeFieldRenderer)
|
|
|
|
|
|
|
|
def configure_fieldset(self, fs):
|
2016-04-25 19:40:29 -05:00
|
|
|
fs.configure(
|
|
|
|
include=[
|
|
|
|
fs.employee,
|
|
|
|
fs.store,
|
2016-05-10 13:08:32 -05:00
|
|
|
fs.start_time,
|
|
|
|
fs.end_time,
|
2016-04-25 19:40:29 -05:00
|
|
|
fs.length,
|
|
|
|
])
|
2017-02-04 12:32:21 -06:00
|
|
|
if self.editing:
|
|
|
|
del fs.length
|
2016-04-25 19:40:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
def includeme(config):
|
2016-04-28 18:16:26 -05:00
|
|
|
ScheduledShiftsView.defaults(config)
|
2016-04-25 19:40:29 -05:00
|
|
|
WorkedShiftsView.defaults(config)
|