-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
177 lines (159 loc) · 4.91 KB
/
app.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
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
168
169
170
171
172
173
174
175
176
from flask import Flask, request
import datetime
from datetime import datetime
import time, os, argparse, sys
from utils.gsheets import gsheet
import utils.db as db
import utils.project_constants as constants
import utils.DevNull as DevNull
app = Flask(__name__)
ATTENDANCE_SHEET = None
def load_attendance_sheet():
"""
Loads in the ATTENDANCE_SHEET global variable via utils.gsheets
Params:
None
Returns:
True if successful or if already loaded, False otherwise
"""
global ATTENDANCE_SHEET
if ATTENDANCE_SHEET is not None:
return True
status = True
# gs denotes Google Sheet
gs = gsheet()
status = gs.load_credentials()
if not status:
return False
# ss denotes Spread Sheet
ss = gs.get_sheet_from_name(constants.STUYCS_DOJO_ATTENDANCE_SHEET_NAME)
if ss is None:
return False
worksheets = gs.get_list_of_sheets(ss)
if len(worksheets) == 0:
return False
ATTENDANCE_SHEET = worksheets[0]
return True
def get_columns(try_again=True):
"""
Gets the row 1 elements of all used columns
Params:
None
Returns:
A list of strings if successful, None otherwise
"""
load_attendance_sheet() # Load the attendance sheet it is not yet loaded
global ATTENDANCE_SHEET
try:
column_headers = ATTENDANCE_SHEET.row_values(1)
return column_headers
except Exception as e:
if try_again:
ATTENDANCE_SHEET = None
load_attendance_sheet()
return get_columns(try_again=False)
else:
return None
def get_known_osis():
"""
Gets a list of known osis numbers. We know that the first column is a list
of all the osis numbers, where the 0th index of the list is "OSIS"
Params:
None
Returns:
A list of strings if successful, None otherwise
"""
load_attendance_sheet() # Load the attendance sheet it is not yet loaded
global ATTENDANCE_SHEET
try:
osis_numbers = ATTENDANCE_SHEET.col_values(1)
return osis_numbers
except Exception as e:
print("ERROR ({0}): {1}".format(e.errno, e.strerror))
return None
@app.route("/")
def add_id():
"""
Adds a single ID into the google spreadsheet, given data in the GET
parameters
Params:
None
Returns:
None
"""
# Grab the parameters and exit if not provided or incorrect
u_name = request.args.get('username')
p_word = request.args.get('pword')
if u_name is None or p_word is None:
return "no_creds"
if not db.admin_exists(u_name, p_word):
return "bad_login"
date = request.args.get('date')
if date is None:
date = str(datetime.now())[0:10].replace("-", "_")
print("No given date. Generating from system time: " + str(date))
osis = request.args.get('osis')
if osis is None:
return "no_osis"
# Denote ATTENDANCE_SHEET as the global variable, not instance variable
global ATTENDANCE_SHEET
# Calculate the column for the specific day
cols = get_columns()
col_num = 0
while col_num < len(cols):
if cols[col_num] == date:
break
col_num += 1
col_num += 1 # The spreadsheet is 1 indexed, not 0 indexed
# Get the row number (row for the osis)
osis_nums = get_known_osis()
# Resize the table!
ATTENDANCE_SHEET.resize(len(osis_nums) + 1, len(cols) + 1)
# Update the date for that column:
ATTENDANCE_SHEET.update_cell(1, col_num, date)
# Keep in mind that osis_nums should be sorted
row_num = 1
while row_num < len(osis_nums):
# If the osis matches, stop iterating
if osis_nums[row_num] == osis:
break
# Convert the values into integers
# If the osis is between the values, insert a row
intval = -1
intosis = -1
try:
intval = int(osis_nums[row_num])
intosis = int(osis)
except:
row_num += 1
continue
if int(osis_nums[row_num]) > int(osis):
print("Unrecognized OSIS. Adding to sheet")
ATTENDANCE_SHEET.insert_row(["" for i in cols], row_num + 1)
break
# Increase the counter
row_num += 1
# When we are done iterating to get the osis, mark as present
ATTENDANCE_SHEET.update_cell(row_num + 1, 1, str(osis))
ATTENDANCE_SHEET.update_cell(row_num + 1, col_num, 'X')
return "OK"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--debug",
help="Sets Flask's app.debug value to True",
action="store_true"
)
parser.add_argument(
"-q",
"--quiet",
help="Silences STDERR",
action="store_true"
)
args = parser.parse_args()
if args.debug:
app.debug = True
if args.quiet:
sys.stderr = DevNull.DevNull()
app.run("0.0.0.0", 11235)