From 12e9de9ba45f0f03e196ebbd597fdd59cf7f151c Mon Sep 17 00:00:00 2001 From: Nyan404 Date: Fri, 21 Aug 2026 14:22:48 +0300 Subject: [PATCH] fix(applications): accept VK domains without protocol --- app/belki/applications.py | 14 +++++++++++++- tests/test_applications.py | 10 ++++++++++ web/applications.html | 2 +- web/calculator.html | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/belki/applications.py b/app/belki/applications.py index 4288af1..c9a905d 100644 --- a/app/belki/applications.py +++ b/app/belki/applications.py @@ -7,6 +7,7 @@ import os import sqlite3 from pathlib import Path from typing import Any +from urllib.parse import urlparse APPLICATION_TYPES = { @@ -31,6 +32,17 @@ def _positive_id(value: Any, label: str) -> int: return result +def _normalise_vk_link(value: Any) -> str: + """Сохраняет VK-ссылку с HTTPS, но не заставляет пользователя вводить протокол.""" + link = str(value or "").strip() + if not link: + return "" + parsed = urlparse(link) + if not parsed.scheme and parsed.path.split("/", 1)[0].lower() in {"vk.com", "www.vk.com", "vk.ru", "www.vk.ru"}: + return f"https://{link}" + return link + + def validate_application(payload: dict[str, Any]) -> dict[str, Any]: """Возвращает очищенную заявку и проверяет обязательные поля её типа.""" if not isinstance(payload, dict): @@ -46,7 +58,7 @@ def validate_application(payload: dict[str, Any]) -> dict[str, Any]: "name": name, "character_id": _positive_id(payload.get("character_id"), "ID персонажа"), "attachments": [item for item in payload.get("attachments", []) if isinstance(item, dict)], - "vk_link": str(payload.get("vk_link", "")).strip(), + "vk_link": _normalise_vk_link(payload.get("vk_link")), } if application_type == "kitten": if not result["attachments"]: diff --git a/tests/test_applications.py b/tests/test_applications.py index 20ab26e..62c25fa 100644 --- a/tests/test_applications.py +++ b/tests/test_applications.py @@ -17,6 +17,16 @@ class ApplicationTests(unittest.TestCase): with self.assertRaisesRegex(ApplicationError, "прошлой практики"): validate_application({"type": "flying_squirrel", "name": "Муся", "character_id": 1337, "practiced_before": True}) + def test_vk_link_accepts_a_domain_without_protocol(self) -> None: + for link in ("vk.com", "vk.ru", "https://vk.com", "https://vk.ru"): + with self.subTest(link=link): + application = validate_application({ + "type": "flying_squirrel", "name": "Муся", "character_id": 1337, + "practiced_before": False, "vk_link": link, + }) + expected = link if link.startswith("https://") else f"https://{link}" + self.assertEqual(application["vk_link"], expected) + def test_feather_donation_is_saved_and_accepted(self) -> None: with tempfile.TemporaryDirectory() as directory: store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3") diff --git a/web/applications.html b/web/applications.html index e1bbd43..70ac5be 100644 --- a/web/applications.html +++ b/web/applications.html @@ -27,7 +27,7 @@
- + diff --git a/web/calculator.html b/web/calculator.html index 60ae1ab..b5becbe 100644 --- a/web/calculator.html +++ b/web/calculator.html @@ -163,7 +163,7 @@
- +