ichieh commited on
Commit
4953451
·
1 Parent(s): a1d202c

修改移動桌次同步

Browse files
Files changed (2) hide show
  1. app.py +53 -53
  2. templates/admin.html +24 -27
app.py CHANGED
@@ -272,77 +272,77 @@ def update_settings():
272
  try:
273
  data = request.get_json()
274
  action = data.get('action')
275
-
276
  if action == 'update_tables':
277
  total_tables = int(data.get('total_tables', 10))
278
  seats_per_table = int(data.get('seats_per_table', 10))
279
 
280
- print(f"更新設定: 總桌數={total_tables}, 每桌人數={seats_per_table}")
281
-
282
- # 同步到 Google Sheets
283
- sync_result = sync_settings_to_sheets('update_tables', {
284
  'total_tables': total_tables,
285
  'seats_per_table': seats_per_table
286
  })
287
 
288
- if not sync_result:
289
- return jsonify({'error': '同步失敗:無法連接到 Google Sheets'}), 500
290
-
291
- if not sync_result.get('success', False):
292
- return jsonify({
293
- 'error': f"同步到 Google Sheets 失敗: {sync_result.get('error', '未知錯誤')}"
294
- }), 500
295
-
296
- # 更新本地資料庫
297
- current_tables = Table.query.count()
298
-
299
- if total_tables > current_tables:
300
- # 添加新桌子
301
- for i in range(current_tables + 1, total_tables + 1):
302
- table = Table(name=f'第{i}桌', capacity=seats_per_table)
303
- db.session.add(table)
304
- elif total_tables < current_tables:
305
- # 刪除多餘的桌子(確保沒有參與者)
306
- for i in range(total_tables + 1, current_tables + 1):
307
- table = Table.query.filter_by(name=f'第{i}桌').first()
308
- if table and len(table.participants) == 0:
309
- db.session.delete(table)
310
-
311
- # 更新所有桌子的容量
312
  for table in Table.query.all():
313
  table.capacity = seats_per_table
314
 
315
  db.session.commit()
316
- print("本地資料庫更新成功")
317
- return jsonify({'message': '設定已更新', 'sync_result': sync_result})
 
 
 
 
 
 
318
 
319
  elif action == 'update_table_name':
320
  table_id = data.get('table_id')
321
  name = data.get('name', '')
322
 
323
- if table_id is None:
324
- return jsonify({'error': '缺少桌次 ID'}), 400
325
-
326
- try:
327
- table = Table.query.get(table_id)
328
- if not table:
329
- return jsonify({'error': '找不到指定的桌次'}), 404
330
-
331
- table.name = name
332
- db.session.commit()
333
- sync_result = sync_settings_to_sheets('update_table_name', {
334
- 'table_id': table_id,
335
- 'name': name
336
- })
337
- return jsonify(sync_result), 200 if sync_result.get('success') else 500
338
-
339
- except Exception as e:
340
- db.session.rollback()
341
- return jsonify({'error': str(e)}), 500
342
-
343
- else:
344
- return jsonify({'error': '無效的操作'}), 400
345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
  except Exception as e:
347
  print(f"更新設定時出錯: {str(e)}")
348
  db.session.rollback()
 
272
  try:
273
  data = request.get_json()
274
  action = data.get('action')
275
+
276
  if action == 'update_tables':
277
  total_tables = int(data.get('total_tables', 10))
278
  seats_per_table = int(data.get('seats_per_table', 10))
279
 
280
+ # 更新 Google Sheets
281
+ sync_settings_to_sheets('updateSettings', {
 
 
282
  'total_tables': total_tables,
283
  'seats_per_table': seats_per_table
284
  })
285
 
286
+ # 更新資料庫中的桌次
287
+ current_tables = Table.query.order_by(Table.id).all()
288
+ current_count = len(current_tables)
289
+
290
+ if total_tables > current_count:
291
+ # 需要添加新桌次
292
+ for i in range(current_count + 1, total_tables + 1):
293
+ new_table = Table(id=i, capacity=seats_per_table)
294
+ db.session.add(new_table)
295
+ elif total_tables < current_count:
296
+ # 需要刪除多餘的桌次
297
+ # 檢查要刪除的桌次是否有參與者
298
+ for table in current_tables[total_tables:]:
299
+ if len(table.participants) > 0:
300
+ return jsonify({
301
+ 'error': f'無法刪除第 {table.id} 桌,因為還有參與者在該桌'
302
+ }), 400
303
+ db.session.delete(table)
304
+
305
+ # 更新所有桌次的容量
 
 
 
 
306
  for table in Table.query.all():
307
  table.capacity = seats_per_table
308
 
309
  db.session.commit()
310
+
311
+ return jsonify({
312
+ 'message': '設定已更新',
313
+ 'settings': {
314
+ 'total_tables': total_tables,
315
+ 'seats_per_table': seats_per_table
316
+ }
317
+ })
318
 
319
  elif action == 'update_table_name':
320
  table_id = data.get('table_id')
321
  name = data.get('name', '')
322
 
323
+ table = Table.query.get(table_id)
324
+ if not table:
325
+ return jsonify({'error': '找不到該桌次'}), 404
326
+
327
+ table.name = name
328
+ db.session.commit()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
 
330
+ # 同步到 Google Sheets
331
+ sync_settings_to_sheets('update_table_name', {
332
+ 'table_id': table_id,
333
+ 'name': name
334
+ })
335
+
336
+ return jsonify({
337
+ 'message': '桌次名稱已更新',
338
+ 'table': {
339
+ 'id': table.id,
340
+ 'name': table.name
341
+ }
342
+ })
343
+
344
+ return jsonify({'error': '未知的操作'}), 400
345
+
346
  except Exception as e:
347
  print(f"更新設定時出錯: {str(e)}")
348
  db.session.rollback()
templates/admin.html CHANGED
@@ -253,27 +253,21 @@
253
  name: name
254
  })
255
  });
256
-
 
 
257
  if (!response.ok) {
258
- const data = await response.json();
259
- throw new Error(data.error || '更新桌次名稱失敗');
260
  }
261
-
262
- // 重新載入桌次名稱
263
- loadTableNames();
264
-
265
- Swal.fire({
266
- icon: 'success',
267
- title: '成功',
268
- text: '桌次名稱已更新',
269
- toast: true,
270
- position: 'top-end',
271
- showConfirmButton: false,
272
- timer: 3000
273
- });
274
  } catch (error) {
275
  console.error('Error:', error);
276
- Swal.fire('錯誤', error.message, 'error');
277
  }
278
  }
279
 
@@ -291,22 +285,25 @@
291
  },
292
  body: JSON.stringify({
293
  action: 'update_tables',
294
- total_tables: parseInt(totalTables),
295
- seats_per_table: parseInt(seatsPerTable)
296
  })
297
  });
298
 
299
- if (!response.ok) {
300
- const data = await response.json();
301
- throw new Error(data.error || '更新設定失敗');
302
- }
303
 
304
- const data = await response.json();
305
- Swal.fire('成功', '桌次設定已更新', 'success');
306
- loadParticipants(); // 重新載入參與者列表以反映新的桌次設定
 
 
 
 
 
 
307
  } catch (error) {
308
  console.error('Error:', error);
309
- Swal.fire('錯誤', error.message || '更新設定失敗', 'error');
310
  }
311
  });
312
 
 
253
  name: name
254
  })
255
  });
256
+
257
+ const result = await response.json();
258
+
259
  if (!response.ok) {
260
+ throw new Error(result.error || '更新桌次名稱失敗');
 
261
  }
262
+
263
+ // 重新載入桌次名稱設定
264
+ await loadTableNames();
265
+ // 重新載入參與者列表以更新顯示
266
+ await loadParticipants();
267
+
 
 
 
 
 
 
 
268
  } catch (error) {
269
  console.error('Error:', error);
270
+ Swal.fire('錯誤', error.message || '更新桌次名稱失敗', 'error');
271
  }
272
  }
273
 
 
285
  },
286
  body: JSON.stringify({
287
  action: 'update_tables',
288
+ total_tables: totalTables,
289
+ seats_per_table: seatsPerTable
290
  })
291
  });
292
 
293
+ const result = await response.json();
 
 
 
294
 
295
+ if (response.ok) {
296
+ await Swal.fire('成功', '桌次設定已更新', 'success');
297
+ // 重新載入桌次名稱設定
298
+ await loadTableNames();
299
+ // 重新載入參與者列表
300
+ await loadParticipants();
301
+ } else {
302
+ Swal.fire('錯誤', result.error || '更新失敗', 'error');
303
+ }
304
  } catch (error) {
305
  console.error('Error:', error);
306
+ Swal.fire('錯誤', '更新設定失敗', 'error');
307
  }
308
  });
309