ichieh commited on
Commit
08794f7
·
1 Parent(s): a377724

增加桌次命名

Browse files
Files changed (2) hide show
  1. app.py +39 -53
  2. sync_settings.gs +31 -0
app.py CHANGED
@@ -621,47 +621,21 @@ def register():
621
  db.session.add(participant)
622
  db.session.commit()
623
 
624
- # 自動備份到 Google Sheets
625
  try:
626
- # 獲取所有參與者資料
627
- all_participants = Participant.query.all()
628
- sync_settings_to_sheets('backupParticipants', {
629
- 'participants': [{
630
- 'id': p.id,
631
- 'nickname': p.nickname,
632
- 'email': p.email,
633
- 'fb_link': p.fb_link,
634
- 'introduction': p.introduction,
635
- 'table_id': p.table_id,
636
- 'seat_number': p.seat_number,
637
- 'is_table_leader': p.is_table_leader,
638
- 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S'),
639
- 'has_paid': p.has_paid,
640
- 'note': p.note
641
- } for p in all_participants]
642
- })
643
  print("成功備份參與者資料到 Google Sheets")
644
  except Exception as e:
645
- print(f"備份到 Google Sheets 時發生錯誤: {str(e)}")
646
- # 不要因為備份失敗而影響註冊流程
647
- pass
648
 
649
  return jsonify({
650
  'message': '報名成功',
651
- 'participant': {
652
- 'id': participant.id,
653
- 'nickname': participant.nickname,
654
- 'email': participant.email,
655
- 'fb_link': participant.fb_link,
656
- 'introduction': participant.introduction,
657
- 'table_id': participant.table_id,
658
- 'seat_number': participant.seat_number
659
- }
660
  })
661
-
662
  except Exception as e:
663
- db.session.rollback()
664
- return jsonify({'error': str(e)}), 400
665
 
666
  @app.route('/api/participants/export', methods=['GET'])
667
  def export_participants():
@@ -744,62 +718,74 @@ def toggle_leader(participant_id):
744
  participant.is_table_leader = not participant.is_table_leader
745
  db.session.commit()
746
 
 
 
 
 
 
 
 
747
  return jsonify({
748
  'message': f'已{participant.is_table_leader and "設為" or "取消"}桌長',
749
  'participant': participant.to_dict()
750
  })
751
  except Exception as e:
752
- db.session.rollback()
753
  return jsonify({'error': str(e)}), 500
754
 
755
  @app.route('/api/admin/participant/<int:participant_id>/payment', methods=['POST'])
756
  @admin_required
757
  def toggle_payment(participant_id):
758
  try:
759
- participant = Participant.query.get_or_404(participant_id)
 
 
 
760
  participant.has_paid = not participant.has_paid
761
  db.session.commit()
762
 
763
- # 自動備份到 Google Sheets
764
  try:
765
- all_participants = Participant.query.all()
766
- sync_settings_to_sheets('backupParticipants', {
767
- 'participants': [p.to_dict() for p in all_participants]
768
- })
769
  except Exception as e:
770
- print(f"備份到 Google Sheets 時發生錯誤: {str(e)}")
771
- pass
772
 
773
  return jsonify({
774
- 'message': '付款狀態已更新',
775
- 'has_paid': participant.has_paid
776
  })
777
  except Exception as e:
778
- db.session.rollback()
779
  return jsonify({'error': str(e)}), 500
780
 
781
  @app.route('/api/admin/participant/<int:participant_id>/note', methods=['POST'])
782
  @admin_required
783
  def update_participant_note(participant_id):
784
  try:
785
- participant = Participant.query.get_or_404(participant_id)
786
  data = request.get_json()
 
787
 
788
- participant.note = data.get('note', '')
 
 
 
 
789
  db.session.commit()
790
 
791
- # 備份到 Google Sheets
792
  try:
793
  backup_data()
794
- print(f"已更新參與者 {participant.nickname} 的備註並同步到 Google Sheets")
795
  except Exception as e:
796
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
797
- # 不要讓同步錯誤影響主要功能
798
- pass
799
 
800
- return jsonify({'message': '備註已更新'})
 
 
 
801
  except Exception as e:
802
- db.session.rollback()
803
  return jsonify({'error': str(e)}), 500
804
 
805
  if __name__ == '__main__':
 
621
  db.session.add(participant)
622
  db.session.commit()
623
 
624
+ # 同步到 Google Sheets
625
  try:
626
+ backup_data()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
627
  print("成功備份參與者資料到 Google Sheets")
628
  except Exception as e:
629
+ print(f"同步到 Google Sheets 時出錯: {str(e)}")
630
+ # 不中斷操作,只記錄錯誤
 
631
 
632
  return jsonify({
633
  'message': '報名成功',
634
+ 'participant': participant.to_dict()
 
 
 
 
 
 
 
 
635
  })
 
636
  except Exception as e:
637
+ print("報名時出錯:", str(e))
638
+ return jsonify({'error': str(e)}), 500
639
 
640
  @app.route('/api/participants/export', methods=['GET'])
641
  def export_participants():
 
718
  participant.is_table_leader = not participant.is_table_leader
719
  db.session.commit()
720
 
721
+ # 同步到 Google Sheets
722
+ try:
723
+ backup_data()
724
+ except Exception as e:
725
+ print(f"同步到 Google Sheets 時出錯: {str(e)}")
726
+ # 不中斷操作,只記錄錯誤
727
+
728
  return jsonify({
729
  'message': f'已{participant.is_table_leader and "設為" or "取消"}桌長',
730
  'participant': participant.to_dict()
731
  })
732
  except Exception as e:
733
+ print("更新桌長狀態時出錯:", str(e))
734
  return jsonify({'error': str(e)}), 500
735
 
736
  @app.route('/api/admin/participant/<int:participant_id>/payment', methods=['POST'])
737
  @admin_required
738
  def toggle_payment(participant_id):
739
  try:
740
+ participant = Participant.query.get(participant_id)
741
+ if not participant:
742
+ return jsonify({'error': '找不到該參與者'}), 404
743
+
744
  participant.has_paid = not participant.has_paid
745
  db.session.commit()
746
 
747
+ # 同步到 Google Sheets
748
  try:
749
+ backup_data()
 
 
 
750
  except Exception as e:
751
+ print(f"同步到 Google Sheets 時出錯: {str(e)}")
752
+ # 不中斷操作,只記錄錯誤
753
 
754
  return jsonify({
755
+ 'message': f'已更新付款狀態為{"已" if participant.has_paid else "未"}付款',
756
+ 'participant': participant.to_dict()
757
  })
758
  except Exception as e:
759
+ print("更新付款狀態時出錯:", str(e))
760
  return jsonify({'error': str(e)}), 500
761
 
762
  @app.route('/api/admin/participant/<int:participant_id>/note', methods=['POST'])
763
  @admin_required
764
  def update_participant_note(participant_id):
765
  try:
 
766
  data = request.get_json()
767
+ note = data.get('note', '')
768
 
769
+ participant = Participant.query.get(participant_id)
770
+ if not participant:
771
+ return jsonify({'error': '找不到該參與者'}), 404
772
+
773
+ participant.note = note
774
  db.session.commit()
775
 
776
+ # 同步到 Google Sheets
777
  try:
778
  backup_data()
 
779
  except Exception as e:
780
  print(f"同步到 Google Sheets 時出錯: {str(e)}")
781
+ # 不中斷操作,只記錄錯誤
 
782
 
783
+ return jsonify({
784
+ 'message': '備註已更新',
785
+ 'participant': participant.to_dict()
786
+ })
787
  except Exception as e:
788
+ print("更新備註時出錯:", str(e))
789
  return jsonify({'error': str(e)}), 500
790
 
791
  if __name__ == '__main__':
sync_settings.gs CHANGED
@@ -192,6 +192,37 @@ function updateSettings(sheet, settings) {
192
  ['每桌人數', seats_per_table]
193
  ]);
194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  return ContentService.createTextOutput(JSON.stringify({
196
  success: true,
197
  message: 'Settings updated successfully',
 
192
  ['每桌人數', seats_per_table]
193
  ]);
194
 
195
+ // 初始化或更新 Tables 工作表
196
+ var tablesSheet = sheet.getSheetByName('Tables') || sheet.insertSheet('Tables');
197
+
198
+ // 如果是新工作表,設置標題行
199
+ if (tablesSheet.getLastRow() === 0) {
200
+ tablesSheet.getRange('A1:B1').setValues([['Table ID', 'Table Name']]);
201
+ }
202
+
203
+ // 獲取現有的桌次數據
204
+ var existingData = tablesSheet.getDataRange().getValues();
205
+ var existingTables = {};
206
+ for (var i = 1; i < existingData.length; i++) {
207
+ existingTables[existingData[i][0]] = existingData[i][1];
208
+ }
209
+
210
+ // 準備新的桌次數據
211
+ var tableValues = [];
212
+ for (var i = 1; i <= total_tables; i++) {
213
+ tableValues.push([i, existingTables[i] || '']); // 保留現有的桌次名稱
214
+ }
215
+
216
+ // 清除現有數據(保留標題行)
217
+ if (tablesSheet.getLastRow() > 1) {
218
+ tablesSheet.getRange(2, 1, tablesSheet.getLastRow() - 1, 2).clear();
219
+ }
220
+
221
+ // 寫入新數據
222
+ if (tableValues.length > 0) {
223
+ tablesSheet.getRange(2, 1, tableValues.length, 2).setValues(tableValues);
224
+ }
225
+
226
  return ContentService.createTextOutput(JSON.stringify({
227
  success: true,
228
  message: 'Settings updated successfully',