Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
f3205c7
1
Parent(s): 71e734b
修改桌次名稱同步
Browse files
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 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|