Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,46 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify
|
2 |
-
import
|
3 |
from google.oauth2.credentials import Credentials
|
4 |
-
from google.oauth2.service_account import Credentials
|
5 |
-
from oauth2client.service_account import ServiceAccountCredentials
|
6 |
-
import io
|
7 |
-
import datetime
|
8 |
from googleapiclient.discovery import build
|
9 |
from googleapiclient.http import MediaIoBaseUpload
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
FOLDER_ID = '17gS_4w0fjOeyod6Vz6LgDK15KrQqMC-M' # Your Google Drive folder ID
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
HTML_TEMPLATE = '''
|
19 |
<!DOCTYPE html>
|
20 |
<html>
|
@@ -115,12 +141,10 @@ def submit():
|
|
115 |
cost = request.form['cost']
|
116 |
file = request.files['receipt']
|
117 |
|
118 |
-
#
|
119 |
-
|
120 |
-
sheet = gc.open_by_key(SPREADSHEET_ID).sheet1
|
121 |
|
122 |
-
# Upload file to Drive
|
123 |
-
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json')
|
124 |
drive_service = build('drive', 'v3', credentials=creds)
|
125 |
|
126 |
file_metadata = {
|
@@ -142,11 +166,21 @@ def submit():
|
|
142 |
|
143 |
file_url = uploaded_file.get('webViewLink', '')
|
144 |
|
145 |
-
#
|
|
|
|
|
146 |
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
147 |
ip_address = request.remote_addr
|
|
|
|
|
|
|
148 |
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
return jsonify({'success': True, 'message': 'Data submitted successfully!'})
|
152 |
|
@@ -154,4 +188,5 @@ def submit():
|
|
154 |
return jsonify({'success': False, 'message': f'Error: {str(e)}'})
|
155 |
|
156 |
if __name__ == '__main__':
|
|
|
157 |
app.run(host='0.0.0.0', port=7860)
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, session
|
2 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
3 |
from google.oauth2.credentials import Credentials
|
|
|
|
|
|
|
|
|
4 |
from googleapiclient.discovery import build
|
5 |
from googleapiclient.http import MediaIoBaseUpload
|
6 |
+
from google.auth.transport.requests import Request
|
7 |
+
import pickle
|
8 |
+
import os
|
9 |
+
import io
|
10 |
+
import datetime
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
+
app.secret_key = 'your-secret-key' # Change this to a random secure string
|
14 |
+
|
15 |
+
# If modifying these scopes, delete the file token.pickle.
|
16 |
+
SCOPES = ['https://www.googleapis.com/auth/spreadsheets',
|
17 |
+
'https://www.googleapis.com/auth/drive.file']
|
18 |
|
19 |
+
SPREADSHEET_ID = '1rcIJflbC1VIc70F6dnASLFvGQ90TXHhCx0iyxsXR7Ww'
|
20 |
+
FOLDER_ID = '1brmLNqOMCvRS0TDY6ECHvIBmlRx8pcPz'
|
|
|
21 |
|
22 |
+
def get_google_auth():
|
23 |
+
creds = None
|
24 |
+
# The file token.pickle stores the user's access and refresh tokens
|
25 |
+
if os.path.exists('token.pickle'):
|
26 |
+
with open('token.pickle', 'rb') as token:
|
27 |
+
creds = pickle.load(token)
|
28 |
+
|
29 |
+
# If there are no (valid) credentials available, let the user log in.
|
30 |
+
if not creds or not creds.valid:
|
31 |
+
if creds and creds.expired and creds.refresh_token:
|
32 |
+
creds.refresh(Request())
|
33 |
+
else:
|
34 |
+
flow = InstalledAppFlow.from_client_secrets_file(
|
35 |
+
'client_secret.json', SCOPES)
|
36 |
+
creds = flow.run_local_server(port=0)
|
37 |
+
# Save the credentials for the next run
|
38 |
+
with open('token.pickle', 'wb') as token:
|
39 |
+
pickle.dump(creds, token)
|
40 |
+
|
41 |
+
return creds
|
42 |
+
|
43 |
+
# HTML template (same as before)
|
44 |
HTML_TEMPLATE = '''
|
45 |
<!DOCTYPE html>
|
46 |
<html>
|
|
|
141 |
cost = request.form['cost']
|
142 |
file = request.files['receipt']
|
143 |
|
144 |
+
# Get Google credentials
|
145 |
+
creds = get_google_auth()
|
|
|
146 |
|
147 |
+
# Upload file to Drive
|
|
|
148 |
drive_service = build('drive', 'v3', credentials=creds)
|
149 |
|
150 |
file_metadata = {
|
|
|
166 |
|
167 |
file_url = uploaded_file.get('webViewLink', '')
|
168 |
|
169 |
+
# Update Google Sheet
|
170 |
+
sheets_service = build('sheets', 'v4', credentials=creds)
|
171 |
+
|
172 |
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
173 |
ip_address = request.remote_addr
|
174 |
+
|
175 |
+
values = [[name, student_id, product, cost, file_url, timestamp, ip_address]]
|
176 |
+
body = {'values': values}
|
177 |
|
178 |
+
sheets_service.spreadsheets().values().append(
|
179 |
+
spreadsheetId=SPREADSHEET_ID,
|
180 |
+
range='Sheet1!A:G',
|
181 |
+
valueInputOption='USER_ENTERED',
|
182 |
+
body=body
|
183 |
+
).execute()
|
184 |
|
185 |
return jsonify({'success': True, 'message': 'Data submitted successfully!'})
|
186 |
|
|
|
188 |
return jsonify({'success': False, 'message': f'Error: {str(e)}'})
|
189 |
|
190 |
if __name__ == '__main__':
|
191 |
+
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' # Only for development
|
192 |
app.run(host='0.0.0.0', port=7860)
|