ichieh commited on
Commit
4661923
·
1 Parent(s): 0758969

Add payment status and note fields to admin panel

Browse files
Files changed (2) hide show
  1. app.py +79 -12
  2. templates/admin.html +86 -0
app.py CHANGED
@@ -63,10 +63,12 @@ class Participant(db.Model):
63
  email = db.Column(db.String(120), nullable=False)
64
  fb_link = db.Column(db.String(200))
65
  introduction = db.Column(db.Text)
66
- table_id = db.Column(db.Integer, db.ForeignKey('table.id')) # 移除 nullable=False
67
  seat_number = db.Column(db.Integer)
68
- is_table_leader = db.Column(db.Boolean, default=False) # 新增: 是否為桌長
69
  created_at = db.Column(db.DateTime, default=datetime.utcnow)
 
 
70
 
71
  def to_dict(self):
72
  return {
@@ -77,8 +79,10 @@ class Participant(db.Model):
77
  'introduction': self.introduction,
78
  'table_id': self.table_id,
79
  'seat_number': self.seat_number,
80
- 'is_table_leader': self.is_table_leader, # 新增: 回傳桌長狀態
81
- 'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S')
 
 
82
  }
83
 
84
  # 初始化資料庫和創建預設桌子
@@ -255,7 +259,9 @@ def backup_settings():
255
  'table_id': p.table_id,
256
  'seat_number': p.seat_number,
257
  'is_table_leader': p.is_table_leader,
258
- 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S')
 
 
259
  } for p in participants]
260
  })
261
 
@@ -316,12 +322,14 @@ def restore_settings():
316
  participant = Participant(
317
  nickname=participant_data['nickname'],
318
  email=participant_data['email'],
319
- fb_link=participant_data.get('fb_link', ''), # 使用 get 避免缺少欄位
320
  introduction=participant_data.get('introduction', ''),
321
  table_id=new_table_id,
322
  seat_number=participant_data['seat_number'],
323
- is_table_leader=participant_data.get('is_table_leader', False), # 使用 get 避免缺少欄位
324
- created_at=created_at
 
 
325
  )
326
  db.session.add(participant)
327
 
@@ -330,7 +338,7 @@ def restore_settings():
330
 
331
  except Exception as e:
332
  db.session.rollback()
333
- print('Error restoring data:', str(e)) # 添加錯誤日誌
334
  return jsonify({'error': str(e)}), 500
335
 
336
  @app.route('/api/admin/participants', methods=['GET'])
@@ -349,7 +357,9 @@ def get_all_participants():
349
  'table_id': p.table_id,
350
  'seat_number': p.seat_number,
351
  'is_table_leader': p.is_table_leader,
352
- 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S')
 
 
353
  } for p in participants],
354
  'tables': [{
355
  'id': t.id,
@@ -470,7 +480,9 @@ def register():
470
  'table_id': p.table_id,
471
  'seat_number': p.seat_number,
472
  'is_table_leader': p.is_table_leader,
473
- 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S')
 
 
474
  } for p in all_participants]
475
  })
476
  print("成功備份參與者資料到 Google Sheets")
@@ -515,7 +527,9 @@ def export_participants():
515
  'email': participant.email,
516
  'fb_link': participant.fb_link,
517
  'introduction': participant.introduction,
518
- 'registration_time': local_time.strftime('%Y-%m-%d %H:%M:%S')
 
 
519
  })
520
 
521
  return jsonify(export_data)
@@ -554,5 +568,58 @@ def toggle_leader(participant_id):
554
  db.session.rollback()
555
  return jsonify({'error': str(e)}), 500
556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557
  if __name__ == '__main__':
558
  app.run(host='0.0.0.0', port=7860)
 
63
  email = db.Column(db.String(120), nullable=False)
64
  fb_link = db.Column(db.String(200))
65
  introduction = db.Column(db.Text)
66
+ table_id = db.Column(db.Integer, db.ForeignKey('table.id'))
67
  seat_number = db.Column(db.Integer)
68
+ is_table_leader = db.Column(db.Boolean, default=False)
69
  created_at = db.Column(db.DateTime, default=datetime.utcnow)
70
+ has_paid = db.Column(db.Boolean, default=False)
71
+ note = db.Column(db.Text)
72
 
73
  def to_dict(self):
74
  return {
 
79
  'introduction': self.introduction,
80
  'table_id': self.table_id,
81
  'seat_number': self.seat_number,
82
+ 'is_table_leader': self.is_table_leader,
83
+ 'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S'),
84
+ 'has_paid': self.has_paid,
85
+ 'note': self.note
86
  }
87
 
88
  # 初始化資料庫和創建預設桌子
 
259
  'table_id': p.table_id,
260
  'seat_number': p.seat_number,
261
  'is_table_leader': p.is_table_leader,
262
+ 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S'),
263
+ 'has_paid': p.has_paid,
264
+ 'note': p.note
265
  } for p in participants]
266
  })
267
 
 
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=new_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', '')
333
  )
334
  db.session.add(participant)
335
 
 
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'])
 
357
  'table_id': p.table_id,
358
  'seat_number': p.seat_number,
359
  'is_table_leader': p.is_table_leader,
360
+ 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S'),
361
+ 'has_paid': p.has_paid,
362
+ 'note': p.note
363
  } for p in participants],
364
  'tables': [{
365
  'id': t.id,
 
480
  'table_id': p.table_id,
481
  'seat_number': p.seat_number,
482
  'is_table_leader': p.is_table_leader,
483
+ 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S'),
484
+ 'has_paid': p.has_paid,
485
+ 'note': p.note
486
  } for p in all_participants]
487
  })
488
  print("成功備份參與者資料到 Google Sheets")
 
527
  'email': participant.email,
528
  'fb_link': participant.fb_link,
529
  'introduction': participant.introduction,
530
+ 'registration_time': local_time.strftime('%Y-%m-%d %H:%M:%S'),
531
+ 'has_paid': participant.has_paid,
532
+ 'note': participant.note
533
  })
534
 
535
  return jsonify(export_data)
 
568
  db.session.rollback()
569
  return jsonify({'error': str(e)}), 500
570
 
571
+ @app.route('/api/admin/participant/<int:participant_id>/payment', methods=['POST'])
572
+ @admin_required
573
+ def toggle_payment(participant_id):
574
+ try:
575
+ participant = Participant.query.get_or_404(participant_id)
576
+ participant.has_paid = not participant.has_paid
577
+ db.session.commit()
578
+
579
+ # 自動備份到 Google Sheets
580
+ try:
581
+ all_participants = Participant.query.all()
582
+ sync_settings_to_sheets('backupParticipants', {
583
+ 'participants': [p.to_dict() for p in all_participants]
584
+ })
585
+ except Exception as e:
586
+ print(f"備份到 Google Sheets 時發生錯誤: {str(e)}")
587
+ pass
588
+
589
+ return jsonify({
590
+ 'message': '付款狀態已更新',
591
+ 'has_paid': participant.has_paid
592
+ })
593
+ except Exception as e:
594
+ db.session.rollback()
595
+ return jsonify({'error': str(e)}), 500
596
+
597
+ @app.route('/api/admin/participant/<int:participant_id>/note', methods=['POST'])
598
+ @admin_required
599
+ def update_note(participant_id):
600
+ try:
601
+ participant = Participant.query.get_or_404(participant_id)
602
+ data = request.get_json()
603
+ participant.note = data.get('note', '')
604
+ db.session.commit()
605
+
606
+ # 自動備份到 Google Sheets
607
+ try:
608
+ all_participants = Participant.query.all()
609
+ sync_settings_to_sheets('backupParticipants', {
610
+ 'participants': [p.to_dict() for p in all_participants]
611
+ })
612
+ except Exception as e:
613
+ print(f"備份到 Google Sheets 時發生錯誤: {str(e)}")
614
+ pass
615
+
616
+ return jsonify({
617
+ 'message': '備註已更新',
618
+ 'note': participant.note
619
+ })
620
+ except Exception as e:
621
+ db.session.rollback()
622
+ return jsonify({'error': str(e)}), 500
623
+
624
  if __name__ == '__main__':
625
  app.run(host='0.0.0.0', port=7860)
templates/admin.html CHANGED
@@ -63,6 +63,8 @@
63
  <th>Email</th>
64
  <th>FB連結</th>
65
  <th>自我介紹</th>
 
 
66
  <th>身份</th>
67
  <th>報名時間</th>
68
  <th>操作</th>
@@ -207,6 +209,23 @@
207
  <td>${p.email}</td>
208
  <td>${p.fb_link ? `<a href="${p.fb_link}" target="_blank">FB連結</a>` : '-'}</td>
209
  <td>${shortIntro || '-'}</td>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  <td>
211
  ${p.is_table_leader ?
212
  '<span class="badge bg-success">桌長</span>' :
@@ -335,6 +354,73 @@
335
  }
336
  }
337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
  // 備份資料到 Google Sheets
339
  async function backupData() {
340
  try {
 
63
  <th>Email</th>
64
  <th>FB連結</th>
65
  <th>自我介紹</th>
66
+ <th>付款狀態</th>
67
+ <th>備註</th>
68
  <th>身份</th>
69
  <th>報名時間</th>
70
  <th>操作</th>
 
209
  <td>${p.email}</td>
210
  <td>${p.fb_link ? `<a href="${p.fb_link}" target="_blank">FB連結</a>` : '-'}</td>
211
  <td>${shortIntro || '-'}</td>
212
+ <td>
213
+ <button
214
+ class="btn btn-sm"
215
+ ${p.has_paid ? 'btn-success' : 'btn-outline-secondary'}
216
+ onclick="togglePayment(${p.id})"
217
+ >
218
+ ${p.has_paid ? '已付款' : '未付款'}
219
+ </button>
220
+ </td>
221
+ <td>
222
+ <div class="d-flex align-items-center">
223
+ <span ${p.note ? `onclick="showNote('${p.note}')" style="cursor: pointer; color: #007bff;"` : ''}>
224
+ ${p.note ? (p.note.length > 10 ? p.note.substring(0, 10) + '...' : p.note) : '-'}
225
+ </span>
226
+ <i class="fas fa-edit ms-2" style="cursor: pointer; color: #6c757d;" onclick="editNote(${p.id})"></i>
227
+ </div>
228
+ </td>
229
  <td>
230
  ${p.is_table_leader ?
231
  '<span class="badge bg-success">桌長</span>' :
 
354
  }
355
  }
356
 
357
+ // 切換付款狀態
358
+ async function togglePayment(participantId) {
359
+ try {
360
+ const response = await fetch(`/api/admin/participant/${participantId}/toggle-payment`, {
361
+ method: 'POST',
362
+ headers: {
363
+ 'Content-Type': 'application/json'
364
+ }
365
+ });
366
+
367
+ const data = await response.json();
368
+
369
+ if (response.ok) {
370
+ Swal.fire('成功', data.message, 'success');
371
+ loadParticipants();
372
+ } else {
373
+ throw new Error(data.error);
374
+ }
375
+ } catch (error) {
376
+ console.error('Error:', error);
377
+ Swal.fire('錯誤', error.message || '操作失敗', 'error');
378
+ }
379
+ }
380
+
381
+ // 顯示備註
382
+ function showNote(note) {
383
+ Swal.fire({
384
+ title: '備註',
385
+ text: note,
386
+ icon: 'info'
387
+ });
388
+ }
389
+
390
+ // 編輯備註
391
+ async function editNote(participantId) {
392
+ const { value: note } = await Swal.fire({
393
+ title: '編輯備註',
394
+ input: 'textarea',
395
+ inputPlaceholder: '請輸入備註',
396
+ showCancelButton: true
397
+ });
398
+
399
+ if (note) {
400
+ try {
401
+ const response = await fetch(`/api/admin/participant/${participantId}/note`, {
402
+ method: 'POST',
403
+ headers: {
404
+ 'Content-Type': 'application/json'
405
+ },
406
+ body: JSON.stringify({ note })
407
+ });
408
+
409
+ const data = await response.json();
410
+
411
+ if (response.ok) {
412
+ Swal.fire('成功', data.message, 'success');
413
+ loadParticipants();
414
+ } else {
415
+ throw new Error(data.error);
416
+ }
417
+ } catch (error) {
418
+ console.error('Error:', error);
419
+ Swal.fire('錯誤', error.message || '操作失敗', 'error');
420
+ }
421
+ }
422
+ }
423
+
424
  // 備份資料到 Google Sheets
425
  async function backupData() {
426
  try {