-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
185 lines (148 loc) · 6.13 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
177
178
179
180
181
182
183
184
185
from flask import Flask, request, render_template, redirect, url_for, session, send_from_directory
from google.oauth2 import service_account
from googleapiclient.discovery import build
import os
import datetime
from waitress import serve
route_prefix = os.getenv('APP_ROUTE') or ""
if(route_prefix != ""):
route_prefix = "/" + route_prefix
app = Flask(__name__)
app.secret_key = "thisismyveryloooongsecretkey"
# Initialize Google Sheets API
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = '/json/proud-device-436515-b7-039323121352.json' # Update this path
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
SHEET_ID = '19Bt8DrXW7l-tAPskOiY9prwWFw7WZ5JHyusnT-54EmM'
RANGE_NAME = 'Sheet1!A2:J' # Adjust range as needed
ADMIN_RANGE_NAME = 'Sheet2!A1:B'
@app.route('/')
def index():
session.clear()
return render_template('index.html',route=route_prefix)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static/images'),
'favicon.png', mimetype='image/vnd.microsoft.icon')
def build_list(name_to_filter):
service = build('sheets', 'v4', credentials=creds)
result = service.spreadsheets().values().get(spreadsheetId=SHEET_ID, range=RANGE_NAME).execute()
values = result.get('values', [])
ndate = datetime.datetime.now() + datetime.timedelta(days=-1) #datetime.strptime(date_string, "%Y-%m-%d")
if session.get("isadmin") == None:
# Filter by name
filtered_values = [row for row in values if row and row[1] == name_to_filter and datetime.datetime.strptime(row[3], "%Y-%m-%d") >= ndate]
else:
filtered_values = [row for row in values if row and datetime.datetime.strptime(row[3], "%Y-%m-%d") >= ndate]
for row in filtered_values:
row.append(datetime.datetime.strptime(row[3], '%Y-%m-%d').strftime('%A'))
filtered_values.sort(key=lambda x: (datetime.datetime.strptime(x[3], '%Y-%m-%d'),datetime.datetime.strptime(x[4], '%H:%M')))
[print(row) for row in filtered_values]
return filtered_values
@app.route('/auth', methods=['POST'])
def auth():
session.clear()
name_to_filter = request.form['mobile']
passkey = request.form.get("passkey")
service = build('sheets', 'v4', credentials=creds)
if(passkey):
result = service.spreadsheets().values().get(spreadsheetId=SHEET_ID, range=ADMIN_RANGE_NAME).execute()
values = result.get('values', [])
filtered_values = [row for row in values if row and row[0] == name_to_filter]
if filtered_values and passkey == filtered_values[0][1]:
session["isadmin"] = True
session["name_to_filter"] = name_to_filter
return redirect(route_prefix + '/list')
@app.route('/list', methods=['GET'])
def new_and_list():
filtered_values = build_list(name_to_filter=session['name_to_filter'])
return render_template('home.html', filtered_values=filtered_values, mobile=session['name_to_filter'], is_admin=session.get('isadmin'),route=route_prefix)
@app.route('/delete', methods=['POST'])
def delete():
row = request.form['delrowid']
column = "G" #will be fixed for row
new_value = 'Cancelled'
mobile = request.form['delrowmobile']
service = build('sheets', 'v4', credentials=creds)
range_name = f'Sheet1!{column}{row}' # Example: "Sheet1!A2"
body = {
'values': [[new_value]]
}
service.spreadsheets().values().update(
spreadsheetId=SHEET_ID,
range=range_name,
valueInputOption='RAW',
body=body
).execute()
return redirect(route_prefix + '/list')
@app.route('/schedule', methods=['POST'])
def schedule():
Mobile = request.form['Mobile']
Name = request.form['Name']
Date = request.form['Date']
Time = request.form['Time']
Duration = request.form['Duration']
service = build('sheets', 'v4', credentials=creds)
status_val = '=if(INDIRECT(ADDRESS(row(),column()+1))="","Pay Now",if(INDIRECT(ADDRESS(row(),column()+3)),"Confirmed","Approval Pending"))'
values = [["=Row()", Mobile, Name, Date, Time, Duration,status_val,'','','FALSE']]
body = {
'values': values
}
service.spreadsheets().values().append(
spreadsheetId=SHEET_ID,
range=RANGE_NAME,
valueInputOption='USER_ENTERED',
body=body
).execute()
return redirect(route_prefix + '/list')
@app.route('/confirm', methods=['POST'])
def update_confirm():
row = request.form['confirmrowid']
columnApproval = "J" #will be fixed for row for approval checkbox
service = build('sheets', 'v4', credentials=creds)
#can be improved with one call
rangeApproval_name = f'Sheet1!{columnApproval}{row}'
bodyApproval = {
'values': [[True]]
}
service.spreadsheets().values().update(
spreadsheetId=SHEET_ID,
range=rangeApproval_name,
valueInputOption='RAW',
body=bodyApproval
).execute()
return redirect(route_prefix + '/list')
@app.route('/paymentconfirm', methods=['POST'])
def update_payment():
row = request.form['payrowid']
column = "H" #will be fixed for row
columnApproval = "J" #will be fixed for row for approval checkbox
new_value = request.form['payref']
mobile = request.form['payrowmobile']
service = build('sheets', 'v4', credentials=creds)
range_name = f'Sheet1!{column}{row}' # Example: "Sheet1!A2"
body = {
'values': [[new_value]]
}
service.spreadsheets().values().update(
spreadsheetId=SHEET_ID,
range=range_name,
valueInputOption='RAW',
body=body
).execute()
#can be improved with one call
rangeApproval_name = f'Sheet1!{columnApproval}{row}'
bodyApproval = {
'values': [[False]]
}
service.spreadsheets().values().update(
spreadsheetId=SHEET_ID,
range=rangeApproval_name,
valueInputOption='RAW',
body=bodyApproval
).execute()
return redirect(route_prefix + '/list')
if __name__ == '__main__':
#app.run(debug=True, host="0.0.0.0", port=5000)
serve(app, host='0.0.0.0', port=5000)