refactor: improve language normalization and date handling utilities
All checks were successful
CI / lint-and-test (push) Successful in 21s
All checks were successful
CI / lint-and-test (push) Successful in 21s
- Introduced a new `normalize_lang` function to standardize language codes across the application, ensuring consistent handling of user language preferences. - Refactored date handling utilities by adding `parse_utc_iso` and `parse_utc_iso_naive` functions for better parsing of ISO 8601 date strings, enhancing timezone awareness. - Updated various modules to utilize the new language normalization and date parsing functions, improving code clarity and maintainability. - Enhanced error handling in date validation to raise specific `DateRangeValidationError` exceptions, providing clearer feedback on validation issues. - Improved test coverage for date range validation and language normalization functionalities, ensuring robustness and reliability.
This commit is contained in:
@@ -4,13 +4,15 @@ BOT_TOKEN is not validated on import; call require_bot_token() in the entry poin
|
||||
when running the bot.
|
||||
"""
|
||||
|
||||
import re
|
||||
import os
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from duty_teller.i18n.lang import normalize_lang
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Project root (parent of duty_teller package). Used for webapp path, etc.
|
||||
@@ -34,14 +36,6 @@ def normalize_phone(phone: str | None) -> str:
|
||||
return _PHONE_DIGITS_RE.sub("", phone.strip())
|
||||
|
||||
|
||||
def _normalize_default_language(value: str) -> str:
|
||||
"""Normalize DEFAULT_LANGUAGE from env to 'ru' or 'en'."""
|
||||
if not value:
|
||||
return "en"
|
||||
v = value.strip().lower()
|
||||
return "ru" if v.startswith("ru") else "en"
|
||||
|
||||
|
||||
def _parse_phone_list(raw: str) -> set[str]:
|
||||
"""Parse comma-separated phones into set of normalized (digits-only) strings."""
|
||||
result = set()
|
||||
@@ -113,9 +107,7 @@ class Settings:
|
||||
).strip(),
|
||||
duty_display_tz=os.getenv("DUTY_DISPLAY_TZ", "Europe/Moscow").strip()
|
||||
or "Europe/Moscow",
|
||||
default_language=_normalize_default_language(
|
||||
os.getenv("DEFAULT_LANGUAGE", "en").strip()
|
||||
),
|
||||
default_language=normalize_lang(os.getenv("DEFAULT_LANGUAGE", "en")),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user