ichieh commited on
Commit
65ec15e
·
1 Parent(s): 4953451

修改報名人同步問題

Browse files
Files changed (1) hide show
  1. app.py +59 -40
app.py CHANGED
@@ -245,6 +245,52 @@ def sync_settings_to_sheets(action, data):
245
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
246
  return {'success': False, 'error': str(e)}
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  @app.route('/')
249
  def index():
250
  return render_template('index.html')
@@ -538,7 +584,7 @@ def move_participant(participant_id):
538
 
539
  # 同步到 Google Sheets
540
  try:
541
- backup_data()
542
  print("成功備份參與者資料到 Google Sheets")
543
  except Exception as e:
544
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
@@ -568,7 +614,7 @@ def delete_participant(participant_id):
568
 
569
  # 同步到 Google Sheets
570
  try:
571
- backup_data()
572
  print("成功備份參與者資料到 Google Sheets")
573
  except Exception as e:
574
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
@@ -641,7 +687,7 @@ def register():
641
 
642
  # 同步到 Google Sheets
643
  try:
644
- backup_data()
645
  print("成功備份參與者資料到 Google Sheets")
646
  except Exception as e:
647
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
@@ -649,10 +695,15 @@ def register():
649
 
650
  return jsonify({
651
  'message': '報名成功',
652
- 'participant': participant.to_dict()
 
 
 
 
 
653
  })
 
654
  except Exception as e:
655
- print("報名時出錯:", str(e))
656
  return jsonify({'error': str(e)}), 500
657
 
658
  @app.route('/api/participants/export', methods=['GET'])
@@ -680,38 +731,6 @@ def export_participants():
680
  except Exception as e:
681
  return jsonify({'error': str(e)}), 500
682
 
683
- @app.route('/api/admin/backup', methods=['POST'])
684
- @admin_required
685
- def backup_data():
686
- try:
687
- all_participants = Participant.query.all()
688
- participants_data = []
689
-
690
- for p in all_participants:
691
- local_time = p.created_at + timedelta(hours=8) # 轉換為台灣時間
692
- participants_data.append({
693
- 'id': p.id,
694
- 'nickname': p.nickname,
695
- 'email': p.email,
696
- 'fb_link': p.fb_link,
697
- 'introduction': p.introduction,
698
- 'table_id': p.table_id,
699
- 'seat_number': p.seat_number,
700
- 'is_table_leader': p.is_table_leader,
701
- 'created_at': local_time.strftime('%Y-%m-%d %H:%M:%S'),
702
- 'has_paid': p.has_paid,
703
- 'note': p.note or ''
704
- })
705
-
706
- sync_settings_to_sheets('backupParticipants', {
707
- 'participants': participants_data
708
- })
709
-
710
- return jsonify({'message': '備份成功'})
711
- except Exception as e:
712
- print("備份失敗:", str(e))
713
- return jsonify({'error': str(e)}), 500
714
-
715
  @app.route('/api/admin/participant/<int:participant_id>/toggle-leader', methods=['POST'])
716
  @admin_required
717
  def toggle_leader(participant_id):
@@ -738,7 +757,7 @@ def toggle_leader(participant_id):
738
 
739
  # 同步到 Google Sheets
740
  try:
741
- backup_data()
742
  except Exception as e:
743
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
744
  # 不中斷操作,只記錄錯誤
@@ -764,7 +783,7 @@ def toggle_payment(participant_id):
764
 
765
  # 同步到 Google Sheets
766
  try:
767
- backup_data()
768
  except Exception as e:
769
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
770
  # 不中斷操作,只記錄錯誤
@@ -793,7 +812,7 @@ def update_participant_note(participant_id):
793
 
794
  # 同步到 Google Sheets
795
  try:
796
- backup_data()
797
  except Exception as e:
798
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
799
  # 不中斷操作,只記錄錯誤
 
245
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
246
  return {'success': False, 'error': str(e)}
247
 
248
+ def _backup_data_internal():
249
+ """內部函數:同步參與者資料到 Google Sheets"""
250
+ try:
251
+ all_participants = Participant.query.all()
252
+ participants_data = []
253
+
254
+ for p in all_participants:
255
+ local_time = p.created_at + timedelta(hours=8) # 轉換為台灣時間
256
+ participants_data.append({
257
+ 'id': p.id,
258
+ 'nickname': p.nickname,
259
+ 'email': p.email,
260
+ 'fb_link': p.fb_link,
261
+ 'introduction': p.introduction,
262
+ 'table_id': p.table_id,
263
+ 'seat_number': p.seat_number,
264
+ 'is_table_leader': p.is_table_leader,
265
+ 'created_at': local_time.strftime('%Y-%m-%d %H:%M:%S'),
266
+ 'has_paid': p.has_paid,
267
+ 'note': p.note or ''
268
+ })
269
+
270
+ # 同步到 Google Sheets
271
+ response = requests.post(SETTINGS_SCRIPT_URL, json={
272
+ 'action': 'backup_participants',
273
+ 'data': participants_data
274
+ })
275
+
276
+ if response.status_code != 200:
277
+ raise Exception(f"同步到 Google Sheets 失敗: {response.text}")
278
+
279
+ return True
280
+ except Exception as e:
281
+ print(f"備份資料時出錯: {str(e)}")
282
+ raise e
283
+
284
+ @app.route('/api/admin/backup', methods=['POST'])
285
+ @admin_required
286
+ def backup_data():
287
+ """API 路由:備份參與者資料(需要管理員權限)"""
288
+ try:
289
+ _backup_data_internal()
290
+ return jsonify({'message': '備份成功'})
291
+ except Exception as e:
292
+ return jsonify({'error': str(e)}), 500
293
+
294
  @app.route('/')
295
  def index():
296
  return render_template('index.html')
 
584
 
585
  # 同步到 Google Sheets
586
  try:
587
+ _backup_data_internal() # 使用內部函數而不是 API 路由
588
  print("成功備份參與者資料到 Google Sheets")
589
  except Exception as e:
590
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
 
614
 
615
  # 同步到 Google Sheets
616
  try:
617
+ _backup_data_internal() # 使用內部函數而不是 API 路由
618
  print("成功備份參與者資料到 Google Sheets")
619
  except Exception as e:
620
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
 
687
 
688
  # 同步到 Google Sheets
689
  try:
690
+ _backup_data_internal() # 使用內部函數而不是 API 路由
691
  print("成功備份參與者資料到 Google Sheets")
692
  except Exception as e:
693
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
 
695
 
696
  return jsonify({
697
  'message': '報名成功',
698
+ 'participant': {
699
+ 'id': participant.id,
700
+ 'nickname': participant.nickname,
701
+ 'table_id': participant.table_id,
702
+ 'seat_number': participant.seat_number
703
+ }
704
  })
705
+
706
  except Exception as e:
 
707
  return jsonify({'error': str(e)}), 500
708
 
709
  @app.route('/api/participants/export', methods=['GET'])
 
731
  except Exception as e:
732
  return jsonify({'error': str(e)}), 500
733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
734
  @app.route('/api/admin/participant/<int:participant_id>/toggle-leader', methods=['POST'])
735
  @admin_required
736
  def toggle_leader(participant_id):
 
757
 
758
  # 同步到 Google Sheets
759
  try:
760
+ _backup_data_internal() # 使用內部函數而不是 API 路由
761
  except Exception as e:
762
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
763
  # 不中斷操作,只記錄錯誤
 
783
 
784
  # 同步到 Google Sheets
785
  try:
786
+ _backup_data_internal() # 使用內部函數而不是 API 路由
787
  except Exception as e:
788
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
789
  # 不中斷操作,只記錄錯誤
 
812
 
813
  # 同步到 Google Sheets
814
  try:
815
+ _backup_data_internal() # 使用內部函數而不是 API 路由
816
  except Exception as e:
817
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
818
  # 不中斷操作,只記錄錯誤