Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -27,56 +27,6 @@ client_config = {
|
|
27 |
}
|
28 |
}
|
29 |
|
30 |
-
def credentials_to_dict(credentials):
|
31 |
-
return {
|
32 |
-
'token': credentials.token,
|
33 |
-
'refresh_token': credentials.refresh_token,
|
34 |
-
'token_uri': credentials.token_uri,
|
35 |
-
'client_id': credentials.client_id,
|
36 |
-
'client_secret': credentials.client_secret,
|
37 |
-
'scopes': credentials.scopes
|
38 |
-
}
|
39 |
-
|
40 |
-
def get_credentials():
|
41 |
-
if 'credentials' not in session:
|
42 |
-
return None
|
43 |
-
return Credentials(**session['credentials'])
|
44 |
-
|
45 |
-
@app.route('/authorize')
|
46 |
-
def authorize():
|
47 |
-
flow = Flow.from_client_config(
|
48 |
-
client_config,
|
49 |
-
scopes=SCOPES,
|
50 |
-
redirect_uri=client_config['web']['redirect_uris'][0]
|
51 |
-
)
|
52 |
-
authorization_url, state = flow.authorization_url(
|
53 |
-
access_type='offline',
|
54 |
-
include_granted_scopes='true'
|
55 |
-
)
|
56 |
-
session['state'] = state
|
57 |
-
return redirect(authorization_url)
|
58 |
-
|
59 |
-
@app.route('/oauth2callback')
|
60 |
-
def oauth2callback():
|
61 |
-
state = session['state']
|
62 |
-
flow = Flow.from_client_config(
|
63 |
-
client_config,
|
64 |
-
scopes=SCOPES,
|
65 |
-
state=state,
|
66 |
-
redirect_uri=client_config['web']['redirect_uris'][0]
|
67 |
-
)
|
68 |
-
authorization_response = request.url
|
69 |
-
flow.fetch_token(authorization_response=authorization_response)
|
70 |
-
credentials = flow.credentials
|
71 |
-
session['credentials'] = credentials_to_dict(credentials)
|
72 |
-
return redirect('/')
|
73 |
-
|
74 |
-
@app.route('/')
|
75 |
-
def index():
|
76 |
-
if not get_credentials():
|
77 |
-
return redirect('/authorize')
|
78 |
-
return render_template_string(HTML_TEMPLATE)
|
79 |
-
|
80 |
HTML_TEMPLATE = '''
|
81 |
<!DOCTYPE html>
|
82 |
<html>
|
@@ -203,12 +153,62 @@ HTML_TEMPLATE = '''
|
|
203 |
</html>
|
204 |
'''
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
@app.route('/')
|
207 |
-
def
|
|
|
|
|
208 |
return render_template_string(HTML_TEMPLATE)
|
209 |
|
210 |
@app.route('/submit', methods=['POST'])
|
211 |
def submit():
|
|
|
|
|
|
|
|
|
212 |
try:
|
213 |
name = request.form['name']
|
214 |
student_id = request.form['studentId']
|
@@ -216,8 +216,6 @@ def submit():
|
|
216 |
cost = request.form['cost']
|
217 |
file = request.files['receipt']
|
218 |
|
219 |
-
credentials = Credentials.from_authorized_user_info(CREDENTIALS)
|
220 |
-
|
221 |
# Upload file to Drive
|
222 |
drive_service = build('drive', 'v3', credentials=credentials)
|
223 |
|
|
|
27 |
}
|
28 |
}
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
HTML_TEMPLATE = '''
|
31 |
<!DOCTYPE html>
|
32 |
<html>
|
|
|
153 |
</html>
|
154 |
'''
|
155 |
|
156 |
+
def credentials_to_dict(credentials):
|
157 |
+
return {
|
158 |
+
'token': credentials.token,
|
159 |
+
'refresh_token': credentials.refresh_token,
|
160 |
+
'token_uri': credentials.token_uri,
|
161 |
+
'client_id': credentials.client_id,
|
162 |
+
'client_secret': credentials.client_secret,
|
163 |
+
'scopes': credentials.scopes
|
164 |
+
}
|
165 |
+
|
166 |
+
def get_credentials():
|
167 |
+
if 'credentials' not in session:
|
168 |
+
return None
|
169 |
+
return Credentials(**session['credentials'])
|
170 |
+
|
171 |
+
@app.route('/authorize')
|
172 |
+
def authorize():
|
173 |
+
flow = Flow.from_client_config(
|
174 |
+
client_config,
|
175 |
+
scopes=SCOPES,
|
176 |
+
redirect_uri=client_config['web']['redirect_uris'][0]
|
177 |
+
)
|
178 |
+
authorization_url, state = flow.authorization_url(
|
179 |
+
access_type='offline',
|
180 |
+
include_granted_scopes='true'
|
181 |
+
)
|
182 |
+
session['state'] = state
|
183 |
+
return redirect(authorization_url)
|
184 |
+
|
185 |
+
@app.route('/oauth2callback')
|
186 |
+
def oauth2callback():
|
187 |
+
state = session['state']
|
188 |
+
flow = Flow.from_client_config(
|
189 |
+
client_config,
|
190 |
+
scopes=SCOPES,
|
191 |
+
state=state,
|
192 |
+
redirect_uri=client_config['web']['redirect_uris'][0]
|
193 |
+
)
|
194 |
+
authorization_response = request.url
|
195 |
+
flow.fetch_token(authorization_response=authorization_response)
|
196 |
+
credentials = flow.credentials
|
197 |
+
session['credentials'] = credentials_to_dict(credentials)
|
198 |
+
return redirect('/')
|
199 |
+
|
200 |
@app.route('/')
|
201 |
+
def home(): # Changed from 'index' to 'home'
|
202 |
+
if not get_credentials():
|
203 |
+
return redirect('/authorize')
|
204 |
return render_template_string(HTML_TEMPLATE)
|
205 |
|
206 |
@app.route('/submit', methods=['POST'])
|
207 |
def submit():
|
208 |
+
credentials = get_credentials()
|
209 |
+
if not credentials:
|
210 |
+
return jsonify({'success': False, 'message': 'Authentication required'})
|
211 |
+
|
212 |
try:
|
213 |
name = request.form['name']
|
214 |
student_id = request.form['studentId']
|
|
|
216 |
cost = request.form['cost']
|
217 |
file = request.files['receipt']
|
218 |
|
|
|
|
|
219 |
# Upload file to Drive
|
220 |
drive_service = build('drive', 'v3', credentials=credentials)
|
221 |
|