m1m02530125 Claude Sonnet 4.6 commited on
Commit
3b24624
·
1 Parent(s): 4a4580d

feat: 「希望シフト [N]」形式CSVと時刻範囲値に対応

Browse files

- _parse_numbered_shift_header(): 「希望シフト [N]」列を期間の開始日からのオフセットで日付に変換
- _time_range_to_pattern(): 「6:00~10:00」形式の時刻範囲をパターンIDに変換
- 既存パターンと一致すれば pattern_id を返す(例: 6:00~10:00 → b_short)
- 一致しなければ custom として start/end を保持(例: 9:00~14:00 → custom)
- parse_forms_csv の列検出で [N] 形式も自動認識

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

Files changed (1) hide show
  1. utils/forms_csv_parser.py +67 -8
utils/forms_csv_parser.py CHANGED
@@ -51,6 +51,54 @@ def _parse_date_from_header(header: str, period: SchedulePeriod) -> Optional[str
51
  return None
52
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def _parse_custom_times(raw: str, period: SchedulePeriod) -> dict[str, tuple[str, str]]:
55
  """
56
  「4/1: 10:00〜16:00, 4/5: 11:00〜20:00」などを解析して
@@ -137,6 +185,8 @@ def parse_forms_csv(
137
  date_cols: dict[str, dict[str, str]] = {}
138
  for h in headers:
139
  ds = _parse_date_from_header(h, period)
 
 
140
  if not ds:
141
  continue
142
  if ds not in date_cols:
@@ -304,17 +354,26 @@ def _resolve_time_value(
304
  pattern_id="custom", custom_start=custom_start, custom_end=custom_end, note=note,
305
  )
306
 
307
- pattern_id = _LABEL_TO_ID.get(value)
308
- if pattern_id is None and value not in _LABEL_TO_ID:
309
- result.warnings.append(
310
- f"「{name}」{date_str}: 選択値「{value}」が不明です(スキップ)"
 
311
  )
312
- return None
313
 
314
- return ShiftRequest(
315
- employee_id=emp.id, date=date_str,
316
- pattern_id=pattern_id, custom_start=None, custom_end=None, note=note,
 
 
 
 
 
 
 
 
317
  )
 
318
 
319
 
320
  # Google Forms 設問テンプレート文字列
 
51
  return None
52
 
53
 
54
+ def _parse_numbered_shift_header(header: str, period: SchedulePeriod) -> Optional[str]:
55
+ """
56
+ 「希望シフト [N]」形式のヘッダーから YYYY-MM-DD を返す。
57
+ N は 1 始まりで period.start_date から N-1 日後に対応する。
58
+ 例: 「希望シフト [3]」→ period.start_date + 2 days
59
+ """
60
+ m = re.search(r'\[(\d+)\]', header)
61
+ if not m:
62
+ return None
63
+ n = int(m.group(1))
64
+ dates = list(period.date_range())
65
+ if 1 <= n <= len(dates):
66
+ return dates[n - 1].isoformat()
67
+ return None
68
+
69
+
70
+ def _time_range_to_pattern(time_str: str) -> tuple[Optional[str], Optional[str], Optional[str]]:
71
+ """
72
+ 「6:00~10:00」などの時刻範囲文字列を (pattern_id, custom_start, custom_end) に変換する。
73
+
74
+ - 既存パターンの start/end と完全一致 → (pattern_id, None, None)
75
+ - 一致なし → ("custom", start, end)
76
+ - 解析不能 → (None, None, None)
77
+
78
+ 対応フォーマット: HH:MM ~ HH:MM(半角・全角どちらも可)
79
+ """
80
+ import unicodedata
81
+ s = unicodedata.normalize('NFKC', time_str.strip())
82
+ m = re.match(r'^(\d{1,2}:\d{2})\s*[~〜~\-–—]+\s*(\d{1,2}:\d{2})$', s)
83
+ if not m:
84
+ return None, None, None
85
+
86
+ def _norm(t: str) -> str:
87
+ h, mn = t.split(':')
88
+ return f"{int(h):02d}:{mn}"
89
+
90
+ start = _norm(m.group(1))
91
+ end = _norm(m.group(2))
92
+
93
+ for p in ALL_PATTERNS:
94
+ if p.start and p.end:
95
+ if _norm(p.start) == start and _norm(p.end) == end:
96
+ return p.id, None, None
97
+
98
+ # 既存パターンに一致しない → カスタムとして時刻を保持
99
+ return "custom", start, end
100
+
101
+
102
  def _parse_custom_times(raw: str, period: SchedulePeriod) -> dict[str, tuple[str, str]]:
103
  """
104
  「4/1: 10:00〜16:00, 4/5: 11:00〜20:00」などを解析して
 
185
  date_cols: dict[str, dict[str, str]] = {}
186
  for h in headers:
187
  ds = _parse_date_from_header(h, period)
188
+ if not ds:
189
+ ds = _parse_numbered_shift_header(h, period) # 「希望シフト [N]」形式
190
  if not ds:
191
  continue
192
  if ds not in date_cols:
 
354
  pattern_id="custom", custom_start=custom_start, custom_end=custom_end, note=note,
355
  )
356
 
357
+ # ラベルで直接一致を試みる
358
+ if value in _LABEL_TO_ID:
359
+ return ShiftRequest(
360
+ employee_id=emp.id, date=date_str,
361
+ pattern_id=_LABEL_TO_ID[value], custom_start=None, custom_end=None, note=note,
362
  )
 
363
 
364
+ # 「HH:MM~HH:MM」形式の時刻範囲を試みる(例: 6:00~10:00)
365
+ if re.match(r'^\d{1,2}:\d{2}', value):
366
+ pid, cs, ce = _time_range_to_pattern(value)
367
+ if pid is not None:
368
+ return ShiftRequest(
369
+ employee_id=emp.id, date=date_str,
370
+ pattern_id=pid, custom_start=cs, custom_end=ce, note=note,
371
+ )
372
+
373
+ result.warnings.append(
374
+ f"「{name}」{date_str}: 選択値「{value}」が不明です(スキップ)"
375
  )
376
+ return None
377
 
378
 
379
  # Google Forms 設問テンプレート文字列