ichieh commited on
Commit
fe8f9dd
·
1 Parent(s): 445d61d

管理後台桌次同步修正

Browse files
Files changed (1) hide show
  1. app.py +29 -6
app.py CHANGED
@@ -219,17 +219,31 @@ SETTINGS_SCRIPT_URL = os.environ.get('SETTINGS_SCRIPT_URL', '')
219
  def sync_settings_to_sheets(action, data):
220
  """同步設定到 Google Sheets"""
221
  if not app.config['SETTINGS_SCRIPT_URL']:
222
- return None
 
223
 
224
  try:
 
225
  response = requests.post(app.config['SETTINGS_SCRIPT_URL'], json={
226
  'action': action,
227
  **data
228
  })
229
- return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
230
  except Exception as e:
231
- print(f"Error syncing to sheets: {str(e)}")
232
- return None
233
 
234
  @app.route('/')
235
  def index():
@@ -263,14 +277,21 @@ def update_settings():
263
  total_tables = int(data.get('total_tables', 10))
264
  seats_per_table = int(data.get('seats_per_table', 10))
265
 
 
 
266
  # 同步到 Google Sheets
267
  sync_result = sync_settings_to_sheets('update_tables', {
268
  'total_tables': total_tables,
269
  'seats_per_table': seats_per_table
270
  })
271
 
 
 
 
272
  if not sync_result.get('success', False):
273
- return jsonify({'error': '同步到 Google Sheets 失敗'}), 500
 
 
274
 
275
  # 更新本地資料庫
276
  current_tables = Table.query.count()
@@ -292,12 +313,14 @@ def update_settings():
292
  table.capacity = seats_per_table
293
 
294
  db.session.commit()
295
- return jsonify({'message': '設定已更新'})
 
296
 
297
  else:
298
  return jsonify({'error': '無效的操作'}), 400
299
 
300
  except Exception as e:
 
301
  db.session.rollback()
302
  return jsonify({'error': str(e)}), 500
303
 
 
219
  def sync_settings_to_sheets(action, data):
220
  """同步設定到 Google Sheets"""
221
  if not app.config['SETTINGS_SCRIPT_URL']:
222
+ print("未設定 SETTINGS_SCRIPT_URL")
223
+ return {'success': False, 'error': '未設定 Google Sheets URL'}
224
 
225
  try:
226
+ print(f"正在同步到 Google Sheets: action={action}, data={data}")
227
  response = requests.post(app.config['SETTINGS_SCRIPT_URL'], json={
228
  'action': action,
229
  **data
230
  })
231
+ print(f"Google Sheets 回應狀態碼: {response.status_code}")
232
+ print(f"Google Sheets 回應內容: {response.text}")
233
+
234
+ if response.status_code != 200:
235
+ return {'success': False, 'error': f'Google Sheets 回應錯誤: {response.status_code}'}
236
+
237
+ try:
238
+ result = response.json()
239
+ return result
240
+ except ValueError as e:
241
+ print(f"JSON 解析錯誤: {str(e)}")
242
+ return {'success': False, 'error': '無效的 JSON 回應'}
243
+
244
  except Exception as e:
245
+ print(f"同步到 Google Sheets 時出錯: {str(e)}")
246
+ return {'success': False, 'error': str(e)}
247
 
248
  @app.route('/')
249
  def index():
 
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()
 
313
  table.capacity = seats_per_table
314
 
315
  db.session.commit()
316
+ print("本地資料庫更新成功")
317
+ return jsonify({'message': '設定已更新', 'sync_result': sync_result})
318
 
319
  else:
320
  return jsonify({'error': '無效的操作'}), 400
321
 
322
  except Exception as e:
323
+ print(f"更新設定時出錯: {str(e)}")
324
  db.session.rollback()
325
  return jsonify({'error': str(e)}), 500
326