ichieh commited on
Commit
c46b415
·
1 Parent(s): 89b794e

Add detailed error logging for data restoration

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -274,28 +274,38 @@ def backup_settings():
274
  def restore_data():
275
  try:
276
  # 從 Google Apps Script 獲取數據
 
277
  response = requests.get(SETTINGS_SCRIPT_URL)
 
 
 
278
 
279
  try:
280
  data = response.json()
281
- except ValueError:
282
- print("Error parsing JSON:", response.text)
283
- return jsonify({'error': '從 Google Sheets 獲取資料失敗'}), 500
 
284
 
285
  if not data.get('success'):
286
- return jsonify({'error': data.get('error', '從 Google Sheets 恢復資料失敗')}), 400
 
 
287
 
288
  # 清空現有的參與者資料
289
  Participant.query.delete()
290
 
291
  # 恢復參與者資料
292
  participants_data = data.get('participants', [])
 
 
293
  for participant_data in participants_data:
294
  # 解析時間
295
  try:
296
  created_at = datetime.strptime(participant_data['created_at'], '%Y-%m-%d %H:%M:%S')
297
  created_at = created_at - timedelta(hours=8) # 轉換回 UTC 時間
298
- except (ValueError, KeyError):
 
299
  created_at = datetime.utcnow()
300
 
301
  try:
@@ -312,17 +322,19 @@ def restore_data():
312
  note=participant_data.get('note', '')
313
  )
314
  db.session.add(participant)
 
315
  except (KeyError, ValueError) as e:
316
- print(f"Error processing participant data: {str(e)}")
317
- print(f"Problematic data: {participant_data}")
318
  continue
319
 
320
  db.session.commit()
 
321
  return jsonify({'message': '資料已成功恢復'})
322
 
323
  except Exception as e:
324
  db.session.rollback()
325
- print('Error restoring data:', str(e))
326
  return jsonify({'error': str(e)}), 500
327
 
328
  @app.route('/api/admin/participants', methods=['GET'])
 
274
  def restore_data():
275
  try:
276
  # 從 Google Apps Script 獲取數據
277
+ print("正在從 Google Apps Script 獲取數據...")
278
  response = requests.get(SETTINGS_SCRIPT_URL)
279
+ print("Response status code:", response.status_code)
280
+ print("Response headers:", response.headers)
281
+ print("Response content:", response.text)
282
 
283
  try:
284
  data = response.json()
285
+ except ValueError as e:
286
+ print("JSON 解析錯誤:", str(e))
287
+ print("原始回應內容:", response.text)
288
+ return jsonify({'error': '從 Google Sheets 獲取資料失敗:無效的 JSON 格式'}), 500
289
 
290
  if not data.get('success'):
291
+ error_msg = data.get('error', '從 Google Sheets 恢復資料失敗')
292
+ print("Google Apps Script 返回錯誤:", error_msg)
293
+ return jsonify({'error': error_msg}), 400
294
 
295
  # 清空現有的參與者資料
296
  Participant.query.delete()
297
 
298
  # 恢復參與者資料
299
  participants_data = data.get('participants', [])
300
+ print(f"找到 {len(participants_data)} 個參與者資料")
301
+
302
  for participant_data in participants_data:
303
  # 解析時間
304
  try:
305
  created_at = datetime.strptime(participant_data['created_at'], '%Y-%m-%d %H:%M:%S')
306
  created_at = created_at - timedelta(hours=8) # 轉換回 UTC 時間
307
+ except (ValueError, KeyError) as e:
308
+ print(f"時間解析錯誤: {str(e)}")
309
  created_at = datetime.utcnow()
310
 
311
  try:
 
322
  note=participant_data.get('note', '')
323
  )
324
  db.session.add(participant)
325
+ print(f"已添加參與者: {participant.nickname}")
326
  except (KeyError, ValueError) as e:
327
+ print(f"處理參與者資料時出錯: {str(e)}")
328
+ print(f"問題資料: {participant_data}")
329
  continue
330
 
331
  db.session.commit()
332
+ print("資料恢復完成")
333
  return jsonify({'message': '資料已成功恢復'})
334
 
335
  except Exception as e:
336
  db.session.rollback()
337
+ print('恢復資料時出錯:', str(e))
338
  return jsonify({'error': str(e)}), 500
339
 
340
  @app.route('/api/admin/participants', methods=['GET'])