fix(domain): validate report calendar dates

This commit is contained in:
nyan404 committed 2026-08-25 04:01:20 +03:00
1 parent 7f16c12255
commit 1455b8cfbf
3 files changed
+22 -1

No files matched your search

+14 -1
View File
@@ -1,6 +1,7 @@
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from difflib import SequenceMatcher
from math import ceil, isfinite
import re
@@ -18,6 +19,7 @@ Skill = Literal["ЛУ", "ЗУ"]
Place = Literal["дупло", "расщелина"]
_CLOCK_RE = re.compile(r"^(?:[01]\d|2[0-3]):[0-5]\d$")
_DATE_RE = re.compile(r"^\d{1,2}\.\d{1,2}$")
# The goal calculator is public. These limits keep accidental and hostile
# values from turning a simple estimate into an unbounded exponentiation.
@@ -37,6 +39,17 @@ def parse_clock(value: str) -> int:
return int(hours) * 60 + int(minutes)
def parse_report_date(value: str) -> tuple[int, int]:
"""Validate a report date in ``ДД.ММ`` form without imposing a year."""
if not isinstance(value, str) or not _DATE_RE.fullmatch(value):
raise ValueError("Дата должна быть в формате ДД.ММ, например 15.08")
try:
parsed = datetime.strptime(f"2000.{value}", "%Y.%d.%m")
except ValueError as exc:
raise ValueError("Укажите существующую дату в формате ДД.ММ") from exc
return parsed.day, parsed.month
def format_clock(minutes: int) -> str:
"""Преобразует количество минут от начала суток обратно в строку 'HH:MM'."""
if not isinstance(minutes, int) or not 0 <= minutes < 24 * 60:
@@ -76,12 +89,12 @@ class CoachingSession:
"""Валидирует данные сессии при создании."""
if self.skill not in ("ЛУ", "ЗУ"):
raise ValueError("Навык должен быть ЛУ или ЗУ")
parse_report_date(self.date)
parse_clock(self.start)
parse_clock(self.end)
for value, label in (
(self.escort_name, "Имя сопровождающего"),
(self.kitten_name, "Имя котёнка"),
(self.date, "Дата"),
):
if not isinstance(value, str) or not value.strip():
raise ValueError(f"{label} не может быть пустым")