-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_shifts.py
112 lines (89 loc) · 3.45 KB
/
update_shifts.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
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
import numpy as np
import sys
from flaskblog import create_app
app = create_app()
app = app.app_context().push()
from flaskblog import db
from flaskblog.models import User, Calender, Shift
# Get all entries from calender
# schedule_list = Calender.query.all()
all_schedule = []
# process the values in a list of users
for schedule in Calender.query.order_by("date_posted"):
all_schedule.append(schedule.get_all())
sa_schedules = np.array(all_schedule)
# print(sa_schedules)
# logic
num_of_sa = len(sa_schedules)
if num_of_sa!=0:
max_hours = 48/num_of_sa
else:
sys.exit()
row = sa_schedules.shape[0]
col = sa_schedules.shape[1]
shifts = np.zeros(shape = (6, 3))
total_val = np.zeros(row)
slots_allotted = []
people_allotted = []
for i in range(0,row):
for j in range(1, 7):
if sa_schedules[i][j*4] and shifts[j-1][:].all()==0:
shifts[j-1][:] = sa_schedules[i][0]
slots_allotted.extend([j*4-3,j*4-2,j*4-1,j*4])
people_allotted.append(i)
total_val[i]=8
break
slots_copy = slots_allotted
for x in [i for i in range(0,row) if i not in people_allotted]:
for y in [j for j in range(1,col) if j not in slots_copy]:
if sa_schedules[x][y] and total_val[x]<=8 and shifts[int(y/4)][(int(y%4))-1]==0:
shifts[int(y/4)][(int(y%4))-1] = sa_schedules[x][0]
slots_allotted.append(y)
if (y%4) == 1:
total_val[x]+=2
else:
total_val[x]+=3
slots_copy = slots_allotted
for i in [j for j in range(1,col) if j not in slots_copy]:
for x in range(0,row):
if sa_schedules[x][i] and total_val[x]<=14 and shifts[int(i/4)][(int(i%4))-1]==0:
# print(f'user:{x+1} for slot {i}')
shifts[int(i/4)][(int(i%4))-1] = sa_schedules[x][0]
slots_allotted.append(i)
if (i%4) == 1:
total_val[x]+=2
else:
total_val[x]+=3
# print(shifts)
# sa_schedules[x][4*y] = whole day schedule of days
# shifts = shifts.reshape(18,2)
shifts = shifts.flatten()
# print(shifts)
# change the values of existing shifts
Shift.query.filter_by(id=1).delete()
# db.session.commit()
shifts_t = Shift(mon1=User.query.get(shifts[0]).username if shifts[0]!=0 else 'free',
mon2=User.query.get(shifts[1]).username if shifts[1]!=0 else 'free',
mon3=User.query.get(shifts[2]).username if shifts[2]!=0 else 'free',
tues1=User.query.get(shifts[3]).username if shifts[3]!=0 else 'free',
tues2=User.query.get(shifts[4]).username if shifts[4]!=0 else 'free',
tues3=User.query.get(shifts[5]).username if shifts[5]!=0 else 'free',
wed1=User.query.get(shifts[6]).username if shifts[6]!=0 else 'free',
wed2=User.query.get(shifts[7]).username if shifts[7]!=0 else 'free',
wed3=User.query.get(shifts[8]).username if shifts[8]!=0 else 'free',
thurs1=User.query.get(shifts[9]).username if shifts[9]!=0 else 'free',
thurs2=User.query.get(shifts[10]).username if shifts[10]!=0 else 'free',
thurs3=User.query.get(shifts[11]).username if shifts[11]!=0 else 'free',
fri1=User.query.get(shifts[12]).username if shifts[12]!=0 else 'free',
fri2=User.query.get(shifts[13]).username if shifts[13]!=0 else 'free',
fri3=User.query.get(shifts[14]).username if shifts[14]!=0 else 'free',
sat1=User.query.get(shifts[15]).username if shifts[15]!=0 else 'free',
sat2=User.query.get(shifts[16]).username if shifts[16]!=0 else 'free',
sat3=User.query.get(shifts[17]).username if shifts[17]!=0 else 'free')
print(shifts_t)
db.session.add(shifts_t)
db.session.commit()
# Drop all values in Calender database
Calender.query.delete()
db.session.commit()
# commit to database