Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
c65e601
1
Parent(s): 9841510
Add admin functionality with login and management features
Browse files- app.py +102 -1
- templates/admin.html +172 -0
- templates/admin_login.html +57 -0
app.py
CHANGED
|
@@ -1,10 +1,17 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
from datetime import datetime
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__, instance_relative_config=True)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# 設置資料庫路徑
|
| 9 |
db_path = os.path.join(app.instance_path, 'event.db')
|
| 10 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
|
@@ -41,10 +48,104 @@ with app.app_context():
|
|
| 41 |
except Exception as e:
|
| 42 |
print(f"Error creating database: {e}")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
@app.route('/')
|
| 45 |
def index():
|
| 46 |
return render_template('index.html')
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
@app.route('/api/tables')
|
| 49 |
def get_tables():
|
| 50 |
tables = Table.query.all()
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
from datetime import datetime
|
| 4 |
import os
|
| 5 |
+
from functools import wraps
|
| 6 |
|
| 7 |
app = Flask(__name__, instance_relative_config=True)
|
| 8 |
|
| 9 |
+
# 設置 session secret key
|
| 10 |
+
app.secret_key = os.urandom(24)
|
| 11 |
+
|
| 12 |
+
# 設置管理者密碼(從環境變數獲取)
|
| 13 |
+
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD', '1234') # 預設密碼為 1234
|
| 14 |
+
|
| 15 |
# 設置資料庫路徑
|
| 16 |
db_path = os.path.join(app.instance_path, 'event.db')
|
| 17 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
print(f"Error creating database: {e}")
|
| 50 |
|
| 51 |
+
# 管理者認證裝飾器
|
| 52 |
+
def admin_required(f):
|
| 53 |
+
@wraps(f)
|
| 54 |
+
def decorated_function(*args, **kwargs):
|
| 55 |
+
if not session.get('admin_logged_in'):
|
| 56 |
+
return redirect(url_for('admin_login'))
|
| 57 |
+
return f(*args, **kwargs)
|
| 58 |
+
return decorated_function
|
| 59 |
+
|
| 60 |
@app.route('/')
|
| 61 |
def index():
|
| 62 |
return render_template('index.html')
|
| 63 |
|
| 64 |
+
@app.route('/admin/login')
|
| 65 |
+
def admin_login():
|
| 66 |
+
return render_template('admin_login.html')
|
| 67 |
+
|
| 68 |
+
@app.route('/admin')
|
| 69 |
+
@admin_required
|
| 70 |
+
def admin():
|
| 71 |
+
return render_template('admin.html')
|
| 72 |
+
|
| 73 |
+
@app.route('/api/admin/login', methods=['POST'])
|
| 74 |
+
def admin_login_api():
|
| 75 |
+
data = request.json
|
| 76 |
+
if data.get('password') == ADMIN_PASSWORD:
|
| 77 |
+
session['admin_logged_in'] = True
|
| 78 |
+
return jsonify({'message': 'Login successful'})
|
| 79 |
+
return jsonify({'error': 'Invalid password'}), 401
|
| 80 |
+
|
| 81 |
+
@app.route('/api/admin/settings', methods=['GET', 'POST'])
|
| 82 |
+
@admin_required
|
| 83 |
+
def admin_settings():
|
| 84 |
+
if request.method == 'GET':
|
| 85 |
+
# 獲取目前設定
|
| 86 |
+
total_tables = Table.query.count()
|
| 87 |
+
first_table = Table.query.first()
|
| 88 |
+
seats_per_table = first_table.capacity if first_table else 8
|
| 89 |
+
return jsonify({
|
| 90 |
+
'total_tables': total_tables,
|
| 91 |
+
'seats_per_table': seats_per_table
|
| 92 |
+
})
|
| 93 |
+
else:
|
| 94 |
+
# 更新設定
|
| 95 |
+
data = request.json
|
| 96 |
+
total_tables = data.get('total_tables')
|
| 97 |
+
seats_per_table = data.get('seats_per_table')
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
# 刪除所有現有的桌子
|
| 101 |
+
Table.query.delete()
|
| 102 |
+
|
| 103 |
+
# 創建新的桌子
|
| 104 |
+
for i in range(1, total_tables + 1):
|
| 105 |
+
table = Table(capacity=seats_per_table, name=f'Table {i}')
|
| 106 |
+
db.session.add(table)
|
| 107 |
+
|
| 108 |
+
db.session.commit()
|
| 109 |
+
return jsonify({'message': 'Settings updated successfully'})
|
| 110 |
+
except Exception as e:
|
| 111 |
+
db.session.rollback()
|
| 112 |
+
return jsonify({'error': str(e)}), 400
|
| 113 |
+
|
| 114 |
+
@app.route('/api/admin/participants', methods=['GET'])
|
| 115 |
+
@admin_required
|
| 116 |
+
def admin_participants():
|
| 117 |
+
tables = Table.query.all()
|
| 118 |
+
tables_data = []
|
| 119 |
+
for table in tables:
|
| 120 |
+
participants = Participant.query.filter_by(table_id=table.id).all()
|
| 121 |
+
tables_data.append({
|
| 122 |
+
'id': table.id,
|
| 123 |
+
'name': table.name,
|
| 124 |
+
'capacity': table.capacity,
|
| 125 |
+
'current_seats': len(participants),
|
| 126 |
+
'participants': [{
|
| 127 |
+
'id': p.id,
|
| 128 |
+
'nickname': p.nickname,
|
| 129 |
+
'email': p.email,
|
| 130 |
+
'fb_link': p.fb_link
|
| 131 |
+
} for p in participants]
|
| 132 |
+
})
|
| 133 |
+
return jsonify(tables_data)
|
| 134 |
+
|
| 135 |
+
@app.route('/api/admin/participants/<int:participant_id>', methods=['DELETE'])
|
| 136 |
+
@admin_required
|
| 137 |
+
def delete_participant(participant_id):
|
| 138 |
+
try:
|
| 139 |
+
participant = Participant.query.get(participant_id)
|
| 140 |
+
if participant:
|
| 141 |
+
db.session.delete(participant)
|
| 142 |
+
db.session.commit()
|
| 143 |
+
return jsonify({'message': 'Participant deleted successfully'})
|
| 144 |
+
return jsonify({'error': 'Participant not found'}), 404
|
| 145 |
+
except Exception as e:
|
| 146 |
+
db.session.rollback()
|
| 147 |
+
return jsonify({'error': str(e)}), 400
|
| 148 |
+
|
| 149 |
@app.route('/api/tables')
|
| 150 |
def get_tables():
|
| 151 |
tables = Table.query.all()
|
templates/admin.html
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="zh-TW">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>活動座位管理</title>
|
| 7 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 8 |
+
<link href="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.css" rel="stylesheet">
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<div class="container mt-5">
|
| 12 |
+
<h2>活動座位管理</h2>
|
| 13 |
+
|
| 14 |
+
<!-- 桌子設定 -->
|
| 15 |
+
<div class="card mb-4">
|
| 16 |
+
<div class="card-header">
|
| 17 |
+
<h4>桌子設定</h4>
|
| 18 |
+
</div>
|
| 19 |
+
<div class="card-body">
|
| 20 |
+
<form id="tableSettingsForm">
|
| 21 |
+
<div class="row">
|
| 22 |
+
<div class="col-md-6">
|
| 23 |
+
<div class="mb-3">
|
| 24 |
+
<label for="totalTables" class="form-label">總桌數</label>
|
| 25 |
+
<input type="number" class="form-control" id="totalTables" min="1" required>
|
| 26 |
+
</div>
|
| 27 |
+
</div>
|
| 28 |
+
<div class="col-md-6">
|
| 29 |
+
<div class="mb-3">
|
| 30 |
+
<label for="seatsPerTable" class="form-label">每桌人數</label>
|
| 31 |
+
<input type="number" class="form-control" id="seatsPerTable" min="1" required>
|
| 32 |
+
</div>
|
| 33 |
+
</div>
|
| 34 |
+
</div>
|
| 35 |
+
<button type="submit" class="btn btn-primary">更新設定</button>
|
| 36 |
+
</form>
|
| 37 |
+
</div>
|
| 38 |
+
</div>
|
| 39 |
+
|
| 40 |
+
<!-- 報名者列表 -->
|
| 41 |
+
<div class="card">
|
| 42 |
+
<div class="card-header">
|
| 43 |
+
<h4>報名者列表</h4>
|
| 44 |
+
</div>
|
| 45 |
+
<div class="card-body">
|
| 46 |
+
<div id="participantsList">
|
| 47 |
+
<!-- 這裡會動態載入報名者列表 -->
|
| 48 |
+
</div>
|
| 49 |
+
</div>
|
| 50 |
+
</div>
|
| 51 |
+
</div>
|
| 52 |
+
|
| 53 |
+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
|
| 54 |
+
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>
|
| 55 |
+
<script>
|
| 56 |
+
// 載入目前設定
|
| 57 |
+
async function loadCurrentSettings() {
|
| 58 |
+
const response = await fetch('/api/admin/settings');
|
| 59 |
+
const data = await response.json();
|
| 60 |
+
document.getElementById('totalTables').value = data.total_tables;
|
| 61 |
+
document.getElementById('seatsPerTable').value = data.seats_per_table;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// 更新設定
|
| 65 |
+
document.getElementById('tableSettingsForm').addEventListener('submit', async (e) => {
|
| 66 |
+
e.preventDefault();
|
| 67 |
+
const totalTables = document.getElementById('totalTables').value;
|
| 68 |
+
const seatsPerTable = document.getElementById('seatsPerTable').value;
|
| 69 |
+
|
| 70 |
+
try {
|
| 71 |
+
const response = await fetch('/api/admin/settings', {
|
| 72 |
+
method: 'POST',
|
| 73 |
+
headers: {
|
| 74 |
+
'Content-Type': 'application/json',
|
| 75 |
+
},
|
| 76 |
+
body: JSON.stringify({
|
| 77 |
+
total_tables: parseInt(totalTables),
|
| 78 |
+
seats_per_table: parseInt(seatsPerTable)
|
| 79 |
+
})
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
if (response.ok) {
|
| 83 |
+
Swal.fire('成功', '設定已更新', 'success');
|
| 84 |
+
loadParticipants();
|
| 85 |
+
} else {
|
| 86 |
+
throw new Error('更新失敗');
|
| 87 |
+
}
|
| 88 |
+
} catch (error) {
|
| 89 |
+
Swal.fire('錯誤', error.message, 'error');
|
| 90 |
+
}
|
| 91 |
+
});
|
| 92 |
+
|
| 93 |
+
// 載入報名者列表
|
| 94 |
+
async function loadParticipants() {
|
| 95 |
+
const response = await fetch('/api/admin/participants');
|
| 96 |
+
const tables = await response.json();
|
| 97 |
+
|
| 98 |
+
const participantsList = document.getElementById('participantsList');
|
| 99 |
+
participantsList.innerHTML = tables.map(table => `
|
| 100 |
+
<div class="card mb-3">
|
| 101 |
+
<div class="card-header">
|
| 102 |
+
<h5>${table.name} (${table.current_seats}/${table.capacity}人)</h5>
|
| 103 |
+
</div>
|
| 104 |
+
<div class="card-body">
|
| 105 |
+
<div class="table-responsive">
|
| 106 |
+
<table class="table">
|
| 107 |
+
<thead>
|
| 108 |
+
<tr>
|
| 109 |
+
<th>暱稱</th>
|
| 110 |
+
<th>Email</th>
|
| 111 |
+
<th>FB連結</th>
|
| 112 |
+
<th>操作</th>
|
| 113 |
+
</tr>
|
| 114 |
+
</thead>
|
| 115 |
+
<tbody>
|
| 116 |
+
${table.participants.map(p => `
|
| 117 |
+
<tr>
|
| 118 |
+
<td>${p.nickname}</td>
|
| 119 |
+
<td>${p.email}</td>
|
| 120 |
+
<td><a href="${p.fb_link}" target="_blank">FB連結</a></td>
|
| 121 |
+
<td>
|
| 122 |
+
<button class="btn btn-danger btn-sm" onclick="deleteParticipant(${p.id})">
|
| 123 |
+
刪除
|
| 124 |
+
</button>
|
| 125 |
+
</td>
|
| 126 |
+
</tr>
|
| 127 |
+
`).join('')}
|
| 128 |
+
</tbody>
|
| 129 |
+
</table>
|
| 130 |
+
</div>
|
| 131 |
+
</div>
|
| 132 |
+
</div>
|
| 133 |
+
`).join('');
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
// 刪除報名者
|
| 137 |
+
async function deleteParticipant(participantId) {
|
| 138 |
+
const result = await Swal.fire({
|
| 139 |
+
title: '確定要刪除嗎?',
|
| 140 |
+
text: "此操作無法復原!",
|
| 141 |
+
icon: 'warning',
|
| 142 |
+
showCancelButton: true,
|
| 143 |
+
confirmButtonColor: '#d33',
|
| 144 |
+
cancelButtonColor: '#3085d6',
|
| 145 |
+
confirmButtonText: '是的,刪除!',
|
| 146 |
+
cancelButtonText: '取消'
|
| 147 |
+
});
|
| 148 |
+
|
| 149 |
+
if (result.isConfirmed) {
|
| 150 |
+
try {
|
| 151 |
+
const response = await fetch(`/api/admin/participants/${participantId}`, {
|
| 152 |
+
method: 'DELETE'
|
| 153 |
+
});
|
| 154 |
+
|
| 155 |
+
if (response.ok) {
|
| 156 |
+
Swal.fire('已刪除!', '報名者已被刪除。', 'success');
|
| 157 |
+
loadParticipants();
|
| 158 |
+
} else {
|
| 159 |
+
throw new Error('刪除失敗');
|
| 160 |
+
}
|
| 161 |
+
} catch (error) {
|
| 162 |
+
Swal.fire('錯誤', error.message, 'error');
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
// 初始載入
|
| 168 |
+
loadCurrentSettings();
|
| 169 |
+
loadParticipants();
|
| 170 |
+
</script>
|
| 171 |
+
</body>
|
| 172 |
+
</html>
|
templates/admin_login.html
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="zh-TW">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>管理者登入</title>
|
| 7 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 8 |
+
</head>
|
| 9 |
+
<body>
|
| 10 |
+
<div class="container mt-5">
|
| 11 |
+
<div class="row justify-content-center">
|
| 12 |
+
<div class="col-md-6">
|
| 13 |
+
<div class="card">
|
| 14 |
+
<div class="card-header">
|
| 15 |
+
<h3 class="text-center">管理者登入</h3>
|
| 16 |
+
</div>
|
| 17 |
+
<div class="card-body">
|
| 18 |
+
<form id="loginForm">
|
| 19 |
+
<div class="mb-3">
|
| 20 |
+
<label for="password" class="form-label">密碼</label>
|
| 21 |
+
<input type="password" class="form-control" id="password" required>
|
| 22 |
+
</div>
|
| 23 |
+
<button type="submit" class="btn btn-primary w-100">登入</button>
|
| 24 |
+
</form>
|
| 25 |
+
</div>
|
| 26 |
+
</div>
|
| 27 |
+
</div>
|
| 28 |
+
</div>
|
| 29 |
+
</div>
|
| 30 |
+
|
| 31 |
+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
|
| 32 |
+
<script>
|
| 33 |
+
document.getElementById('loginForm').addEventListener('submit', async (e) => {
|
| 34 |
+
e.preventDefault();
|
| 35 |
+
const password = document.getElementById('password').value;
|
| 36 |
+
|
| 37 |
+
try {
|
| 38 |
+
const response = await fetch('/api/admin/login', {
|
| 39 |
+
method: 'POST',
|
| 40 |
+
headers: {
|
| 41 |
+
'Content-Type': 'application/json',
|
| 42 |
+
},
|
| 43 |
+
body: JSON.stringify({ password })
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
if (response.ok) {
|
| 47 |
+
window.location.href = '/admin';
|
| 48 |
+
} else {
|
| 49 |
+
alert('密碼錯誤');
|
| 50 |
+
}
|
| 51 |
+
} catch (error) {
|
| 52 |
+
alert('登入失敗');
|
| 53 |
+
}
|
| 54 |
+
});
|
| 55 |
+
</script>
|
| 56 |
+
</body>
|
| 57 |
+
</html>
|