-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
130 lines (104 loc) · 3.33 KB
/
helpers.py
File metadata and controls
130 lines (104 loc) · 3.33 KB
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
from django.contrib.auth import get_user_model
import os
import pandas as pd
from datetime import datetime, time, timedelta
from login.models import UserOptions
from utils.message import send_stc_email
from . models import Shift
USER_MODEL = get_user_model()
def scrape_schedule(f):
for i in Shift.objects.all():
i.delete()
xl = pd.ExcelFile(f)
if len(xl.sheet_names) > 1:
print("More than one sheet name, using first sheet {}".format(xl.sheet_names[0]))
sh = xl.parse(xl.sheet_names[0])
start = 0
end = 0
time = { }
return_list = []
col_c = 0
start_t = datetime.strptime('08:00', '%H:%M').time()
#Figure out where the information is stored row wise
for i in sh:
for c, j in enumerate(sh[i]):
if j == start_t and start == 0:
start = c
if j == start_t and c != start:
end = c
sh = sh[start:end + 1]
#Set up a dict to hash index -> datetime
for c in range(0, (end - start) + 1):
time_s = str(c + 8)
if len(time_s) < 2:
time_s = "0" + time_s
time_s += ":00"
time[c] = datetime.strptime(time_s, '%H:%M').time()
for i in sh:
for c, j in enumerate(sh[i]):
#Ensure it's data we want, the sheet returns weird stuff.
if type(j) != float and j != 'x' and type(j) != int:
return_list.append((j, time[c], get_day_by_column(col_c)))
col_c += 1
new_list = [x for x in return_list if type(x[0]) == str]
return [x for x in new_list if (x[0].lower() != 'noon')]
def create_shifts(shifts, file_m):
#Gets a set of datetime objects daily between two dates.
delta = file_m.last_date - file_m.first_date
dates = [file_m.first_date + timedelta(days=date) for date in range(delta.days + 1)]
#Create all the date objects.
for day in dates:
for shift in shifts:
dow = shift[2]
time = shift[1]
name = shift[0]
if get_day_by_value(day.weekday()).lower() == dow.lower():
shift_item = Shift.objects.create(
day_of_week=dow,
date=day,
start=time,
sheet_user=name.lower()
)
claim_shifts()
def claim_shifts(request=None):
shifts = Shift.objects.all()
for shift in shifts:
#Get the name as it appears on the excel sheet.
sheet_name = shift.sheet_user
try:
matching_user = UserOptions.objects.get(shift_name=sheet_name.lower()).user
shift.user = matching_user
shift.save()
except UserOptions.DoesNotExist:
pass
#yep
def get_day_by_column(c):
if c >= 1 and c < 4:
return 'sunday'
elif c >= 5 and c <= 11:
return 'monday'
elif c > 12 and c <= 18:
return 'tuesday'
elif c >= 20 and c <= 25:
return 'wednesday'
elif c >= 28 and c <= 33:
return 'thursday'
elif c >= 33 and c <= 39:
return 'friday'
elif c >= 41 and c <= 46:
return 'saturday'
def get_day_by_value(c):
if c == 0:
return 'monday'
if c == 1:
return 'tuesday'
if c == 2:
return 'wednesday'
if c == 3:
return 'thursday'
if c == 4:
return 'friday'
if c == 5:
return 'saturday'
if c == 6:
return 'sunday'