ichieh commited on
Commit
813fe6a
·
1 Parent(s): e9f7742

Add table leader role and display participants list

Browse files
Files changed (3) hide show
  1. app.py +54 -19
  2. templates/admin.html +36 -2
  3. templates/index.html +80 -12
app.py CHANGED
@@ -94,18 +94,31 @@ db = SQLAlchemy(app)
94
 
95
  class Table(db.Model):
96
  id = db.Column(db.Integer, primary_key=True)
 
97
  capacity = db.Column(db.Integer, nullable=False)
98
- name = db.Column(db.String(50))
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  class Participant(db.Model):
101
  id = db.Column(db.Integer, primary_key=True)
102
- nickname = db.Column(db.String(50), nullable=False)
103
  email = db.Column(db.String(120), nullable=False)
104
- fb_link = db.Column(db.String(200), nullable=True)
105
- introduction = db.Column(db.String(500), nullable=True) # 新增自我介紹欄位,最多500字元
106
  table_id = db.Column(db.Integer, db.ForeignKey('table.id'), nullable=False)
107
- seat_number = db.Column(db.Integer, nullable=False)
108
- created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
 
109
 
110
  def to_dict(self):
111
  return {
@@ -113,9 +126,10 @@ class Participant(db.Model):
113
  'nickname': self.nickname,
114
  'email': self.email,
115
  'fb_link': self.fb_link,
116
- 'introduction': self.introduction, # 加入自我介紹
117
  'table_id': self.table_id,
118
  'seat_number': self.seat_number,
 
119
  'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S')
120
  }
121
 
@@ -416,18 +430,7 @@ def get_tables():
416
  tables = Table.query.all()
417
  tables_data = []
418
  for table in tables:
419
- participants = Participant.query.filter_by(table_id=table.id).all()
420
- tables_data.append({
421
- 'id': table.id,
422
- 'name': table.name,
423
- 'capacity': table.capacity,
424
- 'current_seats': len(participants),
425
- 'participants': [{
426
- 'nickname': p.nickname,
427
- 'fb_link': p.fb_link,
428
- 'introduction': p.introduction
429
- } for p in participants]
430
- })
431
  return jsonify(tables_data)
432
 
433
  @app.route('/api/register', methods=['POST'])
@@ -526,5 +529,37 @@ def export_participants():
526
  except Exception as e:
527
  return jsonify({'error': str(e)}), 500
528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
  if __name__ == '__main__':
530
  app.run(host='0.0.0.0', port=7860)
 
94
 
95
  class Table(db.Model):
96
  id = db.Column(db.Integer, primary_key=True)
97
+ name = db.Column(db.String(50), nullable=False)
98
  capacity = db.Column(db.Integer, nullable=False)
99
+ participants = db.relationship('Participant', backref='table', lazy=True)
100
+
101
+ def to_dict(self):
102
+ return {
103
+ 'id': self.id,
104
+ 'name': self.name,
105
+ 'capacity': self.capacity,
106
+ 'participants': [p.to_dict() for p in sorted(
107
+ self.participants,
108
+ key=lambda x: (not x.is_table_leader, x.created_at) # 桌長優先,其次按報名時間排序
109
+ )]
110
+ }
111
 
112
  class Participant(db.Model):
113
  id = db.Column(db.Integer, primary_key=True)
114
+ nickname = db.Column(db.String(100), nullable=False)
115
  email = db.Column(db.String(120), nullable=False)
116
+ fb_link = db.Column(db.String(200))
117
+ introduction = db.Column(db.Text)
118
  table_id = db.Column(db.Integer, db.ForeignKey('table.id'), nullable=False)
119
+ seat_number = db.Column(db.Integer)
120
+ is_table_leader = db.Column(db.Boolean, default=False) # 新增: 是否為桌長
121
+ created_at = db.Column(db.DateTime, default=datetime.utcnow)
122
 
123
  def to_dict(self):
124
  return {
 
126
  'nickname': self.nickname,
127
  'email': self.email,
128
  'fb_link': self.fb_link,
129
+ 'introduction': self.introduction,
130
  'table_id': self.table_id,
131
  'seat_number': self.seat_number,
132
+ 'is_table_leader': self.is_table_leader, # 新增: 回傳桌長狀態
133
  'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S')
134
  }
135
 
 
430
  tables = Table.query.all()
431
  tables_data = []
432
  for table in tables:
433
+ tables_data.append(table.to_dict())
 
 
 
 
 
 
 
 
 
 
 
434
  return jsonify(tables_data)
435
 
436
  @app.route('/api/register', methods=['POST'])
 
529
  except Exception as e:
530
  return jsonify({'error': str(e)}), 500
531
 
532
+ @app.route('/api/admin/participant/<int:participant_id>/toggle-leader', methods=['POST'])
533
+ @admin_required
534
+ def toggle_leader(participant_id):
535
+ try:
536
+ participant = Participant.query.get(participant_id)
537
+ if not participant:
538
+ return jsonify({'error': '找不到該參與者'}), 404
539
+
540
+ # 如果要設為桌長,先確認該桌是否已有桌長
541
+ if not participant.is_table_leader:
542
+ existing_leader = Participant.query.filter_by(
543
+ table_id=participant.table_id,
544
+ is_table_leader=True
545
+ ).first()
546
+
547
+ if existing_leader:
548
+ return jsonify({
549
+ 'error': f'該桌已有桌長:{existing_leader.nickname}'
550
+ }), 400
551
+
552
+ # 切換桌長狀態
553
+ participant.is_table_leader = not participant.is_table_leader
554
+ db.session.commit()
555
+
556
+ return jsonify({
557
+ 'message': f'已{participant.is_table_leader and "設為" or "取消"}桌長',
558
+ 'participant': participant.to_dict()
559
+ })
560
+ except Exception as e:
561
+ db.session.rollback()
562
+ return jsonify({'error': str(e)}), 500
563
+
564
  if __name__ == '__main__':
565
  app.run(host='0.0.0.0', port=7860)
templates/admin.html CHANGED
@@ -63,6 +63,7 @@
63
  <th>Email</th>
64
  <th>FB連結</th>
65
  <th>自我介紹</th>
 
66
  <th>報名時間</th>
67
  <th>操作</th>
68
  </tr>
@@ -206,10 +207,19 @@
206
  <td>${p.email}</td>
207
  <td>${p.fb_link ? `<a href="${p.fb_link}" target="_blank">FB連結</a>` : '-'}</td>
208
  <td>${shortIntro || '-'}</td>
 
 
 
 
 
209
  <td>${p.created_at}</td>
210
  <td>
211
- <button class="btn btn-sm btn-primary" onclick="showMoveModal(${p.id})">移動座位</button>
212
- <button class="btn btn-sm btn-danger" onclick="deleteParticipant(${p.id})">刪除</button>
 
 
 
 
213
  </td>
214
  `;
215
  tbody.appendChild(row);
@@ -263,6 +273,30 @@
263
  }
264
  }
265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  // 查看完整自我介紹
267
  function viewFullIntro(intro) {
268
  document.getElementById('fullIntroText').textContent = intro;
 
63
  <th>Email</th>
64
  <th>FB連結</th>
65
  <th>自我介紹</th>
66
+ <th>身份</th>
67
  <th>報名時間</th>
68
  <th>操作</th>
69
  </tr>
 
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-primary">桌長</span>' :
213
+ '<span class="badge bg-secondary">參與者</span>'}
214
+ </td>
215
  <td>${p.created_at}</td>
216
  <td>
217
+ <button class="btn btn-sm btn-primary mb-1" onclick="showMoveModal(${p.id})">移動座位</button>
218
+ <button class="btn btn-sm ${p.is_table_leader ? 'btn-warning' : 'btn-success'} mb-1"
219
+ onclick="toggleLeader(${p.id})">
220
+ ${p.is_table_leader ? '取消桌長' : '設為桌長'}
221
+ </button>
222
+ <button class="btn btn-sm btn-danger mb-1" onclick="deleteParticipant(${p.id})">刪除</button>
223
  </td>
224
  `;
225
  tbody.appendChild(row);
 
273
  }
274
  }
275
 
276
+ // 切換桌長狀態
277
+ async function toggleLeader(participantId) {
278
+ try {
279
+ const response = await fetch(`/api/admin/participant/${participantId}/toggle-leader`, {
280
+ method: 'POST',
281
+ headers: {
282
+ 'Content-Type': 'application/json'
283
+ }
284
+ });
285
+
286
+ const data = await response.json();
287
+
288
+ if (response.ok) {
289
+ Swal.fire('成功', data.message, 'success');
290
+ loadParticipants();
291
+ } else {
292
+ throw new Error(data.error);
293
+ }
294
+ } catch (error) {
295
+ console.error('Error:', error);
296
+ Swal.fire('錯誤', error.message || '操作失敗', 'error');
297
+ }
298
+ }
299
+
300
  // 查看完整自我介紹
301
  function viewFullIntro(intro) {
302
  document.getElementById('fullIntroText').textContent = intro;
templates/index.html CHANGED
@@ -48,6 +48,49 @@
48
  font-size: 1rem;
49
  color: #666;
50
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  </style>
52
  </head>
53
  <body>
@@ -56,7 +99,7 @@
56
 
57
  <div class="row">
58
  <div class="col-md-12">
59
- <div class="table-container" id="tablesContainer">
60
  <!-- 桌子將由 JavaScript 動態生成 -->
61
  </div>
62
  </div>
@@ -161,21 +204,46 @@
161
  async function loadTables() {
162
  try {
163
  const response = await fetch('/api/tables');
164
- const data = await response.json();
165
 
166
- const container = document.getElementById('tablesContainer');
167
- container.innerHTML = '';
168
 
169
- data.forEach(table => {
170
- container.appendChild(createTableElement(table));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  });
172
  } catch (error) {
173
- console.error('Error loading tables:', error);
174
- Swal.fire({
175
- icon: 'error',
176
- title: '載入失敗',
177
- text: '無法載入桌位資訊,請重新整理頁面'
178
- });
179
  }
180
  }
181
 
 
48
  font-size: 1rem;
49
  color: #666;
50
  }
51
+ .table-info {
52
+ position: absolute;
53
+ top: -30px;
54
+ left: 50%;
55
+ transform: translateX(-50%);
56
+ background-color: rgba(255, 255, 255, 0.9);
57
+ padding: 5px 10px;
58
+ border-radius: 5px;
59
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
60
+ z-index: 2;
61
+ white-space: nowrap;
62
+ }
63
+ .participants-list {
64
+ position: absolute;
65
+ top: 120%;
66
+ left: 50%;
67
+ transform: translateX(-50%);
68
+ background-color: white;
69
+ padding: 10px;
70
+ border-radius: 5px;
71
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
72
+ width: 200px;
73
+ display: none;
74
+ z-index: 3;
75
+ }
76
+ .table-container:hover .participants-list {
77
+ display: block;
78
+ }
79
+ .participant-item {
80
+ padding: 5px 0;
81
+ border-bottom: 1px solid #eee;
82
+ }
83
+ .participant-item:last-child {
84
+ border-bottom: none;
85
+ }
86
+ .table-leader-badge {
87
+ background-color: #007bff;
88
+ color: white;
89
+ padding: 2px 6px;
90
+ border-radius: 3px;
91
+ font-size: 0.8em;
92
+ margin-left: 5px;
93
+ }
94
  </style>
95
  </head>
96
  <body>
 
99
 
100
  <div class="row">
101
  <div class="col-md-12">
102
+ <div id="tablesContainer">
103
  <!-- 桌子將由 JavaScript 動態生成 -->
104
  </div>
105
  </div>
 
204
  async function loadTables() {
205
  try {
206
  const response = await fetch('/api/tables');
207
+ const tables = await response.json();
208
 
209
+ const tablesContainer = document.getElementById('tablesContainer');
210
+ tablesContainer.innerHTML = '';
211
 
212
+ tables.forEach(table => {
213
+ const tableElement = document.createElement('div');
214
+ tableElement.className = 'table-container';
215
+
216
+ // 計算已坐人數
217
+ const participantsCount = table.participants.length;
218
+ const isFull = participantsCount >= table.capacity;
219
+
220
+ // 創建參與者列表
221
+ const participantsList = table.participants.map(p => `
222
+ <div class="participant-item">
223
+ ${p.nickname}
224
+ ${p.is_table_leader ? '<span class="table-leader-badge">桌長</span>' : ''}
225
+ </div>
226
+ `).join('');
227
+
228
+ tableElement.innerHTML = `
229
+ <div class="table-info">
230
+ ${table.name} (${participantsCount}/${table.capacity})
231
+ </div>
232
+ <div class="round-table ${isFull ? 'full' : ''}"
233
+ onclick="${isFull ? '' : `selectTable(${table.id})`}">
234
+ ${table.name}
235
+ </div>
236
+ <div class="participants-list">
237
+ <div style="font-weight: bold; margin-bottom: 5px;">已報名者:</div>
238
+ ${participantsList || '<div class="participant-item">尚無報名者</div>'}
239
+ </div>
240
+ `;
241
+
242
+ tablesContainer.appendChild(tableElement);
243
  });
244
  } catch (error) {
245
+ console.error('Error:', error);
246
+ Swal.fire('錯誤', '載入桌次資料失敗', 'error');
 
 
 
 
247
  }
248
  }
249