Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (119 loc) · 4.82 KB
/
Copy pathmain.py
File metadata and controls
138 lines (119 loc) · 4.82 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
131
132
133
134
135
136
137
138
from flask import Flask, render_template, request
from datetime import date, datetime
import google.auth
import os
from googleapiclient.discovery import build
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./sgsj-cpp-6a90831e84cc.json"
credentials, project = google.auth.default()
service = build('sheets', 'v4', credentials=credentials)
SPREADSHEET_ID = '1s5wucn6ivMbW6PNjqUCx-aBa4Ky7tg_pPxNXULtWbyU'
SAMPLE_RANGE_NAME = 'IDs!A2:A'
app = Flask(__name__)
print(__name__)
@app.route('/')
def home():
current_date = date.today()
return render_template('home.html', now=current_date)
@app.route('/test', methods=['POST'])
def mark():
event = request.form['event'].strip()
email = request.form['email'].lower().strip()
if email == '':
current_date = date.today()
return render_template('home.html', now=current_date)
else:
return handleEmail(email, event)
def handleEmail(email, event):
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range='IDs!A2:C1000').execute()
values = result.get('values', [])
if not values:
return 'No data in Spreadsheet'
print(f"{len(values)} rows retrieved")
import collections
cnt = collections.Counter()
for row in values:
cnt[row[1].lower().strip()] += 1
if cnt[email] == 0:
record_with_email(email, event)
return render_template('do_survey.html')
if cnt[email] == 1:
for row in values:
if email == row[1].lower().strip():
record_with_id(event, str(row[0]))
if (event == 'Feb 5 - English Assessment Exam'):
return render_template('do_english_test.html')
else:
return render_template('do_math_test.html')
else:
print("Got a child case")
return render_template('child.html', email1=email, event1=event)
def record_with_email(email, event):
body = {
'values': [
[email, event, str(datetime.now())]
]
}
sheet = service.spreadsheets()
sheet.values().append(spreadsheetId=SPREADSHEET_ID, range='Attendance-Email!A2:C2', valueInputOption="USER_ENTERED", body=body).execute()
def record_with_id(event, cpp_id):
body = {
'values': [
[cpp_id, event, str(datetime.now())]
]
}
sheet = service.spreadsheets()
sheet.values().append(spreadsheetId=SPREADSHEET_ID, range='Attendance-ID!A2:C2', valueInputOption="USER_ENTERED", body=body).execute()
@app.route('/mark_second_stage', methods=['POST'])
def mark_second_stage():
event = request.form['event'].strip()
email = request.form['email'].lower().strip()
fName = request.form['fName'].lower().strip()
if email == '' or fName == '':
current_date = date.today()
return render_template('home.html', now=current_date)
else:
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range='IDs!A2:C1000').execute()
values = result.get('values', [])
if not values:
return 'No data in Spreadsheet'
print(f"{len(values)} rows retrieved")
for row in values:
if (email == row[1].lower() and fName == row[2].lower()):
record_with_id(event, str(row[0]))
return 'Thank you ' + row[2] + "! Your attendance is recorded. <br/> Your CPP ID is <b>" + str(row[0]) + "</b>."
# No Match - do survey
record_with_email(email, event)
return render_template('do_survey.html')
@app.route('/getID', methods=['GET', 'POST'])
def getID():
if request.method == 'GET':
return render_template('get_id.html')
else:
email = request.form['email'].lower().strip()
fName = request.form['fName'].lower().strip()
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range='IDs!A2:C1000').execute()
values = result.get('values', [])
if not values:
return 'No data in Spreadsheet'
print(f"{len(values)} rows retrieved")
import collections
cnt = collections.Counter()
for row in values:
cnt[row[1].lower().strip()] += 1
if cnt[email] == 0:
return render_template('do_survey.html')
if cnt[email] == 1:
for row in values:
if email == row[1].lower().strip():
return "Your CPP ID is <b>" + str(row[0]) + "</b>."
else:
#Child
for row in values:
if fName == row[2].lower().strip() and email == row[1].lower().strip():
return "Your CPP ID is <b>" + str(row[0]) + "</b>."
return render_template('do_survey.html')
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8080)