Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
8261b7f
1
Parent(s): fde281b
更新桌子
Browse files
app.py
CHANGED
|
@@ -130,7 +130,7 @@ def init_db():
|
|
| 130 |
# 在應用啟動時初始化資料庫
|
| 131 |
with app.app_context():
|
| 132 |
try:
|
| 133 |
-
# 創建資料庫表格
|
| 134 |
db.create_all()
|
| 135 |
print("資料庫表格創建成功")
|
| 136 |
|
|
@@ -138,9 +138,9 @@ with app.app_context():
|
|
| 138 |
db.session.execute('SELECT 1')
|
| 139 |
print("資料庫初始化成功")
|
| 140 |
|
| 141 |
-
#
|
| 142 |
-
if Table.query.count() == 0:
|
| 143 |
-
print("沒有
|
| 144 |
try:
|
| 145 |
response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
|
| 146 |
print(f"Google Sheets 回應狀態碼: {response.status_code}")
|
|
@@ -152,56 +152,48 @@ with app.app_context():
|
|
| 152 |
settings = data.get('settings', {})
|
| 153 |
total_tables = settings.get('total_tables', 10)
|
| 154 |
seats_per_table = settings.get('seats_per_table', 10)
|
| 155 |
-
|
| 156 |
print(f"從 Google Sheets 恢復設定:總桌數={total_tables}, 每桌人數={seats_per_table}")
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
db.session.add(table)
|
| 165 |
-
|
| 166 |
db.session.commit()
|
| 167 |
print("桌次設定恢復完成")
|
| 168 |
|
| 169 |
-
#
|
| 170 |
-
|
| 171 |
-
print(f"從 Google Sheets 找到 {len(
|
| 172 |
|
| 173 |
-
for participant_data in
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
db.session.add(participant)
|
| 192 |
-
print(f"已添加參與者: {participant.nickname}")
|
| 193 |
-
except Exception as e:
|
| 194 |
-
print(f"處理參與者資料時出錯: {str(e)}")
|
| 195 |
-
print(f"問題資料: {participant_data}")
|
| 196 |
-
continue
|
| 197 |
|
| 198 |
db.session.commit()
|
| 199 |
print("參與者資料恢復完成")
|
| 200 |
-
else:
|
| 201 |
-
print(f"從 Google Sheets 恢復資料失敗: {data.get('error', '未知錯誤')}")
|
| 202 |
except Exception as e:
|
| 203 |
print(f"從 Google Sheets 恢復資料時出錯: {str(e)}")
|
| 204 |
-
|
|
|
|
|
|
|
| 205 |
except Exception as e:
|
| 206 |
print(f"初始化資料庫時出錯: {str(e)}")
|
| 207 |
|
|
|
|
| 130 |
# 在應用啟動時初始化資料庫
|
| 131 |
with app.app_context():
|
| 132 |
try:
|
| 133 |
+
# 創建資料庫表格(如果不存在)
|
| 134 |
db.create_all()
|
| 135 |
print("資料庫表格創建成功")
|
| 136 |
|
|
|
|
| 138 |
db.session.execute('SELECT 1')
|
| 139 |
print("資料庫初始化成功")
|
| 140 |
|
| 141 |
+
# 只在完全沒有桌子和參與者時才從 Google Sheets 恢復資料
|
| 142 |
+
if Table.query.count() == 0 and Participant.query.count() == 0:
|
| 143 |
+
print("沒有任何資料,嘗試從 Google Sheets 恢復資料...")
|
| 144 |
try:
|
| 145 |
response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
|
| 146 |
print(f"Google Sheets 回應狀態碼: {response.status_code}")
|
|
|
|
| 152 |
settings = data.get('settings', {})
|
| 153 |
total_tables = settings.get('total_tables', 10)
|
| 154 |
seats_per_table = settings.get('seats_per_table', 10)
|
|
|
|
| 155 |
print(f"從 Google Sheets 恢復設定:總桌數={total_tables}, 每桌人數={seats_per_table}")
|
| 156 |
|
| 157 |
+
# 創建桌子
|
| 158 |
+
for i in range(1, total_tables + 1):
|
| 159 |
+
table = Table(
|
| 160 |
+
name=f'第{i}桌',
|
| 161 |
+
capacity=seats_per_table
|
| 162 |
+
)
|
| 163 |
db.session.add(table)
|
|
|
|
| 164 |
db.session.commit()
|
| 165 |
print("桌次設定恢復完成")
|
| 166 |
|
| 167 |
+
# 恢復參與者資料
|
| 168 |
+
participants_data = data.get('participants', [])
|
| 169 |
+
print(f"從 Google Sheets 找到 {len(participants_data)} 個參與者")
|
| 170 |
|
| 171 |
+
for participant_data in participants_data:
|
| 172 |
+
created_at_str = participant_data['created_at'].replace('T', ' ').replace('.000Z', '')
|
| 173 |
+
created_at = datetime.strptime(created_at_str, '%Y-%m-%d %H:%M:%S')
|
| 174 |
+
|
| 175 |
+
participant = Participant(
|
| 176 |
+
nickname=participant_data['nickname'],
|
| 177 |
+
email=participant_data['email'],
|
| 178 |
+
fb_link=participant_data.get('fb_link', ''),
|
| 179 |
+
introduction=participant_data.get('introduction', ''),
|
| 180 |
+
table_id=participant_data['table_id'],
|
| 181 |
+
seat_number=participant_data['seat_number'],
|
| 182 |
+
is_table_leader=participant_data.get('is_table_leader', False),
|
| 183 |
+
created_at=created_at,
|
| 184 |
+
has_paid=participant_data.get('has_paid', False),
|
| 185 |
+
note=participant_data.get('note', '')
|
| 186 |
+
)
|
| 187 |
+
db.session.add(participant)
|
| 188 |
+
print(f"已添加參與者: {participant.nickname}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
db.session.commit()
|
| 191 |
print("參與者資料恢復完成")
|
|
|
|
|
|
|
| 192 |
except Exception as e:
|
| 193 |
print(f"從 Google Sheets 恢復資料時出錯: {str(e)}")
|
| 194 |
+
else:
|
| 195 |
+
print(f"資料庫中已有資料:{Table.query.count()} 張桌子,{Participant.query.count()} 位參與者")
|
| 196 |
+
|
| 197 |
except Exception as e:
|
| 198 |
print(f"初始化資料庫時出錯: {str(e)}")
|
| 199 |
|