-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule.py
70 lines (61 loc) · 2.04 KB
/
schedule.py
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
import shiftplanning as sp
import datetime as dt
from desklib.confidential import sp_creds
class AttributeRich(object):
def __init__(self, data, pref_attrs={}):
self.data = data
pref_attrs = {
"start_timestamp": "start",
"end_timestamp": "end"}
for k, v in self.data.iteritems():
k = pref_attrs.get(k, k)
try:
v = dt.datetime.strptime(v, '%Y-%m-%d %H:%M:%S')
except:
pass
try:
v = map(AttributeRich, v)
except:
pass
try:
setattr(self, k, AttributeRich(v))
except:
setattr(self, k, v)
def __repr__(self):
return str(self.data)
def get_shifts(start, end):
connection = sp.ShiftPlanning(sp_creds['key'], sp_creds['username'],
sp_creds['password'])
connection.do_login()
connection.get_shifts(start, end, mode='overview')
shifts = map(AttributeRich, connection.get_public_data())
connection.do_logout()
del connection
return shifts
def onNow():
now = dt.datetime.now()
shifts = get_shifts(now, now)
employees = []
for shift in shifts:
if shift.start < now < shift.end:
employees += [e.name for e in shift.employees]
return employees
def onDay(num, hours=False):
date = dt.datetime.now() + dt.timedelta(days=num)
shifts = get_shifts(date, date)
employees = []
if hours == True:
for shift in shifts:
sched_str = "{} - {}".format(
shift.start.strftime('%H:%M'),
shift.end.strftime('%H:%M'))
delta = shift.end - shift.start
delta_hours = delta.days*24 + delta.seconds/3600
employees += [(e.name, sched_str, delta_hours)
for e in shift.employees]
else:
for shift in shifts:
employees += [e.name for e in shift.employees]
return employees
def isWorking(name):
return name in onNow()