Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
3978799
1
Parent(s): 1d76828
Improve data restore error handling and user experience
Browse files- app.py +27 -16
- templates/admin.html +44 -15
app.py
CHANGED
|
@@ -275,7 +275,12 @@ def restore_data():
|
|
| 275 |
try:
|
| 276 |
# 從 Google Apps Script 獲取數據
|
| 277 |
response = requests.get(SETTINGS_SCRIPT_URL)
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
if not data.get('success'):
|
| 281 |
return jsonify({'error': data.get('error', '從 Google Sheets 恢復資料失敗')}), 400
|
|
@@ -284,27 +289,33 @@ def restore_data():
|
|
| 284 |
Participant.query.delete()
|
| 285 |
|
| 286 |
# 恢復參與者資料
|
| 287 |
-
|
|
|
|
| 288 |
# 解析時間
|
| 289 |
try:
|
| 290 |
created_at = datetime.strptime(participant_data['created_at'], '%Y-%m-%d %H:%M:%S')
|
| 291 |
created_at = created_at - timedelta(hours=8) # 轉換回 UTC 時間
|
| 292 |
-
except ValueError:
|
| 293 |
created_at = datetime.utcnow()
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
db.session.commit()
|
| 310 |
return jsonify({'message': '資料已成功恢復'})
|
|
|
|
| 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
|
|
|
|
| 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:
|
| 302 |
+
participant = Participant(
|
| 303 |
+
nickname=participant_data['nickname'],
|
| 304 |
+
email=participant_data['email'],
|
| 305 |
+
fb_link=participant_data.get('fb_link', ''),
|
| 306 |
+
introduction=participant_data.get('introduction', ''),
|
| 307 |
+
table_id=int(participant_data['table_id']),
|
| 308 |
+
seat_number=int(participant_data['seat_number']),
|
| 309 |
+
is_table_leader=bool(participant_data.get('is_table_leader', False)),
|
| 310 |
+
created_at=created_at,
|
| 311 |
+
has_paid=bool(participant_data.get('has_paid', False)),
|
| 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': '資料已成功恢復'})
|
templates/admin.html
CHANGED
|
@@ -429,26 +429,55 @@
|
|
| 429 |
// 從 Google Sheets 恢復資料
|
| 430 |
async function restoreData() {
|
| 431 |
try {
|
| 432 |
-
const
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
});
|
| 438 |
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
if (response.ok) {
|
| 442 |
Swal.fire({
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
| 446 |
showConfirmButton: false,
|
| 447 |
-
|
|
|
|
|
|
|
| 448 |
});
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
}
|
| 453 |
} catch (error) {
|
| 454 |
console.error('Error:', error);
|
|
|
|
| 429 |
// 從 Google Sheets 恢復資料
|
| 430 |
async function restoreData() {
|
| 431 |
try {
|
| 432 |
+
const result = await Swal.fire({
|
| 433 |
+
title: '確認恢復資料?',
|
| 434 |
+
text: '這將會覆蓋目前的所有資料!',
|
| 435 |
+
icon: 'warning',
|
| 436 |
+
showCancelButton: true,
|
| 437 |
+
confirmButtonColor: '#3085d6',
|
| 438 |
+
cancelButtonColor: '#d33',
|
| 439 |
+
confirmButtonText: '是,恢復資料',
|
| 440 |
+
cancelButtonText: '取消'
|
| 441 |
});
|
| 442 |
|
| 443 |
+
if (result.isConfirmed) {
|
| 444 |
+
// 顯示載入中
|
|
|
|
| 445 |
Swal.fire({
|
| 446 |
+
title: '處理中...',
|
| 447 |
+
text: '正在從 Google Sheets 恢復資料',
|
| 448 |
+
allowOutsideClick: false,
|
| 449 |
+
allowEscapeKey: false,
|
| 450 |
+
allowEnterKey: false,
|
| 451 |
showConfirmButton: false,
|
| 452 |
+
didOpen: () => {
|
| 453 |
+
Swal.showLoading();
|
| 454 |
+
}
|
| 455 |
});
|
| 456 |
+
|
| 457 |
+
const response = await fetch('/api/admin/restore', {
|
| 458 |
+
method: 'POST',
|
| 459 |
+
headers: {
|
| 460 |
+
'Content-Type': 'application/json'
|
| 461 |
+
}
|
| 462 |
+
});
|
| 463 |
+
|
| 464 |
+
const data = await response.json();
|
| 465 |
+
|
| 466 |
+
// 關閉載入中
|
| 467 |
+
await Swal.close();
|
| 468 |
+
|
| 469 |
+
if (response.ok) {
|
| 470 |
+
await Swal.fire({
|
| 471 |
+
icon: 'success',
|
| 472 |
+
title: '成功',
|
| 473 |
+
text: data.message,
|
| 474 |
+
showConfirmButton: false,
|
| 475 |
+
timer: 1500
|
| 476 |
+
});
|
| 477 |
+
await loadParticipants();
|
| 478 |
+
} else {
|
| 479 |
+
throw new Error(data.error || '恢復資料失敗');
|
| 480 |
+
}
|
| 481 |
}
|
| 482 |
} catch (error) {
|
| 483 |
console.error('Error:', error);
|