Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
-
from flask import Flask, request, jsonify, render_template_string
|
2 |
-
from
|
|
|
3 |
from googleapiclient.discovery import build
|
4 |
from googleapiclient.http import MediaIoBaseUpload
|
5 |
import os
|
@@ -7,155 +8,205 @@ import io
|
|
7 |
import datetime
|
8 |
|
9 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
# Google API settings
|
12 |
-
SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.file']
|
13 |
SPREADSHEET_ID = '1rcIJflbC1VIc70F6dnASLFvGQ90TXHhCx0iyxsXR7Ww'
|
14 |
FOLDER_ID = '1brmLNqOMCvRS0TDY6ECHvIBmlRx8pcPz'
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
"
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
"token_uri": "https://oauth2.googleapis.com/token",
|
26 |
-
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
27 |
-
"client_x509_cert_url": os.environ.get('CLIENT_X509_CERT_URL')
|
28 |
}
|
29 |
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
@app.route('/')
|
33 |
-
def
|
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 |
-
<label for="receipt">發票 Receipt:</label>
|
116 |
-
<input type="file" id="receipt" name="receipt" accept="image/*,.pdf" required>
|
117 |
-
</div>
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
document.getElementById('purchaseForm').addEventListener('submit', async (e) => {
|
125 |
-
e.preventDefault();
|
126 |
-
|
127 |
-
const status = document.getElementById('status');
|
128 |
-
status.style.display = 'block';
|
129 |
-
status.textContent = '提交中... Submitting...';
|
130 |
-
status.className = '';
|
131 |
|
132 |
-
const
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
const result = await response.json();
|
141 |
-
|
142 |
-
if (result.success) {
|
143 |
-
status.className = 'success';
|
144 |
-
status.textContent = '提交成功! ' + result.message;
|
145 |
-
e.target.reset();
|
146 |
-
} else {
|
147 |
-
status.className = 'error';
|
148 |
-
status.textContent = '錯誤:' + result.message;
|
149 |
-
}
|
150 |
-
} catch (error) {
|
151 |
status.className = 'error';
|
152 |
-
status.textContent = '
|
153 |
}
|
154 |
-
})
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
@app.route('/submit', methods=['POST'])
|
161 |
def submit():
|
@@ -166,6 +217,8 @@ def submit():
|
|
166 |
cost = request.form['cost']
|
167 |
file = request.files['receipt']
|
168 |
|
|
|
|
|
169 |
# Upload file to Drive
|
170 |
drive_service = build('drive', 'v3', credentials=credentials)
|
171 |
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template_string, redirect, session
|
2 |
+
from google_auth_oauthlib.flow import Flow
|
3 |
+
from google.oauth2.credentials import Credentials
|
4 |
from googleapiclient.discovery import build
|
5 |
from googleapiclient.http import MediaIoBaseUpload
|
6 |
import os
|
|
|
8 |
import datetime
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
+
app.secret_key = os.environ.get('FLASK_SECRET_KEY')
|
12 |
+
|
13 |
+
SCOPES = ['https://www.googleapis.com/auth/spreadsheets',
|
14 |
+
'https://www.googleapis.com/auth/drive.file']
|
15 |
|
|
|
|
|
16 |
SPREADSHEET_ID = '1rcIJflbC1VIc70F6dnASLFvGQ90TXHhCx0iyxsXR7Ww'
|
17 |
FOLDER_ID = '1brmLNqOMCvRS0TDY6ECHvIBmlRx8pcPz'
|
18 |
|
19 |
+
# OAuth configuration
|
20 |
+
client_config = {
|
21 |
+
"web": {
|
22 |
+
"client_id": os.environ.get('GOOGLE_CLIENT_ID'),
|
23 |
+
"client_secret": os.environ.get('GOOGLE_CLIENT_SECRET'),
|
24 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
25 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
26 |
+
"redirect_uris": ["https://euler314-purchase-record.hf.space/oauth2callback"]
|
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 |
+
|
81 |
+
HTML_TEMPLATE = '''
|
82 |
+
<!DOCTYPE html>
|
83 |
+
<html>
|
84 |
+
<head>
|
85 |
+
<title>Purchase Record Form</title>
|
86 |
+
<meta charset="UTF-8">
|
87 |
+
<style>
|
88 |
+
body {
|
89 |
+
font-family: Arial, sans-serif;
|
90 |
+
max-width: 600px;
|
91 |
+
margin: 20px auto;
|
92 |
+
padding: 20px;
|
93 |
+
}
|
94 |
+
.form-group {
|
95 |
+
margin-bottom: 20px;
|
96 |
+
}
|
97 |
+
label {
|
98 |
+
display: block;
|
99 |
+
margin-bottom: 8px;
|
100 |
+
font-weight: bold;
|
101 |
+
}
|
102 |
+
input {
|
103 |
+
width: 100%;
|
104 |
+
padding: 10px;
|
105 |
+
border: 1px solid #ddd;
|
106 |
+
border-radius: 4px;
|
107 |
+
font-size: 16px;
|
108 |
+
}
|
109 |
+
button {
|
110 |
+
background-color: #4CAF50;
|
111 |
+
color: white;
|
112 |
+
padding: 12px 24px;
|
113 |
+
border: none;
|
114 |
+
border-radius: 4px;
|
115 |
+
cursor: pointer;
|
116 |
+
font-size: 16px;
|
117 |
+
width: 100%;
|
118 |
+
}
|
119 |
+
button:hover {
|
120 |
+
background-color: #45a049;
|
121 |
+
}
|
122 |
+
#status {
|
123 |
+
margin-top: 20px;
|
124 |
+
padding: 15px;
|
125 |
+
border-radius: 4px;
|
126 |
+
display: none;
|
127 |
+
}
|
128 |
+
.success {
|
129 |
+
background-color: #dff0d8;
|
130 |
+
color: #3c763d;
|
131 |
+
}
|
132 |
+
.error {
|
133 |
+
background-color: #f2dede;
|
134 |
+
color: #a94442;
|
135 |
+
}
|
136 |
+
</style>
|
137 |
+
</head>
|
138 |
+
<body>
|
139 |
+
<h2>購買記錄表單 Purchase Record Form</h2>
|
140 |
+
<form id="purchaseForm">
|
141 |
+
<div class="form-group">
|
142 |
+
<label for="name">姓名 Name:</label>
|
143 |
+
<input type="text" id="name" name="name" required>
|
144 |
+
</div>
|
145 |
+
|
146 |
+
<div class="form-group">
|
147 |
+
<label for="studentId">學號 Student ID:</label>
|
148 |
+
<input type="text" id="studentId" name="studentId" required>
|
149 |
+
</div>
|
150 |
+
|
151 |
+
<div class="form-group">
|
152 |
+
<label for="product">購買商品 Product:</label>
|
153 |
+
<input type="text" id="product" name="product" required>
|
154 |
+
</div>
|
155 |
+
|
156 |
+
<div class="form-group">
|
157 |
+
<label for="cost">金額 Cost:</label>
|
158 |
+
<input type="number" id="cost" name="cost" required>
|
159 |
+
</div>
|
160 |
+
|
161 |
+
<div class="form-group">
|
162 |
+
<label for="receipt">發票 Receipt:</label>
|
163 |
+
<input type="file" id="receipt" name="receipt" accept="image/*,.pdf" required>
|
164 |
+
</div>
|
165 |
+
|
166 |
+
<button type="submit">提交 Submit</button>
|
167 |
+
</form>
|
168 |
+
<div id="status"></div>
|
169 |
+
|
170 |
+
<script>
|
171 |
+
document.getElementById('purchaseForm').addEventListener('submit', async (e) => {
|
172 |
+
e.preventDefault();
|
173 |
|
174 |
+
const status = document.getElementById('status');
|
175 |
+
status.style.display = 'block';
|
176 |
+
status.textContent = '提交中... Submitting...';
|
177 |
+
status.className = '';
|
178 |
|
179 |
+
const formData = new FormData(e.target);
|
|
|
|
|
|
|
180 |
|
181 |
+
try {
|
182 |
+
const response = await fetch('/submit', {
|
183 |
+
method: 'POST',
|
184 |
+
body: formData
|
185 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
+
const result = await response.json();
|
188 |
|
189 |
+
if (result.success) {
|
190 |
+
status.className = 'success';
|
191 |
+
status.textContent = '提交成功! ' + result.message;
|
192 |
+
e.target.reset();
|
193 |
+
} else {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
status.className = 'error';
|
195 |
+
status.textContent = '錯誤:' + result.message;
|
196 |
}
|
197 |
+
} catch (error) {
|
198 |
+
status.className = 'error';
|
199 |
+
status.textContent = '提交錯誤 Error submitting form';
|
200 |
+
}
|
201 |
+
});
|
202 |
+
</script>
|
203 |
+
</body>
|
204 |
+
</html>
|
205 |
+
'''
|
206 |
+
|
207 |
+
@app.route('/')
|
208 |
+
def index():
|
209 |
+
return render_template_string(HTML_TEMPLATE)
|
210 |
|
211 |
@app.route('/submit', methods=['POST'])
|
212 |
def submit():
|
|
|
217 |
cost = request.form['cost']
|
218 |
file = request.files['receipt']
|
219 |
|
220 |
+
credentials = Credentials.from_authorized_user_info(CREDENTIALS)
|
221 |
+
|
222 |
# Upload file to Drive
|
223 |
drive_service = build('drive', 'v3', credentials=credentials)
|
224 |
|