ichieh commited on
Commit
4307a66
·
1 Parent(s): f22f614

Add introduction field to registration form with 500 character limit

Browse files
Files changed (3) hide show
  1. app.py +29 -7
  2. sync_to_sheets.gs +3 -1
  3. templates/index.html +96 -46
app.py CHANGED
@@ -85,11 +85,25 @@ class Table(db.Model):
85
 
86
  class Participant(db.Model):
87
  id = db.Column(db.Integer, primary_key=True)
88
- nickname = db.Column(db.String(100), nullable=False)
89
  email = db.Column(db.String(120), nullable=False)
90
- fb_link = db.Column(db.String(200), nullable=False)
91
- table_id = db.Column(db.Integer, db.ForeignKey('table.id'))
92
- created_at = db.Column(db.DateTime, default=datetime.utcnow)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  # 初始化資料庫和創建預設桌子
95
  def init_db():
@@ -217,7 +231,11 @@ def admin_participants():
217
  'id': p.id,
218
  'nickname': p.nickname,
219
  'email': p.email,
220
- 'fb_link': p.fb_link
 
 
 
 
221
  } for p in participants]
222
  })
223
  return jsonify(tables_data)
@@ -249,7 +267,8 @@ def get_tables():
249
  'current_seats': len(participants),
250
  'participants': [{
251
  'nickname': p.nickname,
252
- 'fb_link': p.fb_link
 
253
  } for p in participants]
254
  })
255
  return jsonify(tables_data)
@@ -274,7 +293,9 @@ def register():
274
  nickname=data['nickname'],
275
  email=data['email'],
276
  fb_link=data['fb_link'],
277
- table_id=table_id
 
 
278
  )
279
 
280
  db.session.add(participant)
@@ -303,6 +324,7 @@ def export_participants():
303
  'nickname': participant.nickname,
304
  'email': participant.email,
305
  'fb_link': participant.fb_link,
 
306
  'registration_time': local_time.strftime('%Y-%m-%d %H:%M:%S')
307
  })
308
 
 
85
 
86
  class Participant(db.Model):
87
  id = db.Column(db.Integer, primary_key=True)
88
+ nickname = db.Column(db.String(50), nullable=False)
89
  email = db.Column(db.String(120), nullable=False)
90
+ fb_link = db.Column(db.String(200), nullable=True)
91
+ introduction = db.Column(db.String(500), nullable=True) # 新增自我介紹欄位,最多500字元
92
+ table_id = db.Column(db.Integer, db.ForeignKey('table.id'), nullable=False)
93
+ seat_number = db.Column(db.Integer, nullable=False)
94
+ created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
95
+
96
+ def to_dict(self):
97
+ return {
98
+ 'id': self.id,
99
+ 'nickname': self.nickname,
100
+ 'email': self.email,
101
+ 'fb_link': self.fb_link,
102
+ 'introduction': self.introduction, # 加入自我介紹
103
+ 'table_id': self.table_id,
104
+ 'seat_number': self.seat_number,
105
+ 'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S')
106
+ }
107
 
108
  # 初始化資料庫和創建預設桌子
109
  def init_db():
 
231
  'id': p.id,
232
  'nickname': p.nickname,
233
  'email': p.email,
234
+ 'fb_link': p.fb_link,
235
+ 'introduction': p.introduction,
236
+ 'table_id': p.table_id,
237
+ 'seat_number': p.seat_number,
238
+ 'created_at': p.created_at.strftime('%Y-%m-%d %H:%M:%S')
239
  } for p in participants]
240
  })
241
  return jsonify(tables_data)
 
267
  'current_seats': len(participants),
268
  'participants': [{
269
  'nickname': p.nickname,
270
+ 'fb_link': p.fb_link,
271
+ 'introduction': p.introduction
272
  } for p in participants]
273
  })
274
  return jsonify(tables_data)
 
293
  nickname=data['nickname'],
294
  email=data['email'],
295
  fb_link=data['fb_link'],
296
+ introduction=data.get('introduction'),
297
+ table_id=table_id,
298
+ seat_number=current_seats + 1
299
  )
300
 
301
  db.session.add(participant)
 
324
  'nickname': participant.nickname,
325
  'email': participant.email,
326
  'fb_link': participant.fb_link,
327
+ 'introduction': participant.introduction,
328
  'registration_time': local_time.strftime('%Y-%m-%d %H:%M:%S')
329
  })
330
 
sync_to_sheets.gs CHANGED
@@ -11,6 +11,7 @@ const HEADERS = [
11
  '暱稱',
12
  'Email',
13
  'FB連結',
 
14
  '報名時間'
15
  ];
16
 
@@ -51,7 +52,8 @@ function updateSpreadsheet() {
51
  p.table_capacity,
52
  p.nickname,
53
  p.email,
54
- p.fb_link,
 
55
  p.registration_time
56
  ]);
57
 
 
11
  '暱稱',
12
  'Email',
13
  'FB連結',
14
+ '自我介紹',
15
  '報名時間'
16
  ];
17
 
 
52
  p.table_capacity,
53
  p.nickname,
54
  p.email,
55
+ p.fb_link || '',
56
+ p.introduction || '',
57
  p.registration_time
58
  ]);
59
 
templates/index.html CHANGED
@@ -53,27 +53,50 @@
53
  <div class="card">
54
  <div class="card-body">
55
  <h5 class="card-title">登記資料</h5>
56
- <form id="registrationForm">
57
- <div class="mb-3">
58
- <label for="nickname" class="form-label">暱稱</label>
59
- <input type="text" class="form-control" id="nickname" required>
60
- </div>
61
- <div class="mb-3">
62
- <label for="email" class="form-label">Email</label>
63
- <input type="email" class="form-control" id="email" required>
64
- </div>
65
- <div class="mb-3">
66
- <label for="fbLink" class="form-label">FB個人連結</label>
67
- <input type="url" class="form-control" id="fbLink" required>
68
- </div>
69
- <button type="submit" class="btn btn-primary w-100">確認選位</button>
70
- </form>
71
  </div>
72
  </div>
73
  </div>
74
  </div>
75
  </div>
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  <script>
78
  let selectedTableId = null;
79
 
@@ -115,44 +138,71 @@
115
  event.currentTarget.classList.add('selected');
116
  }
117
 
118
- document.getElementById('registrationForm').onsubmit = async (e) => {
119
- e.preventDefault();
 
 
 
120
 
121
- if (!selectedTableId) {
122
- alert('請先選擇一個座位!');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  return;
124
  }
125
 
126
- const formData = {
127
- nickname: document.getElementById('nickname').value,
128
- email: document.getElementById('email').value,
129
- fb_link: document.getElementById('fbLink').value,
130
- table_id: selectedTableId
 
131
  };
132
 
133
- try {
134
- const response = await fetch('/api/register', {
135
- method: 'POST',
136
- headers: {
137
- 'Content-Type': 'application/json'
138
- },
139
- body: JSON.stringify(formData)
140
- });
141
-
142
- const result = await response.json();
143
-
144
- if (response.ok) {
145
- alert('登記成功!');
146
- document.getElementById('registrationForm').reset();
147
- selectedTableId = null;
148
- loadTables();
149
- } else {
150
- alert(result.error || '登記失敗,請稍後再試。');
151
  }
152
- } catch (error) {
153
- alert('發生錯誤,請稍後再試。');
154
- }
155
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
  // 初始載入桌位資訊
158
  loadTables();
 
53
  <div class="card">
54
  <div class="card-body">
55
  <h5 class="card-title">登記資料</h5>
56
+ <button type="button" class="btn btn-primary w-100" data-bs-toggle="modal" data-bs-target="#registrationForm">確認選位</button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  </div>
58
  </div>
59
  </div>
60
  </div>
61
  </div>
62
 
63
+ <!-- 報名表單 -->
64
+ <div id="registrationForm" class="modal fade" tabindex="-1">
65
+ <div class="modal-dialog">
66
+ <div class="modal-content">
67
+ <div class="modal-header">
68
+ <h5 class="modal-title">報名表單</h5>
69
+ <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
70
+ </div>
71
+ <div class="modal-body">
72
+ <form id="seatRegistration">
73
+ <div class="mb-3">
74
+ <label for="nickname" class="form-label">暱稱 <span class="text-danger">*</span></label>
75
+ <input type="text" class="form-control" id="nickname" required>
76
+ </div>
77
+ <div class="mb-3">
78
+ <label for="email" class="form-label">Email <span class="text-danger">*</span></label>
79
+ <input type="email" class="form-control" id="email" required>
80
+ </div>
81
+ <div class="mb-3">
82
+ <label for="fb_link" class="form-label">FB連結</label>
83
+ <input type="url" class="form-control" id="fb_link">
84
+ </div>
85
+ <div class="mb-3">
86
+ <label for="introduction" class="form-label">自我介紹 <small class="text-muted">(最多500字元)</small></label>
87
+ <textarea class="form-control" id="introduction" maxlength="500" rows="4"></textarea>
88
+ <div class="form-text text-end"><span id="charCount">0</span>/500</div>
89
+ </div>
90
+ </form>
91
+ </div>
92
+ <div class="modal-footer">
93
+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
94
+ <button type="button" class="btn btn-primary" onclick="submitRegistration()">確認報名</button>
95
+ </div>
96
+ </div>
97
+ </div>
98
+ </div>
99
+
100
  <script>
101
  let selectedTableId = null;
102
 
 
138
  event.currentTarget.classList.add('selected');
139
  }
140
 
141
+ // 添加字元計數功能
142
+ document.getElementById('introduction').addEventListener('input', function() {
143
+ const maxLength = 500;
144
+ const currentLength = this.value.length;
145
+ document.getElementById('charCount').textContent = currentLength;
146
 
147
+ if (currentLength > maxLength) {
148
+ this.value = this.value.substring(0, maxLength);
149
+ }
150
+ });
151
+
152
+ // 更新提交函數
153
+ function submitRegistration() {
154
+ const nickname = document.getElementById('nickname').value;
155
+ const email = document.getElementById('email').value;
156
+ const fb_link = document.getElementById('fb_link').value;
157
+ const introduction = document.getElementById('introduction').value;
158
+
159
+ if (!nickname || !email) {
160
+ Swal.fire({
161
+ icon: 'error',
162
+ title: '請填寫必填欄位',
163
+ text: '暱稱和Email為必填欄位'
164
+ });
165
  return;
166
  }
167
 
168
+ const data = {
169
+ table_id: selectedTableId,
170
+ nickname: nickname,
171
+ email: email,
172
+ fb_link: fb_link || '',
173
+ introduction: introduction || ''
174
  };
175
 
176
+ fetch('/api/register', {
177
+ method: 'POST',
178
+ headers: {
179
+ 'Content-Type': 'application/json',
180
+ },
181
+ body: JSON.stringify(data)
182
+ })
183
+ .then(response => response.json())
184
+ .then(data => {
185
+ if (data.error) {
186
+ throw new Error(data.error);
 
 
 
 
 
 
 
187
  }
188
+ Swal.fire({
189
+ icon: 'success',
190
+ title: '報名成功!',
191
+ text: '您已成功報名座位'
192
+ }).then(() => {
193
+ $('#registrationForm').modal('hide');
194
+ document.getElementById('seatRegistration').reset();
195
+ loadTables();
196
+ });
197
+ })
198
+ .catch(error => {
199
+ Swal.fire({
200
+ icon: 'error',
201
+ title: '報名失敗',
202
+ text: error.message
203
+ });
204
+ });
205
+ }
206
 
207
  // 初始載入桌位資訊
208
  loadTables();