Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
f3937f7
1
Parent(s): 77adb92
fix: 將環境變數移到 Flask 配置中
Browse files
app.py
CHANGED
|
@@ -16,7 +16,9 @@ app = Flask(__name__, instance_relative_config=True)
|
|
| 16 |
# 設置 session secret key
|
| 17 |
app.secret_key = os.urandom(24)
|
| 18 |
|
| 19 |
-
# 設置管理者密碼
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# 設置資料庫路徑 - 使用持久化存儲
|
| 22 |
if os.environ.get('SPACE_ID'): # 在 Hugging Face Spaces 環境中
|
|
@@ -136,7 +138,7 @@ with app.app_context():
|
|
| 136 |
if Participant.query.count() == 0:
|
| 137 |
print("資料庫為空,嘗試從 Google Sheets 恢復資料...")
|
| 138 |
try:
|
| 139 |
-
response = requests.get(SETTINGS_SCRIPT_URL)
|
| 140 |
data = response.json()
|
| 141 |
|
| 142 |
if data.get('success'):
|
|
@@ -213,11 +215,11 @@ SETTINGS_SCRIPT_URL = os.environ.get('SETTINGS_SCRIPT_URL', '')
|
|
| 213 |
|
| 214 |
def sync_settings_to_sheets(action, data):
|
| 215 |
"""同步設定到 Google Sheets"""
|
| 216 |
-
if not SETTINGS_SCRIPT_URL:
|
| 217 |
return None
|
| 218 |
|
| 219 |
try:
|
| 220 |
-
response = requests.post(SETTINGS_SCRIPT_URL, json={
|
| 221 |
'action': action,
|
| 222 |
**data
|
| 223 |
})
|
|
@@ -242,7 +244,7 @@ def admin():
|
|
| 242 |
@app.route('/api/admin/login', methods=['POST'])
|
| 243 |
def admin_login_api():
|
| 244 |
data = request.json
|
| 245 |
-
if data.get('password') == ADMIN_PASSWORD:
|
| 246 |
session['admin_logged_in'] = True
|
| 247 |
return jsonify({'success': True, 'message': 'Login successful'}), 200
|
| 248 |
return jsonify({'success': False, 'error': 'Invalid password'}), 401
|
|
@@ -352,7 +354,7 @@ def restore_data():
|
|
| 352 |
try:
|
| 353 |
# 從 Google Apps Script 獲取數據
|
| 354 |
print("正在從 Google Apps Script 獲取數據...")
|
| 355 |
-
response = requests.get(SETTINGS_SCRIPT_URL)
|
| 356 |
print("Response status code:", response.status_code)
|
| 357 |
print("Response headers:", response.headers)
|
| 358 |
print("Response content:", response.text)
|
|
|
|
| 16 |
# 設置 session secret key
|
| 17 |
app.secret_key = os.urandom(24)
|
| 18 |
|
| 19 |
+
# 設置管理者密碼和 Google Apps Script URL
|
| 20 |
+
app.config['ADMIN_PASSWORD'] = ADMIN_PASSWORD
|
| 21 |
+
app.config['SETTINGS_SCRIPT_URL'] = SETTINGS_SCRIPT_URL
|
| 22 |
|
| 23 |
# 設置資料庫路徑 - 使用持久化存儲
|
| 24 |
if os.environ.get('SPACE_ID'): # 在 Hugging Face Spaces 環境中
|
|
|
|
| 138 |
if Participant.query.count() == 0:
|
| 139 |
print("資料庫為空,嘗試從 Google Sheets 恢復資料...")
|
| 140 |
try:
|
| 141 |
+
response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
|
| 142 |
data = response.json()
|
| 143 |
|
| 144 |
if data.get('success'):
|
|
|
|
| 215 |
|
| 216 |
def sync_settings_to_sheets(action, data):
|
| 217 |
"""同步設定到 Google Sheets"""
|
| 218 |
+
if not app.config['SETTINGS_SCRIPT_URL']:
|
| 219 |
return None
|
| 220 |
|
| 221 |
try:
|
| 222 |
+
response = requests.post(app.config['SETTINGS_SCRIPT_URL'], json={
|
| 223 |
'action': action,
|
| 224 |
**data
|
| 225 |
})
|
|
|
|
| 244 |
@app.route('/api/admin/login', methods=['POST'])
|
| 245 |
def admin_login_api():
|
| 246 |
data = request.json
|
| 247 |
+
if data.get('password') == app.config['ADMIN_PASSWORD']:
|
| 248 |
session['admin_logged_in'] = True
|
| 249 |
return jsonify({'success': True, 'message': 'Login successful'}), 200
|
| 250 |
return jsonify({'success': False, 'error': 'Invalid password'}), 401
|
|
|
|
| 354 |
try:
|
| 355 |
# 從 Google Apps Script 獲取數據
|
| 356 |
print("正在從 Google Apps Script 獲取數據...")
|
| 357 |
+
response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
|
| 358 |
print("Response status code:", response.status_code)
|
| 359 |
print("Response headers:", response.headers)
|
| 360 |
print("Response content:", response.text)
|