Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
9efb272
1
Parent(s): de731eb
Update backup and restore functions to handle payment status and note fields
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
-
from datetime import datetime
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
from functools import wraps
|
|
@@ -271,62 +271,27 @@ def backup_settings():
|
|
| 271 |
|
| 272 |
@app.route('/api/admin/settings/restore', methods=['POST'])
|
| 273 |
@admin_required
|
| 274 |
-
def
|
| 275 |
try:
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
data = response.json()
|
| 279 |
|
| 280 |
-
|
| 281 |
-
return jsonify({'error': data.get('error', '從 Google Sheets 恢復資料失敗')}), 400
|
| 282 |
-
|
| 283 |
-
# 清空現有資料
|
| 284 |
Participant.query.delete()
|
| 285 |
-
Table.query.delete()
|
| 286 |
-
|
| 287 |
-
# 恢復桌次設定
|
| 288 |
-
tables = {} # 用於儲存 table_id 映射
|
| 289 |
-
for table_data in data['tables']:
|
| 290 |
-
table = Table(
|
| 291 |
-
name=table_data['name'],
|
| 292 |
-
capacity=table_data['capacity']
|
| 293 |
-
)
|
| 294 |
-
db.session.add(table)
|
| 295 |
-
db.session.flush() # 獲取新的 table.id
|
| 296 |
-
tables[str(table_data['id'])] = table.id # 將 id 轉換為字串
|
| 297 |
|
| 298 |
# 恢復參與者資料
|
| 299 |
-
for participant_data in
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
# 處理 ISO 格式的時間字串
|
| 303 |
-
created_at = datetime.strptime(
|
| 304 |
-
participant_data['created_at'].split('.')[0],
|
| 305 |
-
'%Y-%m-%dT%H:%M:%S'
|
| 306 |
-
)
|
| 307 |
-
except ValueError:
|
| 308 |
-
try:
|
| 309 |
-
# 如果上面失敗,嘗試處理原來的格式
|
| 310 |
-
created_at = datetime.strptime(
|
| 311 |
-
participant_data['created_at'],
|
| 312 |
-
'%Y-%m-%d %H:%M:%S'
|
| 313 |
-
)
|
| 314 |
-
except ValueError:
|
| 315 |
-
# 如果都失敗,使用當前時間
|
| 316 |
-
created_at = datetime.utcnow()
|
| 317 |
-
|
| 318 |
-
# 取得新的 table_id
|
| 319 |
-
old_table_id = str(participant_data['table_id'])
|
| 320 |
-
new_table_id = tables.get(old_table_id)
|
| 321 |
|
| 322 |
participant = Participant(
|
| 323 |
nickname=participant_data['nickname'],
|
| 324 |
email=participant_data['email'],
|
| 325 |
-
fb_link=participant_data.get('fb_link', ''),
|
| 326 |
introduction=participant_data.get('introduction', ''),
|
| 327 |
-
table_id=
|
| 328 |
seat_number=participant_data['seat_number'],
|
| 329 |
-
is_table_leader=participant_data.get('is_table_leader', False),
|
| 330 |
created_at=created_at,
|
| 331 |
has_paid=participant_data.get('has_paid', False),
|
| 332 |
note=participant_data.get('note', '')
|
|
@@ -334,11 +299,11 @@ def restore_settings():
|
|
| 334 |
db.session.add(participant)
|
| 335 |
|
| 336 |
db.session.commit()
|
| 337 |
-
return jsonify({'message': '資料已
|
| 338 |
|
| 339 |
except Exception as e:
|
| 340 |
db.session.rollback()
|
| 341 |
-
print('Error restoring data:', str(e))
|
| 342 |
return jsonify({'error': str(e)}), 500
|
| 343 |
|
| 344 |
@app.route('/api/admin/participants', methods=['GET'])
|
|
@@ -511,31 +476,60 @@ def register():
|
|
| 511 |
@app.route('/api/participants/export', methods=['GET'])
|
| 512 |
def export_participants():
|
| 513 |
try:
|
| 514 |
-
|
| 515 |
-
tables = Table.query.all()
|
| 516 |
export_data = []
|
| 517 |
|
| 518 |
-
for
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
'note': participant.note
|
| 533 |
-
})
|
| 534 |
|
| 535 |
return jsonify(export_data)
|
| 536 |
except Exception as e:
|
| 537 |
return jsonify({'error': str(e)}), 500
|
| 538 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
@app.route('/api/admin/participant/<int:participant_id>/toggle-leader', methods=['POST'])
|
| 540 |
@admin_required
|
| 541 |
def toggle_leader(participant_id):
|
|
|
|
| 1 |
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
from functools import wraps
|
|
|
|
| 271 |
|
| 272 |
@app.route('/api/admin/settings/restore', methods=['POST'])
|
| 273 |
@admin_required
|
| 274 |
+
def restore_data():
|
| 275 |
try:
|
| 276 |
+
data = request.get_json()
|
| 277 |
+
participants_data = data.get('participants', [])
|
|
|
|
| 278 |
|
| 279 |
+
# 清空現有的參與者資料
|
|
|
|
|
|
|
|
|
|
| 280 |
Participant.query.delete()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
# 恢復參與者資料
|
| 283 |
+
for participant_data in participants_data:
|
| 284 |
+
created_at = datetime.strptime(participant_data['created_at'], '%Y-%m-%d %H:%M:%S')
|
| 285 |
+
created_at = created_at - timedelta(hours=8) # 轉換回 UTC 時間
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
participant = Participant(
|
| 288 |
nickname=participant_data['nickname'],
|
| 289 |
email=participant_data['email'],
|
| 290 |
+
fb_link=participant_data.get('fb_link', ''),
|
| 291 |
introduction=participant_data.get('introduction', ''),
|
| 292 |
+
table_id=participant_data['table_id'],
|
| 293 |
seat_number=participant_data['seat_number'],
|
| 294 |
+
is_table_leader=participant_data.get('is_table_leader', False),
|
| 295 |
created_at=created_at,
|
| 296 |
has_paid=participant_data.get('has_paid', False),
|
| 297 |
note=participant_data.get('note', '')
|
|
|
|
| 299 |
db.session.add(participant)
|
| 300 |
|
| 301 |
db.session.commit()
|
| 302 |
+
return jsonify({'message': '資料已恢復'})
|
| 303 |
|
| 304 |
except Exception as e:
|
| 305 |
db.session.rollback()
|
| 306 |
+
print('Error restoring data:', str(e))
|
| 307 |
return jsonify({'error': str(e)}), 500
|
| 308 |
|
| 309 |
@app.route('/api/admin/participants', methods=['GET'])
|
|
|
|
| 476 |
@app.route('/api/participants/export', methods=['GET'])
|
| 477 |
def export_participants():
|
| 478 |
try:
|
| 479 |
+
participants = Participant.query.all()
|
|
|
|
| 480 |
export_data = []
|
| 481 |
|
| 482 |
+
for participant in participants:
|
| 483 |
+
local_time = participant.created_at + timedelta(hours=8) # 轉換為台灣時間
|
| 484 |
+
export_data.append({
|
| 485 |
+
'nickname': participant.nickname,
|
| 486 |
+
'email': participant.email,
|
| 487 |
+
'fb_link': participant.fb_link,
|
| 488 |
+
'introduction': participant.introduction,
|
| 489 |
+
'table_id': participant.table_id,
|
| 490 |
+
'seat_number': participant.seat_number,
|
| 491 |
+
'is_table_leader': '是' if participant.is_table_leader else '否',
|
| 492 |
+
'registration_time': local_time.strftime('%Y-%m-%d %H:%M:%S'),
|
| 493 |
+
'has_paid': '是' if participant.has_paid else '否',
|
| 494 |
+
'note': participant.note or ''
|
| 495 |
+
})
|
|
|
|
|
|
|
| 496 |
|
| 497 |
return jsonify(export_data)
|
| 498 |
except Exception as e:
|
| 499 |
return jsonify({'error': str(e)}), 500
|
| 500 |
|
| 501 |
+
@app.route('/api/admin/backup', methods=['POST'])
|
| 502 |
+
@admin_required
|
| 503 |
+
def backup_data():
|
| 504 |
+
try:
|
| 505 |
+
all_participants = Participant.query.all()
|
| 506 |
+
participants_data = []
|
| 507 |
+
|
| 508 |
+
for p in all_participants:
|
| 509 |
+
local_time = p.created_at + timedelta(hours=8) # 轉換為台灣時間
|
| 510 |
+
participants_data.append({
|
| 511 |
+
'id': p.id,
|
| 512 |
+
'nickname': p.nickname,
|
| 513 |
+
'email': p.email,
|
| 514 |
+
'fb_link': p.fb_link,
|
| 515 |
+
'introduction': p.introduction,
|
| 516 |
+
'table_id': p.table_id,
|
| 517 |
+
'seat_number': p.seat_number,
|
| 518 |
+
'is_table_leader': p.is_table_leader,
|
| 519 |
+
'created_at': local_time.strftime('%Y-%m-%d %H:%M:%S'),
|
| 520 |
+
'has_paid': p.has_paid,
|
| 521 |
+
'note': p.note or ''
|
| 522 |
+
})
|
| 523 |
+
|
| 524 |
+
sync_settings_to_sheets('backupParticipants', {
|
| 525 |
+
'participants': participants_data
|
| 526 |
+
})
|
| 527 |
+
|
| 528 |
+
return jsonify({'message': '備份成功'})
|
| 529 |
+
except Exception as e:
|
| 530 |
+
print("備份失敗:", str(e))
|
| 531 |
+
return jsonify({'error': str(e)}), 500
|
| 532 |
+
|
| 533 |
@app.route('/api/admin/participant/<int:participant_id>/toggle-leader', methods=['POST'])
|
| 534 |
@admin_required
|
| 535 |
def toggle_leader(participant_id):
|