145 lines
6.2 KiB
Python
145 lines
6.2 KiB
Python
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
from parsers import parser_sources
|
||
from repositories.project_settings_repository import update_app_setting, update_parser_source
|
||
|
||
|
||
def _field(
|
||
key: str,
|
||
label: str,
|
||
value: str = "",
|
||
*,
|
||
field_type: str = "text",
|
||
options: list[dict] | None = None,
|
||
help_text: str = "",
|
||
wide: bool = False,
|
||
) -> dict[str, Any]:
|
||
return {
|
||
"key": key,
|
||
"label": label,
|
||
"value": value or "",
|
||
"type": field_type,
|
||
"options": options or [],
|
||
"help": help_text,
|
||
"wide": wide,
|
||
}
|
||
|
||
|
||
def _source_field_key(source_key: str, field: str) -> str:
|
||
return f"{source_key}_{field}"
|
||
|
||
|
||
def build_project_settings_context() -> dict[str, Any]:
|
||
"""Данные для формы редактирования настроек проекта из БД."""
|
||
sources = parser_sources.list_parser_sources()
|
||
source_options = [
|
||
{"value": source["key"], "label": source.get("title") or source["key"]}
|
||
for source in sources
|
||
]
|
||
|
||
groups: list[dict[str, Any]] = [
|
||
{
|
||
"title": "Общие настройки проекта",
|
||
"subtitle": "Эти значения хранятся в базе данных, а не в .env.",
|
||
"fields": [
|
||
_field(
|
||
"DATA_EVENT_CODE",
|
||
"Активный источник по умолчанию",
|
||
parser_sources.get_default_source_key(),
|
||
field_type="select",
|
||
options=source_options,
|
||
help_text="Используется, если парсер запускается без выбора источника.",
|
||
),
|
||
_field(
|
||
"RFS_BASE_URL",
|
||
"Базовый URL RFS/WFL",
|
||
parser_sources.get_rfs_base_url(),
|
||
help_text="Обычно https://wfl.rfs.ru",
|
||
wide=True,
|
||
),
|
||
],
|
||
}
|
||
]
|
||
|
||
for source in sources:
|
||
source_key = source["key"]
|
||
title = source.get("title") or source_key
|
||
groups.append(
|
||
{
|
||
"title": title,
|
||
"subtitle": f"Ключ источника: {source_key}",
|
||
"fields": [
|
||
_field(_source_field_key(source_key, "EVENT_NAME"), "Название в интерфейсе", source.get("title", "")),
|
||
_field(_source_field_key(source_key, "SEASON"), "Сезон", source.get("season", "")),
|
||
_field(_source_field_key(source_key, "TOURNAMENT_ID"), "Tournament ID", source.get("tournament_id", "")),
|
||
_field(_source_field_key(source_key, "ROUND_ID"), "Round ID", source.get("round_id", "")),
|
||
_field(
|
||
_source_field_key(source_key, "CALENDAR_TYPE"),
|
||
"Тип календаря",
|
||
source.get("calendar_type", "tours"),
|
||
field_type="select",
|
||
options=[
|
||
{"value": "tours", "label": "tours — туры"},
|
||
{"value": "stages", "label": "stages — стадии"},
|
||
],
|
||
),
|
||
_field(_source_field_key(source_key, "LOGO_BASE_PATH"), "Папка логотипов", source.get("logo_base_path", ""), wide=True),
|
||
_field(_source_field_key(source_key, "PHOTO_BASE_PATH"), "Папка фотографий", source.get("photo_base_path", ""), wide=True),
|
||
_field(_source_field_key(source_key, "CHANNEL_LOGO_BASE_PATH"), "Папка логотипов каналов", source.get("channel_logo_base_path", ""), wide=True),
|
||
_field(_source_field_key(source_key, "TEAMS_URL"), "Ссылка на команды", source.get("teams_url", ""), wide=True),
|
||
_field(_source_field_key(source_key, "SCHEDULE_URL"), "Ссылка на расписание", source.get("schedule_url", ""), wide=True),
|
||
_field(_source_field_key(source_key, "STANDINGS_URL"), "Ссылка на турнирку", source.get("standings_url", ""), wide=True),
|
||
_field(_source_field_key(source_key, "MATCH_BASE_URL"), "Базовая ссылка матча", source.get("match_base_url", ""), wide=True),
|
||
],
|
||
}
|
||
)
|
||
|
||
editable_keys = [field["key"] for group in groups for field in group["fields"]]
|
||
return {
|
||
"storage_title": "База данных",
|
||
"groups": groups,
|
||
"editable_keys": editable_keys,
|
||
}
|
||
|
||
|
||
def save_project_settings(form_values: dict[str, str]) -> list[str]:
|
||
"""Сохраняет настройки проекта в БД."""
|
||
updated: list[str] = []
|
||
|
||
default_source_key = str(form_values.get("DATA_EVENT_CODE") or "SUPERLEAGUE").upper().strip()
|
||
rfs_base_url = str(form_values.get("RFS_BASE_URL") or "https://wfl.rfs.ru").strip().rstrip("/")
|
||
|
||
update_app_setting("default_parser_source_key", default_source_key)
|
||
update_app_setting("rfs_base_url", rfs_base_url)
|
||
updated.extend(["DATA_EVENT_CODE", "RFS_BASE_URL"])
|
||
|
||
sources = parser_sources.list_parser_sources()
|
||
field_map = {
|
||
"EVENT_NAME": "title",
|
||
"SEASON": "season",
|
||
"TOURNAMENT_ID": "tournament_id",
|
||
"ROUND_ID": "round_id",
|
||
"CALENDAR_TYPE": "calendar_type",
|
||
"LOGO_BASE_PATH": "logo_base_path",
|
||
"PHOTO_BASE_PATH": "photo_base_path",
|
||
"CHANNEL_LOGO_BASE_PATH": "channel_logo_base_path",
|
||
"TEAMS_URL": "teams_url",
|
||
"SCHEDULE_URL": "schedule_url",
|
||
"STANDINGS_URL": "standings_url",
|
||
"MATCH_BASE_URL": "match_base_url",
|
||
}
|
||
|
||
for source in sources:
|
||
source_key = source["key"]
|
||
values: dict[str, str] = {"base_url": rfs_base_url}
|
||
for form_field, db_field in field_map.items():
|
||
full_key = _source_field_key(source_key, form_field)
|
||
if full_key in form_values:
|
||
values[db_field] = str(form_values.get(full_key) or "").strip()
|
||
updated.append(full_key)
|
||
update_parser_source(source_key, values)
|
||
|
||
return updated
|