ichieh commited on
Commit
cf0186c
·
1 Parent(s): dd7cb5f

Fix package versions for compatibility

Browse files
Files changed (2) hide show
  1. app.py +83 -45
  2. requirements.txt +4 -2
app.py CHANGED
@@ -199,7 +199,7 @@ def admin_login_api():
199
 
200
  @app.route('/api/admin/settings', methods=['GET', 'POST'])
201
  @admin_required
202
- def admin_settings():
203
  if request.method == 'GET':
204
  # 獲取目前設定
205
  total_tables = Table.query.count()
@@ -210,42 +210,43 @@ def admin_settings():
210
  'seats_per_table': seats_per_table
211
  })
212
  else:
213
- # 更新設定
214
- data = request.json
215
- total_tables = data.get('total_tables')
216
- seats_per_table = data.get('seats_per_table')
217
-
218
  try:
219
- current_tables = Table.query.all()
220
- current_table_count = len(current_tables)
221
-
222
- # 更新現有桌子的容量
223
- for table in current_tables:
224
- table.capacity = seats_per_table
225
-
226
- # 如果需要更多桌子,則新增
227
- if total_tables > current_table_count:
228
- for i in range(current_table_count + 1, total_tables + 1):
229
- table = Table(capacity=seats_per_table, name=f'Table {i}')
230
- db.session.add(table)
231
 
232
- # 如果需要減少桌子,則移除空桌子
233
- elif total_tables < current_table_count:
234
- tables_to_remove = []
235
- for table in current_tables[total_tables:]:
236
- # 檢查桌子是否有人
237
- if Participant.query.filter_by(table_id=table.id).count() == 0:
238
- tables_to_remove.append(table)
239
-
240
- # 只刪除沒有人的桌子
241
- for table in tables_to_remove:
242
- db.session.delete(table)
 
 
243
 
244
  db.session.commit()
245
- return jsonify({'message': 'Settings updated successfully'})
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  except Exception as e:
247
  db.session.rollback()
248
- return jsonify({'error': str(e)}), 400
249
 
250
  @app.route('/api/admin/settings/backup', methods=['POST'])
251
  @admin_required
@@ -432,34 +433,71 @@ def get_tables():
432
  @app.route('/api/register', methods=['POST'])
433
  def register():
434
  try:
435
- data = request.json
436
- table_id = data.get('table_id')
437
 
438
- # Check if table exists and has available seats
439
- table = Table.query.get(table_id)
 
 
 
 
440
  if not table:
441
- return jsonify({'error': 'Table not found'}), 404
442
 
443
- current_seats = Participant.query.filter_by(table_id=table_id).count()
444
  if current_seats >= table.capacity:
445
- return jsonify({'error': 'Table is full'}), 400
446
-
447
- # Create new participant
448
  participant = Participant(
449
  nickname=data['nickname'],
450
  email=data['email'],
451
- fb_link=data['fb_link'],
452
- introduction=data.get('introduction'),
453
- table_id=table_id,
454
  seat_number=current_seats + 1
455
  )
456
 
457
  db.session.add(participant)
458
  db.session.commit()
459
- return jsonify({'message': 'Registration successful'})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460
  except Exception as e:
461
  db.session.rollback()
462
- print(f"Error in registration: {e}")
463
  return jsonify({'error': str(e)}), 400
464
 
465
  @app.route('/api/participants/export', methods=['GET'])
 
199
 
200
  @app.route('/api/admin/settings', methods=['GET', 'POST'])
201
  @admin_required
202
+ def update_settings():
203
  if request.method == 'GET':
204
  # 獲取目前設定
205
  total_tables = Table.query.count()
 
210
  'seats_per_table': seats_per_table
211
  })
212
  else:
 
 
 
 
 
213
  try:
214
+ data = request.get_json()
215
+ total_tables = int(data.get('total_tables', 0))
216
+ seats_per_table = int(data.get('seats_per_table', 0))
 
 
 
 
 
 
 
 
 
217
 
218
+ if total_tables <= 0 or seats_per_table <= 0:
219
+ return jsonify({'error': '桌數和座位數必須大於 0'}), 400
220
+
221
+ # 清除現有的桌子
222
+ Table.query.delete()
223
+
224
+ # 創建新的桌子
225
+ for i in range(total_tables):
226
+ table = Table(
227
+ name=f'第{i+1}桌',
228
+ capacity=seats_per_table
229
+ )
230
+ db.session.add(table)
231
 
232
  db.session.commit()
233
+
234
+ # 自動備份設定到 Google Sheets
235
+ try:
236
+ sync_settings_to_sheets('saveSettings', {
237
+ 'totalTables': total_tables,
238
+ 'seatsPerTable': seats_per_table
239
+ })
240
+ print("成功備份桌次設定到 Google Sheets")
241
+ except Exception as e:
242
+ print(f"備份設定到 Google Sheets 時發生錯誤: {str(e)}")
243
+ # 不要因為備份失敗而影響設定更新
244
+ pass
245
+
246
+ return jsonify({'message': '設定已更新'})
247
  except Exception as e:
248
  db.session.rollback()
249
+ return jsonify({'error': str(e)}), 500
250
 
251
  @app.route('/api/admin/settings/backup', methods=['POST'])
252
  @admin_required
 
433
  @app.route('/api/register', methods=['POST'])
434
  def register():
435
  try:
436
+ data = request.get_json()
 
437
 
438
+ # 驗證必填欄位
439
+ if not all(key in data for key in ['nickname', 'email', 'table_id']):
440
+ return jsonify({'error': '缺少必要欄位'}), 400
441
+
442
+ # 檢查桌子是否存在且有空位
443
+ table = Table.query.get(data['table_id'])
444
  if not table:
445
+ return jsonify({'error': '找不到該桌子'}), 404
446
 
447
+ current_seats = len(Participant.query.filter_by(table_id=data['table_id']).all())
448
  if current_seats >= table.capacity:
449
+ return jsonify({'error': '該桌已滿'}), 400
450
+
451
+ # 創建新參與者
452
  participant = Participant(
453
  nickname=data['nickname'],
454
  email=data['email'],
455
+ fb_link=data.get('fb_link', ''),
456
+ introduction=data.get('introduction', ''),
457
+ table_id=data['table_id'],
458
  seat_number=current_seats + 1
459
  )
460
 
461
  db.session.add(participant)
462
  db.session.commit()
463
+
464
+ # 自動備份到 Google Sheets
465
+ try:
466
+ # 獲取所有參與者資料
467
+ all_participants = Participant.query.all()
468
+ sync_settings_to_sheets('backupParticipants', {
469
+ 'participants': [{
470
+ 'id': p.id,
471
+ 'nickname': p.nickname,
472
+ 'email': p.email,
473
+ 'fb_link': p.fb_link,
474
+ 'introduction': p.introduction,
475
+ 'table_id': p.table_id,
476
+ 'seat_number': p.seat_number,
477
+ 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S')
478
+ } for p in all_participants]
479
+ })
480
+ print("成功備份參與者資料到 Google Sheets")
481
+ except Exception as e:
482
+ print(f"備份到 Google Sheets 時發生錯誤: {str(e)}")
483
+ # 不要因為備份失敗而影響註冊流程
484
+ pass
485
+
486
+ return jsonify({
487
+ 'message': '報名成功',
488
+ 'participant': {
489
+ 'id': participant.id,
490
+ 'nickname': participant.nickname,
491
+ 'email': participant.email,
492
+ 'fb_link': participant.fb_link,
493
+ 'introduction': participant.introduction,
494
+ 'table_id': participant.table_id,
495
+ 'seat_number': participant.seat_number
496
+ }
497
+ })
498
+
499
  except Exception as e:
500
  db.session.rollback()
 
501
  return jsonify({'error': str(e)}), 400
502
 
503
  @app.route('/api/participants/export', methods=['GET'])
requirements.txt CHANGED
@@ -1,4 +1,6 @@
1
- flask==2.0.1
2
- flask-sqlalchemy==2.5.1
 
 
3
  requests==2.26.0
4
  python-dotenv==0.19.0
 
1
+ Flask==2.0.1
2
+ Werkzeug==2.0.1
3
+ Flask-SQLAlchemy==2.5.1
4
+ SQLAlchemy==1.4.23
5
  requests==2.26.0
6
  python-dotenv==0.19.0