ichieh commited on
Commit
f3205c7
·
1 Parent(s): 71e734b

修改桌次名稱同步

Browse files
Files changed (1) hide show
  1. app.py +69 -46
app.py CHANGED
@@ -100,55 +100,78 @@ def init_db():
100
  # 從 Google Sheets 獲取設定
101
  print("正在從 Google Sheets 獲取設定...")
102
  try:
 
 
 
 
103
  response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
104
- data = response.json()
105
- if data.get('success'):
106
- settings = data.get('settings', {})
107
- tables_data = data.get('tables', [])
108
- total_tables = settings.get('total_tables', 10)
109
- seats_per_table = settings.get('seats_per_table', 10)
110
-
111
- # 創建或更新桌次
112
- for i in range(1, total_tables + 1):
113
- table = Table.query.get(i)
114
- if not table:
115
- # 尋找對應的桌次名稱
116
- table_name = ''
117
- for table_data in tables_data:
118
- if table_data.get('id') == i:
119
- table_name = table_data.get('name', '')
120
- break
121
- table = Table(id=i, name=table_name, capacity=seats_per_table)
122
- db.session.add(table)
123
- else:
124
- # 更新現有桌次的容量
125
- table.capacity = seats_per_table
126
- # 更新桌次名稱(如果有)
127
- for table_data in tables_data:
128
- if table_data.get('id') == i and 'name' in table_data:
129
- table.name = table_data['name']
130
- break
131
-
132
- # 刪除多餘的桌次
133
- extra_tables = Table.query.filter(Table.id > total_tables).all()
134
- for table in extra_tables:
135
- if len(table.participants) == 0: # 只刪除沒有參與者的桌次
136
- db.session.delete(table)
 
 
 
 
137
 
138
- db.session.commit()
139
- print(f"已更新桌次設定:總桌數={total_tables}, 每桌人數={seats_per_table}")
140
- else:
141
- print("無法從 Google Sheets 獲取設定,使用預設值")
142
- # 使用預設值創建桌次
143
- if Table.query.count() == 0:
144
- for i in range(1, 11): # 預設 10 張桌子
145
- table = Table(id=i, capacity=10) # 預設每桌 10 人
146
- db.session.add(table)
147
- db.session.commit()
148
- print("已創建預設桌次")
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  except Exception as e:
150
  print(f"從 Google Sheets 獲取設定時出錯: {str(e)}")
151
- # 如果出錯,使用預設值
 
 
152
  if Table.query.count() == 0:
153
  for i in range(1, 11): # 預設 10 張桌子
154
  table = Table(id=i, capacity=10) # 預設每桌 10 人
@@ -376,7 +399,7 @@ def update_settings():
376
  # 需要刪除多餘的桌次
377
  # 檢查要刪除的桌次是否有參與者
378
  for table in current_tables[total_tables:]:
379
- if len(table.participants) > 0:
380
  return jsonify({
381
  'error': f'無法刪除第 {table.id} 桌,因為還有參與者在該桌'
382
  }), 400
 
100
  # 從 Google Sheets 獲取設定
101
  print("正在從 Google Sheets 獲取設定...")
102
  try:
103
+ if not app.config.get('SETTINGS_SCRIPT_URL'):
104
+ print("未設定 SETTINGS_SCRIPT_URL,使用預設值")
105
+ raise Exception("未設定 SETTINGS_SCRIPT_URL")
106
+
107
  response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
108
+ if response.status_code != 200:
109
+ print(f"Google Sheets API 返回錯誤狀態碼: {response.status_code}")
110
+ raise Exception(f"Google Sheets API 返回錯誤狀態碼: {response.status_code}")
111
+
112
+ try:
113
+ data = response.json()
114
+ except ValueError as e:
115
+ print("JSON 解析錯誤:", str(e))
116
+ print("原始回應內容:", response.text)
117
+ raise
118
+
119
+ if not data.get('success'):
120
+ error_msg = data.get('error', '從 Google Sheets 獲取資料失敗')
121
+ print("Google Sheets 返回錯誤:", error_msg)
122
+ raise Exception(error_msg)
123
+
124
+ settings = data.get('settings', {})
125
+ tables_data = data.get('tables', [])
126
+ total_tables = settings.get('total_tables', 10)
127
+ seats_per_table = settings.get('seats_per_table', 10)
128
+
129
+ print(f"從 Google Sheets 獲取到的資料:")
130
+ print(f"- 總桌數: {total_tables}")
131
+ print(f"- 每桌人數: {seats_per_table}")
132
+ print(f"- 桌次資料: {tables_data}")
133
+
134
+ # 檢查現有桌次
135
+ existing_tables = {table.id: table for table in Table.query.all()}
136
+
137
+ # 創建或更新桌次
138
+ for i in range(1, total_tables + 1):
139
+ # 尋找對應的桌次名稱
140
+ table_name = None
141
+ for table_data in tables_data:
142
+ if table_data.get('id') == i:
143
+ table_name = table_data.get('name')
144
+ break
145
 
146
+ if i in existing_tables:
147
+ table = existing_tables[i]
148
+ table.capacity = seats_per_table
149
+ if table_name is not None: # 只有當找到名稱時才更新
150
+ table.name = table_name
151
+ print(f"更新第 {i} 桌名稱為: {table_name}")
152
+ else:
153
+ table = Table(
154
+ id=i,
155
+ name=table_name if table_name is not None else '',
156
+ capacity=seats_per_table
157
+ )
158
+ db.session.add(table)
159
+ print(f"創建第 {i} 桌,名稱為: {table_name if table_name is not None else '(空)'}")
160
+
161
+ # 刪除多餘的桌次
162
+ for table_id, table in existing_tables.items():
163
+ if table_id > total_tables and len(table.participants) == 0:
164
+ db.session.delete(table)
165
+ print(f"刪除第 {table_id} 桌")
166
+
167
+ db.session.commit()
168
+ print(f"已更新桌次設定:總桌數={total_tables}, 每桌人數={seats_per_table}")
169
+
170
  except Exception as e:
171
  print(f"從 Google Sheets 獲取設定時出錯: {str(e)}")
172
+ print("使用預設值")
173
+
174
+ # 如果沒有任何桌次,創建預設桌次
175
  if Table.query.count() == 0:
176
  for i in range(1, 11): # 預設 10 張桌子
177
  table = Table(id=i, capacity=10) # 預設每桌 10 人
 
399
  # 需要刪除多餘的桌次
400
  # 檢查要刪除的桌次是否有參與者
401
  for table in current_tables[total_tables:]:
402
+ if len(table.participants) > 0: # 只刪除沒有參與者的桌次
403
  return jsonify({
404
  'error': f'無法刪除第 {table.id} 桌,因為還有參與者在該桌'
405
  }), 400