m1m02530125 Claude Sonnet 4.6 commited on
Commit
ae2adb5
·
1 Parent(s): 341515d

アルバイトの希望シフト入力UXを改善:過去パターン提案+行色分け

Browse files

- db/repositories.py: get_employee_dow_patterns() を追加
曜日ごとに最も多く希望したパターンを返す(2回以上が対象)
- routes/shifts.py: アルバイト表示時に top_patterns / dow_suggestions を計算してテンプレートへ渡す
- templates/shifts/input.html:
- よく使うパターン提案バナーを追加(ワンタップで全日に適用)
- 曜日別おすすめで一括入力ボタン(曜日×パターン頻度から推定)
- パターンカテゴリ別の行左ボーダー色分け(朝食=青/ディナー=橙/通し=緑/中番=紫)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (3) hide show
  1. db/repositories.py +27 -0
  2. routes/shifts.py +15 -2
  3. templates/shifts/input.html +101 -12
db/repositories.py CHANGED
@@ -567,6 +567,33 @@ def get_employee_pattern_history(emp_id: int) -> list[tuple[str, int]]:
567
  return [(r["pattern_id"], r["cnt"]) for r in rows]
568
 
569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
  def get_schedule_notes(period_id: int) -> dict[str, str]:
571
  """期間の備考を {date_str: note} で返す"""
572
  conn = get_connection()
 
567
  return [(r["pattern_id"], r["cnt"]) for r in rows]
568
 
569
 
570
+ def get_employee_dow_patterns(emp_id: int) -> dict[int, str]:
571
+ """従業員の曜日別最頻パターンを {python_weekday(0=月..6=日): pattern_id} で返す。
572
+ 2回以上出現したパターンのみ提案対象とする。"""
573
+ conn = get_connection()
574
+ rows = conn.execute(
575
+ """SELECT strftime('%w', date) as dow, pattern_id, COUNT(*) as cnt
576
+ FROM shift_requests
577
+ WHERE employee_id=? AND pattern_id IS NOT NULL AND pattern_id != ''
578
+ AND pattern_id != 'custom'
579
+ GROUP BY dow, pattern_id""",
580
+ (emp_id,)
581
+ ).fetchall()
582
+ conn.close()
583
+ # SQLite strftime('%w'): 0=日,1=月..6=土 → Python weekday: 0=月..6=日
584
+ sqlite_to_py = {0: 6, 1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5}
585
+ dow_counts: dict[int, dict[str, int]] = {}
586
+ for r in rows:
587
+ py_dow = sqlite_to_py[int(r["dow"])]
588
+ dow_counts.setdefault(py_dow, {})
589
+ dow_counts[py_dow][r["pattern_id"]] = dow_counts[py_dow].get(r["pattern_id"], 0) + r["cnt"]
590
+ return {
591
+ dow: max(patterns, key=patterns.get)
592
+ for dow, patterns in dow_counts.items()
593
+ if max(patterns.values()) >= 2
594
+ }
595
+
596
+
597
  def get_schedule_notes(period_id: int) -> dict[str, str]:
598
  """期間の備考を {date_str: note} で返す"""
599
  conn = get_connection()
routes/shifts.py CHANGED
@@ -4,6 +4,7 @@ from flask import Blueprint, render_template, request, redirect, url_for, flash
4
  from db import repositories as repo
5
  from models.schedule import SchedulePeriod, ShiftRequest
6
  from utils.shift_patterns import ALL_PATTERNS, PATTERN_MAP
 
7
 
8
  bp = Blueprint("shifts", __name__, url_prefix="/shifts")
9
 
@@ -82,13 +83,22 @@ def input(period_id):
82
  next_idx = emp_idx + 1 if emp_idx < len(employees) - 1 else None
83
 
84
  # 入力済み従業員数(社員は出勤前提なので常にカウント)
85
- from utils.constants import EmploymentType as _ET
86
  filled_emp_ids = {r.employee_id for r in requests_list}
87
  filled_count = sum(
88
  1 for e in employees
89
- if e.id in filled_emp_ids or e.employment_type == _ET.FULL_TIME
90
  )
91
 
 
 
 
 
 
 
 
 
 
 
92
  return render_template(
93
  "shifts/input.html",
94
  period=period,
@@ -103,6 +113,9 @@ def input(period_id):
103
  filled_count=filled_count,
104
  filled_emp_ids=filled_emp_ids,
105
  DAY_JP=["月", "火", "水", "木", "金", "土", "日"],
 
 
 
106
  )
107
 
108
 
 
4
  from db import repositories as repo
5
  from models.schedule import SchedulePeriod, ShiftRequest
6
  from utils.shift_patterns import ALL_PATTERNS, PATTERN_MAP
7
+ from utils.constants import EmploymentType
8
 
9
  bp = Blueprint("shifts", __name__, url_prefix="/shifts")
10
 
 
83
  next_idx = emp_idx + 1 if emp_idx < len(employees) - 1 else None
84
 
85
  # 入力済み従業員数(社員は出勤前提なので常にカウント)
 
86
  filled_emp_ids = {r.employee_id for r in requests_list}
87
  filled_count = sum(
88
  1 for e in employees
89
+ if e.id in filled_emp_ids or e.employment_type == EmploymentType.FULL_TIME
90
  )
91
 
92
+ # アルバイトの場合のみ過去パターン提案を計算
93
+ top_patterns = []
94
+ dow_suggestions = {}
95
+ if current_emp and current_emp.employment_type != EmploymentType.FULL_TIME:
96
+ top_patterns = [
97
+ (pid, cnt) for pid, cnt in repo.get_employee_pattern_history(current_emp.id)
98
+ if pid in PATTERN_MAP
99
+ ]
100
+ dow_suggestions = repo.get_employee_dow_patterns(current_emp.id)
101
+
102
  return render_template(
103
  "shifts/input.html",
104
  period=period,
 
113
  filled_count=filled_count,
114
  filled_emp_ids=filled_emp_ids,
115
  DAY_JP=["月", "火", "水", "木", "金", "土", "日"],
116
+ top_patterns=top_patterns,
117
+ dow_suggestions=dow_suggestions,
118
+ PATTERN_MAP=PATTERN_MAP,
119
  )
120
 
121
 
templates/shifts/input.html CHANGED
@@ -36,6 +36,13 @@
36
  .custom-time { justify-content: space-between; }
37
  .custom-time input { width: 45% !important; font-size: .72rem; }
38
  }
 
 
 
 
 
 
 
39
  </style>
40
  {% endblock %}
41
  {% block content %}
@@ -158,18 +165,45 @@
158
  </button>
159
  </div>
160
  {% else %}
161
- <div class="px-3 pb-2 d-flex flex-wrap gap-2 align-items-center mobile-btn-wrap">
162
- <span class="small text-muted flex-shrink-0">一括:</span>
163
- <select id="bulk-pattern" class="form-select form-select-sm flex-grow-1 flex-md-grow-0" style="max-width:160px">
164
- <option value="">(休み)</option>
165
- {% for p in patterns %}
166
- <option value="{{ p.id }}">{{ p.label }}</option>
167
- {% endfor %}
168
- </select>
169
- <button type="button" class="btn btn-outline-secondary btn-sm text-nowrap flex-grow-1 flex-md-grow-0"
170
- onclick="applyBulk('{{ current_emp.id }}')">
171
- 全日に適用
172
- </button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  </div>
174
  {% endif %}
175
 
@@ -424,9 +458,64 @@ function applyBulk(empId) {
424
  document.querySelectorAll(`select[name^="pattern_${empId}_"]`).forEach(sel => {
425
  sel.value = pattern;
426
  toggleCustom(sel, sel.name.replace(`pattern_${empId}_`, empId + '_'));
 
427
  });
 
428
  }
429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430
  // ── 名前検索 ──────────────────────────────────────────
431
  const EMP_LIST = [
432
  {% for e in employees %}
 
36
  .custom-time { justify-content: space-between; }
37
  .custom-time input { width: 45% !important; font-size: .72rem; }
38
  }
39
+ /* パターンカテゴリ別の行色分け */
40
+ tr.pcat-b > th:first-child { border-left: 4px solid #3b82f6 !important; }
41
+ tr.pcat-d > th:first-child { border-left: 4px solid #f97316 !important; }
42
+ tr.pcat-m > th:first-child { border-left: 4px solid #a855f7 !important; }
43
+ tr.pcat-db > th:first-child { border-left: 4px solid #10b981 !important; }
44
+ .suggest-banner { background:#eff6ff; border:1px solid #bfdbfe; border-radius:10px; }
45
+ .suggest-banner .label { color:#1d4ed8; font-size:.78rem; font-weight:600; }
46
  </style>
47
  {% endblock %}
48
  {% block content %}
 
165
  </button>
166
  </div>
167
  {% else %}
168
+ <div class="px-3 pb-2">
169
+ {# 過去パターン提案バナー #}
170
+ {% if top_patterns %}
171
+ <div class="suggest-banner p-2 mb-2">
172
+ <div class="label mb-1"><i class="bi bi-clock-history me-1"></i>よく使うパターン(タップで全日に適用)</div>
173
+ <div class="d-flex flex-wrap gap-1 mb-2">
174
+ {% for pid, cnt in top_patterns[:2] %}
175
+ {% set p = PATTERN_MAP.get(pid) %}
176
+ {% if p %}
177
+ <button type="button" class="btn btn-outline-primary btn-sm"
178
+ onclick="applyAllPattern('{{ current_emp.id }}', '{{ pid }}')">
179
+ {{ p.label }}
180
+ <span class="badge bg-primary bg-opacity-75 ms-1">{{ cnt }}回</span>
181
+ </button>
182
+ {% endif %}
183
+ {% endfor %}
184
+ </div>
185
+ {% if dow_suggestions %}
186
+ <button type="button" class="btn btn-primary btn-sm w-100"
187
+ onclick="applyDowSuggestions('{{ current_emp.id }}')">
188
+ <i class="bi bi-calendar3 me-1"></i>曜日別おすすめで一括入力
189
+ </button>
190
+ {% endif %}
191
+ </div>
192
+ {% endif %}
193
+ {# 手動一括入力 #}
194
+ <div class="d-flex flex-wrap gap-2 align-items-center mobile-btn-wrap">
195
+ <span class="small text-muted flex-shrink-0">一括:</span>
196
+ <select id="bulk-pattern" class="form-select form-select-sm flex-grow-1 flex-md-grow-0" style="max-width:160px">
197
+ <option value="">(休み)</option>
198
+ {% for p in patterns %}
199
+ <option value="{{ p.id }}">{{ p.label }}</option>
200
+ {% endfor %}
201
+ </select>
202
+ <button type="button" class="btn btn-outline-secondary btn-sm text-nowrap flex-grow-1 flex-md-grow-0"
203
+ onclick="applyBulk('{{ current_emp.id }}')">
204
+ 全日に適用
205
+ </button>
206
+ </div>
207
  </div>
208
  {% endif %}
209
 
 
458
  document.querySelectorAll(`select[name^="pattern_${empId}_"]`).forEach(sel => {
459
  sel.value = pattern;
460
  toggleCustom(sel, sel.name.replace(`pattern_${empId}_`, empId + '_'));
461
+ updateRowColor(sel);
462
  });
463
+ _changed = true;
464
  }
465
 
466
+ // ── パターン提案 ──────────────────────────────────────────────────
467
+ const DOW_SUGGESTIONS = {{ dow_suggestions | tojson if dow_suggestions is defined else '{}' }};
468
+
469
+ const PATTERN_CATS = {
470
+ b_short:'b', b_std:'b', b_long:'b', b_half:'b',
471
+ mid:'m',
472
+ d_full1:'d', d_full2:'d',
473
+ d_std1:'d', d_std2:'d', d_std3:'d',
474
+ d_s1:'d', d_s2:'d', d_s3:'d',
475
+ double:'db',
476
+ };
477
+
478
+ function updateRowColor(sel) {
479
+ const row = sel.closest('tr');
480
+ if (!row) return;
481
+ row.classList.remove('pcat-b', 'pcat-d', 'pcat-m', 'pcat-db');
482
+ const cat = PATTERN_CATS[sel.value];
483
+ if (cat) row.classList.add('pcat-' + cat);
484
+ }
485
+
486
+ function applyAllPattern(empId, patternId) {
487
+ document.querySelectorAll(`select[name^="pattern_${empId}_"]`).forEach(sel => {
488
+ sel.value = patternId;
489
+ const dateStr = sel.name.replace(`pattern_${empId}_`, '');
490
+ toggleCustom(sel, `${empId}_${dateStr}`);
491
+ updateRowColor(sel);
492
+ });
493
+ _changed = true;
494
+ }
495
+
496
+ function applyDowSuggestions(empId) {
497
+ document.querySelectorAll(`select[name^="pattern_${empId}_"]`).forEach(sel => {
498
+ const dateStr = sel.name.replace(`pattern_${empId}_`, '');
499
+ const d = new Date(dateStr + 'T00:00:00');
500
+ // JS getDay(): 0=日..6=土 → Python weekday: 0=月..6=日
501
+ const jsDow = d.getDay();
502
+ const pyDow = jsDow === 0 ? 6 : jsDow - 1;
503
+ const suggested = DOW_SUGGESTIONS[String(pyDow)];
504
+ if (suggested !== undefined) {
505
+ sel.value = suggested;
506
+ toggleCustom(sel, `${empId}_${dateStr}`);
507
+ updateRowColor(sel);
508
+ }
509
+ });
510
+ _changed = true;
511
+ }
512
+
513
+ // 行の色分けを��期化 & changeイベントで更新
514
+ document.querySelectorAll('select.pattern-sel').forEach(sel => {
515
+ updateRowColor(sel);
516
+ sel.addEventListener('change', () => { updateRowColor(sel); });
517
+ });
518
+
519
  // ── 名前検索 ──────────────────────────────────────────
520
  const EMP_LIST = [
521
  {% for e in employees %}