Commit ·
94e960e
1
Parent(s): 4e49a00
Drag & drop staff reordering with display_order persistence
Browse files- employees.display_order column (migration, DEFAULT 0)
- get_all_employees: ORDER BY display_order, id
- reorder_employees(): bulk UPDATE display_order
- POST /employees/reorder endpoint
- SortableJS (CDN) with drag handle on employee cards
- Debounced auto-save (500ms after last drag)
- Saving indicator during API call
- Handle hidden during search/archive view
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- db/database.py +1 -0
- db/repositories.py +11 -1
- routes/employees.py +10 -1
- templates/employees/index.html +55 -1
db/database.py
CHANGED
|
@@ -252,6 +252,7 @@ _MIGRATIONS = [
|
|
| 252 |
("employees", "always_available_dinner", "INTEGER NOT NULL DEFAULT 0"),
|
| 253 |
("schedule_periods", "gen_status", "TEXT NOT NULL DEFAULT 'idle'"),
|
| 254 |
("schedule_periods", "gen_message", "TEXT NOT NULL DEFAULT ''"),
|
|
|
|
| 255 |
]
|
| 256 |
|
| 257 |
_INDEXES = [
|
|
|
|
| 252 |
("employees", "always_available_dinner", "INTEGER NOT NULL DEFAULT 0"),
|
| 253 |
("schedule_periods", "gen_status", "TEXT NOT NULL DEFAULT 'idle'"),
|
| 254 |
("schedule_periods", "gen_message", "TEXT NOT NULL DEFAULT ''"),
|
| 255 |
+
("employees", "display_order", "INTEGER NOT NULL DEFAULT 0"),
|
| 256 |
]
|
| 257 |
|
| 258 |
_INDEXES = [
|
db/repositories.py
CHANGED
|
@@ -14,7 +14,7 @@ from cache import cache
|
|
| 14 |
def get_all_employees(active_only: bool = True) -> list[Employee]:
|
| 15 |
conn = get_connection()
|
| 16 |
rows = conn.execute(
|
| 17 |
-
"SELECT * FROM employees" + (" WHERE is_active=1" if active_only else "") + " ORDER BY id"
|
| 18 |
).fetchall()
|
| 19 |
if not rows:
|
| 20 |
conn.close()
|
|
@@ -100,6 +100,16 @@ def restore_employee(employee_id: int):
|
|
| 100 |
cache.delete_memoized(get_all_employees)
|
| 101 |
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def _row_to_employee_preloaded(row, pat_map: dict, unavail_map: dict) -> Employee:
|
| 104 |
eid = row["id"]
|
| 105 |
keys = row.keys()
|
|
|
|
| 14 |
def get_all_employees(active_only: bool = True) -> list[Employee]:
|
| 15 |
conn = get_connection()
|
| 16 |
rows = conn.execute(
|
| 17 |
+
"SELECT * FROM employees" + (" WHERE is_active=1" if active_only else "") + " ORDER BY display_order, id"
|
| 18 |
).fetchall()
|
| 19 |
if not rows:
|
| 20 |
conn.close()
|
|
|
|
| 100 |
cache.delete_memoized(get_all_employees)
|
| 101 |
|
| 102 |
|
| 103 |
+
def reorder_employees(ordered_ids: list[int]):
|
| 104 |
+
"""ドラッグ&ドロップ後の並び順を保存する"""
|
| 105 |
+
conn = get_connection()
|
| 106 |
+
for i, emp_id in enumerate(ordered_ids):
|
| 107 |
+
conn.execute("UPDATE employees SET display_order=? WHERE id=?", (i, emp_id))
|
| 108 |
+
conn.commit()
|
| 109 |
+
conn.close()
|
| 110 |
+
cache.delete_memoized(get_all_employees)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
def _row_to_employee_preloaded(row, pat_map: dict, unavail_map: dict) -> Employee:
|
| 114 |
eid = row["id"]
|
| 115 |
keys = row.keys()
|
routes/employees.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Blueprint, render_template, request, redirect, url_for, flash
|
| 2 |
from db import repositories as repo
|
| 3 |
from models.employee import Employee, FixedPattern
|
| 4 |
from utils.constants import EmploymentType, SkillLevel, PrimaryPosition, TimeSlot, DAY_OF_WEEK_LABELS
|
|
@@ -67,6 +67,15 @@ def update(emp_id):
|
|
| 67 |
return redirect(url_for("employees.index"))
|
| 68 |
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
@bp.post("/<int:emp_id>/archive")
|
| 71 |
def archive(emp_id):
|
| 72 |
repo.delete_employee(emp_id)
|
|
|
|
| 1 |
+
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify
|
| 2 |
from db import repositories as repo
|
| 3 |
from models.employee import Employee, FixedPattern
|
| 4 |
from utils.constants import EmploymentType, SkillLevel, PrimaryPosition, TimeSlot, DAY_OF_WEEK_LABELS
|
|
|
|
| 67 |
return redirect(url_for("employees.index"))
|
| 68 |
|
| 69 |
|
| 70 |
+
@bp.post("/reorder")
|
| 71 |
+
def reorder():
|
| 72 |
+
ids = request.get_json()
|
| 73 |
+
if not isinstance(ids, list):
|
| 74 |
+
return jsonify({"ok": False, "error": "invalid"}), 400
|
| 75 |
+
repo.reorder_employees([int(i) for i in ids])
|
| 76 |
+
return jsonify({"ok": True})
|
| 77 |
+
|
| 78 |
+
|
| 79 |
@bp.post("/<int:emp_id>/archive")
|
| 80 |
def archive(emp_id):
|
| 81 |
repo.delete_employee(emp_id)
|
templates/employees/index.html
CHANGED
|
@@ -20,11 +20,25 @@
|
|
| 20 |
{% endif %}
|
| 21 |
</form>
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
<!-- 従業員一覧 -->
|
|
|
|
| 24 |
{% for emp in employees %}
|
| 25 |
-
<div class="card mb-2 {% if not emp.is_active %}opacity-50{% endif %}"
|
|
|
|
| 26 |
<div class="card-body py-2 px-3">
|
| 27 |
<div class="d-flex align-items-center gap-2">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
<div class="flex-grow-1">
|
| 29 |
<div class="fw-semibold">{{ emp.name }}</div>
|
| 30 |
<div class="text-muted small">
|
|
@@ -57,5 +71,45 @@
|
|
| 57 |
従業員が登録されていません
|
| 58 |
</div>
|
| 59 |
{% endfor %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
</div>
|
| 61 |
{% endblock %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
{% endif %}
|
| 21 |
</form>
|
| 22 |
|
| 23 |
+
{% if not show_archive and not q %}
|
| 24 |
+
<p class="text-muted small mb-2">
|
| 25 |
+
<i class="bi bi-grip-vertical me-1"></i>左端のハンドルをドラッグして並び替えできます
|
| 26 |
+
</p>
|
| 27 |
+
{% endif %}
|
| 28 |
+
|
| 29 |
<!-- 従業員一覧 -->
|
| 30 |
+
<div id="emp-list">
|
| 31 |
{% for emp in employees %}
|
| 32 |
+
<div class="card mb-2 {% if not emp.is_active %}opacity-50{% endif %}"
|
| 33 |
+
data-id="{{ emp.id }}">
|
| 34 |
<div class="card-body py-2 px-3">
|
| 35 |
<div class="d-flex align-items-center gap-2">
|
| 36 |
+
<!-- ドラッグハンドル(検索中・アーカイブ中は非表示) -->
|
| 37 |
+
{% if not show_archive and not q %}
|
| 38 |
+
<span class="drag-handle text-muted" style="cursor:grab;font-size:1.2rem;padding:4px 2px">
|
| 39 |
+
<i class="bi bi-grip-vertical"></i>
|
| 40 |
+
</span>
|
| 41 |
+
{% endif %}
|
| 42 |
<div class="flex-grow-1">
|
| 43 |
<div class="fw-semibold">{{ emp.name }}</div>
|
| 44 |
<div class="text-muted small">
|
|
|
|
| 71 |
従業員が登録されていません
|
| 72 |
</div>
|
| 73 |
{% endfor %}
|
| 74 |
+
</div>
|
| 75 |
+
|
| 76 |
+
<!-- 保存中インジケーター -->
|
| 77 |
+
<div id="order-saving" class="text-center small text-muted mt-2" style="display:none">
|
| 78 |
+
<span class="spinner-border spinner-border-sm me-1"></span>並び順を保存中…
|
| 79 |
+
</div>
|
| 80 |
</div>
|
| 81 |
{% endblock %}
|
| 82 |
+
|
| 83 |
+
{% block scripts %}
|
| 84 |
+
{% if not show_archive and not q %}
|
| 85 |
+
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.2/Sortable.min.js"></script>
|
| 86 |
+
<script>
|
| 87 |
+
const saveIndicator = document.getElementById('order-saving');
|
| 88 |
+
let saveTimer = null;
|
| 89 |
+
|
| 90 |
+
Sortable.create(document.getElementById('emp-list'), {
|
| 91 |
+
handle: '.drag-handle',
|
| 92 |
+
animation: 150,
|
| 93 |
+
ghostClass: 'opacity-50',
|
| 94 |
+
onEnd() {
|
| 95 |
+
// デバウンス:連続操作後に1回だけ保存
|
| 96 |
+
clearTimeout(saveTimer);
|
| 97 |
+
saveTimer = setTimeout(saveOrder, 500);
|
| 98 |
+
}
|
| 99 |
+
});
|
| 100 |
+
|
| 101 |
+
function saveOrder() {
|
| 102 |
+
const ids = [...document.querySelectorAll('#emp-list [data-id]')]
|
| 103 |
+
.map(el => parseInt(el.dataset.id));
|
| 104 |
+
saveIndicator.style.display = 'block';
|
| 105 |
+
fetch('{{ url_for("employees.reorder") }}', {
|
| 106 |
+
method: 'POST',
|
| 107 |
+
headers: {'Content-Type': 'application/json'},
|
| 108 |
+
body: JSON.stringify(ids)
|
| 109 |
+
}).finally(() => {
|
| 110 |
+
saveIndicator.style.display = 'none';
|
| 111 |
+
});
|
| 112 |
+
}
|
| 113 |
+
</script>
|
| 114 |
+
{% endif %}
|
| 115 |
+
{% endblock %}
|