Files
@ 180c9314d848
Branch filter:
Location: rattail-project/rattail/rattail/db/model/shifts.py
180c9314d848
5.4 KiB
text/x-python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | # -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# 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.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Data models for employee work shifts
"""
from __future__ import unicode_literals, absolute_import
import six
import sqlalchemy as sa
from sqlalchemy import orm
from .core import Base, uuid_column
from rattail.time import localtime, make_utc
class ShiftMixin(object):
"""
Mixin for logic common to both scheduled and worked shifts.
"""
@property
def length(self):
if self.start_time and self.end_time:
return self.end_time - self.start_time
def get_date(self, config):
"""
Return the effective date for the shift, according to the given config
(i.e. timezone).
"""
time = self.end_time or self.start_time
assert time
time = make_utc(time, tzinfo=True)
return localtime(config, time).date()
def get_display(self, config):
"""
Return a simple string for displaying the shift, according to the given
config (i.e. timezone).
"""
return '{} - {}'.format(self._format_punch(config, self.start_time),
self._format_punch(config, self.end_time))
def _format_punch(self, config, time):
if time is None:
return '??'
time = localtime(config, make_utc(time, tzinfo=True))
return time.strftime('%I:%M %p')
class ScheduledShift(Base, ShiftMixin):
"""
Represents a scheduled shift for an employee.
"""
__tablename__ = 'employee_shift_scheduled'
__table_args__ = (
sa.ForeignKeyConstraint(['employee_uuid'], ['employee.uuid'], name='employee_shift_scheduled_fk_employee'),
sa.ForeignKeyConstraint(['store_uuid'], ['store.uuid'], name='employee_shift_scheduled_fk_store'),
)
uuid = uuid_column()
employee_uuid = sa.Column(sa.String(length=32), nullable=False)
employee = orm.relationship(
'Employee',
doc="""
Reference to the :class:`rattail.db.model.Employee` instance whose
shift this is.
""",
backref=orm.backref('scheduled_shifts', doc="""
Sequence of :class:`rattail.db.model.ScheduledShift` instances for the
employee.
"""))
store_uuid = sa.Column(sa.String(length=32), nullable=True)
store = orm.relationship(
'Store',
doc="""
Reference to the :class:`rattail.db.model.Store` instance, representing
the location at which the shift is scheduled, if applicable/known.
""")
start_time = sa.Column(sa.DateTime(), nullable=False, doc="""
Date and time when the shift is scheduled to start.
""")
end_time = sa.Column(sa.DateTime(), nullable=False, doc="""
Date and time when the shift is scheduled to end.
""")
@six.python_2_unicode_compatible
class WorkedShift(Base, ShiftMixin):
"""
Represents a shift actually *worked* by an employee. (Either ``punch_in``
or ``punch_out`` is generally assumed to be non-null.)
"""
__tablename__ = 'employee_shift_worked'
__table_args__ = (
sa.ForeignKeyConstraint(['employee_uuid'], ['employee.uuid'], name='employee_shift_worked_fk_employee'),
sa.ForeignKeyConstraint(['store_uuid'], ['store.uuid'], name='employee_shift_worked_fk_store'),
)
uuid = uuid_column()
employee_uuid = sa.Column(sa.String(length=32), nullable=False)
employee = orm.relationship(
'Employee',
doc="""
Reference to the :class:`rattail.db.model.Employee` instance whose
shift this is.
""",
backref=orm.backref('worked_shifts', doc="""
Sequence of :class:`rattail.db.model.WorkedShift` instances for the
employee.
"""))
store_uuid = sa.Column(sa.String(length=32), nullable=True)
store = orm.relationship(
'Store',
doc="""
Reference to the :class:`rattail.db.model.Store` instance, representing
the location at which the shift was worked, if applicable/known.
""")
punch_in = sa.Column(sa.DateTime(), nullable=True, doc="""
UTC timestamp representing the punch-in time for the shift.
""")
punch_out = sa.Column(sa.DateTime(), nullable=True, doc="""
UTC timestamp representing the punch-out time for the shift.
""")
# TODO: These are needed to make WorkedShift interchangeable with
# ScheduledShift for certain UI logic etc. Perhaps should just rename the
# 'punch' columns at some point..?
start_time = orm.synonym('punch_in')
end_time = orm.synonym('punch_out')
def __str__(self):
return unicode(self.employee or '')
|