Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
b2c849d
1
Parent(s): 4f4e7f8
fix: 改進資料庫初始化和連接邏輯
Browse files1. 添加更詳細的錯誤處理和日誌記錄
2. 改進資料庫初始化邏輯,確保表格先被創建
3. 優化參與者資料處理邏輯
app.py
CHANGED
|
@@ -130,16 +130,22 @@ def init_db():
|
|
| 130 |
# 在應用啟動時初始化資料庫
|
| 131 |
with app.app_context():
|
| 132 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# 檢查資料庫連接
|
| 134 |
db.session.execute('SELECT 1')
|
| 135 |
print("資料庫初始化成功")
|
| 136 |
|
| 137 |
-
# 如果
|
| 138 |
-
if
|
| 139 |
-
print("資料
|
| 140 |
try:
|
| 141 |
response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
|
|
|
|
| 142 |
data = response.json()
|
|
|
|
| 143 |
|
| 144 |
if data.get('success'):
|
| 145 |
# 首先恢復設定
|
|
@@ -168,40 +174,35 @@ with app.app_context():
|
|
| 168 |
try:
|
| 169 |
created_at = datetime.strptime(participant_data['created_at'], '%Y-%m-%d %H:%M:%S')
|
| 170 |
created_at = created_at - timedelta(hours=8) # 轉換回 UTC 時間
|
| 171 |
-
|
| 172 |
-
created_at = datetime.utcnow()
|
| 173 |
-
|
| 174 |
-
try:
|
| 175 |
participant = Participant(
|
| 176 |
nickname=participant_data['nickname'],
|
| 177 |
email=participant_data['email'],
|
| 178 |
fb_link=participant_data.get('fb_link', ''),
|
| 179 |
introduction=participant_data.get('introduction', ''),
|
| 180 |
-
table_id=
|
| 181 |
-
seat_number=
|
| 182 |
-
is_table_leader=
|
| 183 |
created_at=created_at,
|
| 184 |
-
has_paid=
|
| 185 |
note=participant_data.get('note', '')
|
| 186 |
)
|
| 187 |
db.session.add(participant)
|
| 188 |
print(f"已添加參與者: {participant.nickname}")
|
| 189 |
-
except
|
| 190 |
print(f"處理參與者資料時出錯: {str(e)}")
|
| 191 |
print(f"問題資料: {participant_data}")
|
| 192 |
continue
|
| 193 |
|
| 194 |
db.session.commit()
|
| 195 |
-
print("資料恢復完成")
|
| 196 |
else:
|
| 197 |
-
print("從 Google Sheets 恢復資料失敗:
|
| 198 |
except Exception as e:
|
| 199 |
-
print("恢復資料時出錯:
|
| 200 |
-
|
| 201 |
-
print(f"資料庫中已有 {Participant.query.count()} 個參與者,跳過資料恢復")
|
| 202 |
-
|
| 203 |
except Exception as e:
|
| 204 |
-
print("初始化資料庫時出錯:
|
| 205 |
|
| 206 |
# 管理者認證裝飾器
|
| 207 |
def admin_required(f):
|
|
@@ -507,8 +508,11 @@ def delete_participant(participant_id):
|
|
| 507 |
@app.route('/api/tables')
|
| 508 |
def get_tables():
|
| 509 |
try:
|
|
|
|
| 510 |
tables = Table.query.order_by(Table.id).all()
|
| 511 |
-
|
|
|
|
|
|
|
| 512 |
'id': table.id,
|
| 513 |
'name': table.name,
|
| 514 |
'capacity': table.capacity,
|
|
@@ -519,8 +523,13 @@ def get_tables():
|
|
| 519 |
'introduction': p.introduction,
|
| 520 |
'is_table_leader': p.is_table_leader
|
| 521 |
} for p in sorted(table.participants, key=lambda x: (not x.is_table_leader, x.created_at))]
|
| 522 |
-
} for table in tables]
|
|
|
|
|
|
|
|
|
|
| 523 |
except Exception as e:
|
|
|
|
|
|
|
| 524 |
return jsonify({'error': str(e)}), 500
|
| 525 |
|
| 526 |
@app.route('/api/register', methods=['POST'])
|
|
|
|
| 130 |
# 在應用啟動時初始化資料庫
|
| 131 |
with app.app_context():
|
| 132 |
try:
|
| 133 |
+
# 創建資料庫表格
|
| 134 |
+
db.create_all()
|
| 135 |
+
print("資料庫表格創建成功")
|
| 136 |
+
|
| 137 |
# 檢查資料庫連接
|
| 138 |
db.session.execute('SELECT 1')
|
| 139 |
print("資料庫初始化成功")
|
| 140 |
|
| 141 |
+
# 如果沒有桌子,從 Google Sheets 恢復資料
|
| 142 |
+
if Table.query.count() == 0:
|
| 143 |
+
print("沒有桌子資料,嘗試從 Google Sheets 恢復資料...")
|
| 144 |
try:
|
| 145 |
response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
|
| 146 |
+
print(f"Google Sheets 回應狀態碼: {response.status_code}") # 添加日誌
|
| 147 |
data = response.json()
|
| 148 |
+
print(f"Google Sheets 回應內容: {data}") # 添加日誌
|
| 149 |
|
| 150 |
if data.get('success'):
|
| 151 |
# 首先恢復設定
|
|
|
|
| 174 |
try:
|
| 175 |
created_at = datetime.strptime(participant_data['created_at'], '%Y-%m-%d %H:%M:%S')
|
| 176 |
created_at = created_at - timedelta(hours=8) # 轉換回 UTC 時間
|
| 177 |
+
|
|
|
|
|
|
|
|
|
|
| 178 |
participant = Participant(
|
| 179 |
nickname=participant_data['nickname'],
|
| 180 |
email=participant_data['email'],
|
| 181 |
fb_link=participant_data.get('fb_link', ''),
|
| 182 |
introduction=participant_data.get('introduction', ''),
|
| 183 |
+
table_id=participant_data.get('table_id'),
|
| 184 |
+
seat_number=participant_data.get('seat_number'),
|
| 185 |
+
is_table_leader=participant_data.get('is_table_leader', False),
|
| 186 |
created_at=created_at,
|
| 187 |
+
has_paid=participant_data.get('has_paid', False),
|
| 188 |
note=participant_data.get('note', '')
|
| 189 |
)
|
| 190 |
db.session.add(participant)
|
| 191 |
print(f"已添加參與者: {participant.nickname}")
|
| 192 |
+
except Exception as e:
|
| 193 |
print(f"處理參與者資料時出錯: {str(e)}")
|
| 194 |
print(f"問題資料: {participant_data}")
|
| 195 |
continue
|
| 196 |
|
| 197 |
db.session.commit()
|
| 198 |
+
print("參與者資料恢復完成")
|
| 199 |
else:
|
| 200 |
+
print(f"從 Google Sheets 恢復資料失敗: {data.get('error', '未知錯誤')}")
|
| 201 |
except Exception as e:
|
| 202 |
+
print(f"從 Google Sheets 恢復資料時出錯: {str(e)}")
|
| 203 |
+
|
|
|
|
|
|
|
| 204 |
except Exception as e:
|
| 205 |
+
print(f"初始化資料庫時出錯: {str(e)}")
|
| 206 |
|
| 207 |
# 管理者認證裝飾器
|
| 208 |
def admin_required(f):
|
|
|
|
| 508 |
@app.route('/api/tables')
|
| 509 |
def get_tables():
|
| 510 |
try:
|
| 511 |
+
print("正在獲取桌次資料...") # 添加日誌
|
| 512 |
tables = Table.query.order_by(Table.id).all()
|
| 513 |
+
print(f"找到 {len(tables)} 張桌子") # 添加日誌
|
| 514 |
+
|
| 515 |
+
result = [{
|
| 516 |
'id': table.id,
|
| 517 |
'name': table.name,
|
| 518 |
'capacity': table.capacity,
|
|
|
|
| 523 |
'introduction': p.introduction,
|
| 524 |
'is_table_leader': p.is_table_leader
|
| 525 |
} for p in sorted(table.participants, key=lambda x: (not x.is_table_leader, x.created_at))]
|
| 526 |
+
} for table in tables]
|
| 527 |
+
|
| 528 |
+
print("桌次資料獲取成功") # 添加日誌
|
| 529 |
+
return jsonify(result)
|
| 530 |
except Exception as e:
|
| 531 |
+
print(f"獲取桌次資料時出錯: {str(e)}") # 添加錯誤日誌
|
| 532 |
+
db.session.rollback()
|
| 533 |
return jsonify({'error': str(e)}), 500
|
| 534 |
|
| 535 |
@app.route('/api/register', methods=['POST'])
|