Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,26 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, render_template_string
|
| 2 |
-
from
|
| 3 |
-
from google.oauth2.credentials import Credentials
|
| 4 |
from googleapiclient.discovery import build
|
| 5 |
from googleapiclient.http import MediaIoBaseUpload
|
| 6 |
import os
|
| 7 |
import io
|
| 8 |
import datetime
|
|
|
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
-
app.secret_key = os.environ.get('FLASK_SECRET_KEY')
|
| 12 |
|
| 13 |
SPREADSHEET_ID = '1rcIJflbC1VIc70F6dnASLFvGQ90TXHhCx0iyxsXR7Ww'
|
| 14 |
FOLDER_ID = '1brmLNqOMCvRS0TDY6ECHvIBmlRx8pcPz'
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
SCOPES = [
|
| 28 |
-
'https://www.googleapis.com/auth/spreadsheets',
|
| 29 |
-
'https://www.googleapis.com/auth/drive.file'
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
|
| 33 |
HTML_TEMPLATE = '''
|
| 34 |
<!DOCTYPE html>
|
|
@@ -158,59 +150,10 @@ HTML_TEMPLATE = '''
|
|
| 158 |
|
| 159 |
@app.route('/')
|
| 160 |
def index():
|
| 161 |
-
if 'credentials' not in session:
|
| 162 |
-
return redirect('/login')
|
| 163 |
return render_template_string(HTML_TEMPLATE)
|
| 164 |
|
| 165 |
-
@app.route('/login')
|
| 166 |
-
def login():
|
| 167 |
-
flow = Flow.from_client_config(
|
| 168 |
-
CLIENT_CONFIG,
|
| 169 |
-
scopes=SCOPES,
|
| 170 |
-
redirect_uri=CLIENT_CONFIG['web']['redirect_uris'][0]
|
| 171 |
-
)
|
| 172 |
-
authorization_url, state = flow.authorization_url(
|
| 173 |
-
access_type='offline',
|
| 174 |
-
include_granted_scopes='true'
|
| 175 |
-
)
|
| 176 |
-
session['state'] = state
|
| 177 |
-
return redirect(authorization_url)
|
| 178 |
-
|
| 179 |
-
@app.route('/oauth2callback')
|
| 180 |
-
def oauth2callback():
|
| 181 |
-
flow = Flow.from_client_config(
|
| 182 |
-
CLIENT_CONFIG,
|
| 183 |
-
scopes=SCOPES,
|
| 184 |
-
state=session['state'],
|
| 185 |
-
redirect_uri=CLIENT_CONFIG['web']['redirect_uris'][0]
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
authorization_response = request.url
|
| 189 |
-
flow.fetch_token(authorization_response=authorization_response)
|
| 190 |
-
|
| 191 |
-
credentials = flow.credentials
|
| 192 |
-
session['credentials'] = {
|
| 193 |
-
'token': credentials.token,
|
| 194 |
-
'refresh_token': credentials.refresh_token,
|
| 195 |
-
'token_uri': credentials.token_uri,
|
| 196 |
-
'client_id': credentials.client_id,
|
| 197 |
-
'client_secret': credentials.client_secret,
|
| 198 |
-
'scopes': credentials.scopes
|
| 199 |
-
}
|
| 200 |
-
|
| 201 |
-
return redirect('/')
|
| 202 |
-
|
| 203 |
-
def get_credentials():
|
| 204 |
-
if 'credentials' not in session:
|
| 205 |
-
return None
|
| 206 |
-
return Credentials(**session['credentials'])
|
| 207 |
-
|
| 208 |
@app.route('/submit', methods=['POST'])
|
| 209 |
def submit():
|
| 210 |
-
credentials = get_credentials()
|
| 211 |
-
if not credentials:
|
| 212 |
-
return jsonify({'success': False, 'message': 'Authentication required'})
|
| 213 |
-
|
| 214 |
try:
|
| 215 |
name = request.form['name']
|
| 216 |
student_id = request.form['studentId']
|
|
@@ -219,7 +162,7 @@ def submit():
|
|
| 219 |
file = request.files['receipt']
|
| 220 |
|
| 221 |
# Upload file to Drive
|
| 222 |
-
drive_service = build('drive', 'v3', credentials=
|
| 223 |
|
| 224 |
file_metadata = {
|
| 225 |
'name': f"{student_id}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{file.filename}",
|
|
@@ -241,7 +184,7 @@ def submit():
|
|
| 241 |
file_url = uploaded_file.get('webViewLink', '')
|
| 242 |
|
| 243 |
# Update Google Sheet
|
| 244 |
-
sheets_service = build('sheets', 'v4', credentials=
|
| 245 |
|
| 246 |
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 247 |
ip_address = request.remote_addr
|
|
@@ -262,5 +205,4 @@ def submit():
|
|
| 262 |
return jsonify({'success': False, 'message': f'Error: {str(e)}'})
|
| 263 |
|
| 264 |
if __name__ == '__main__':
|
| 265 |
-
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
|
| 266 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template_string
|
| 2 |
+
from google.oauth2.service_account import Credentials
|
|
|
|
| 3 |
from googleapiclient.discovery import build
|
| 4 |
from googleapiclient.http import MediaIoBaseUpload
|
| 5 |
import os
|
| 6 |
import io
|
| 7 |
import datetime
|
| 8 |
+
import json
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
|
|
|
| 11 |
|
| 12 |
SPREADSHEET_ID = '1rcIJflbC1VIc70F6dnASLFvGQ90TXHhCx0iyxsXR7Ww'
|
| 13 |
FOLDER_ID = '1brmLNqOMCvRS0TDY6ECHvIBmlRx8pcPz'
|
| 14 |
|
| 15 |
+
# Create service account credentials from environment variable
|
| 16 |
+
CREDS_JSON = os.environ.get('GOOGLE_CREDENTIALS')
|
| 17 |
+
if CREDS_JSON:
|
| 18 |
+
CREDS_INFO = json.loads(CREDS_JSON)
|
| 19 |
+
CREDENTIALS = Credentials.from_service_account_info(
|
| 20 |
+
CREDS_INFO,
|
| 21 |
+
scopes=['https://www.googleapis.com/auth/spreadsheets',
|
| 22 |
+
'https://www.googleapis.com/auth/drive.file']
|
| 23 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
HTML_TEMPLATE = '''
|
| 26 |
<!DOCTYPE html>
|
|
|
|
| 150 |
|
| 151 |
@app.route('/')
|
| 152 |
def index():
|
|
|
|
|
|
|
| 153 |
return render_template_string(HTML_TEMPLATE)
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
@app.route('/submit', methods=['POST'])
|
| 156 |
def submit():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
try:
|
| 158 |
name = request.form['name']
|
| 159 |
student_id = request.form['studentId']
|
|
|
|
| 162 |
file = request.files['receipt']
|
| 163 |
|
| 164 |
# Upload file to Drive
|
| 165 |
+
drive_service = build('drive', 'v3', credentials=CREDENTIALS)
|
| 166 |
|
| 167 |
file_metadata = {
|
| 168 |
'name': f"{student_id}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{file.filename}",
|
|
|
|
| 184 |
file_url = uploaded_file.get('webViewLink', '')
|
| 185 |
|
| 186 |
# Update Google Sheet
|
| 187 |
+
sheets_service = build('sheets', 'v4', credentials=CREDENTIALS)
|
| 188 |
|
| 189 |
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 190 |
ip_address = request.remote_addr
|
|
|
|
| 205 |
return jsonify({'success': False, 'message': f'Error: {str(e)}'})
|
| 206 |
|
| 207 |
if __name__ == '__main__':
|
|
|
|
| 208 |
app.run(host='0.0.0.0', port=7860)
|