fix(applications): accept VK domains without protocol
This commit is contained in:
1 parent
e5bd31b428
commit
12e9de9ba4
4 files changed
+25
-3
No files matched your search
@@ -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"]:
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
</div>
|
||||
<form id="form" class="panel"><div class="grid">
|
||||
<label>Имя персонажа<input name="name" required></label><label>ID персонажа<input name="character_id" type="number" min="1" required></label>
|
||||
<label id="vkField" class="wide">Ссылка на страницу VK <span class="hint">(необязательно)</span><input name="vk_link" type="url" placeholder="https://vk.com/id…"></label>
|
||||
<label id="vkField" class="wide">Ссылка на страницу VK <span class="hint">(необязательно)</span><input name="vk_link" type="text" inputmode="url" placeholder="vk.com/id… или vk.ru/id…"></label>
|
||||
<section id="kittenFields" class="wide hidden"><label>Скриншот активности<span class="upload"><strong>Перетащите изображение сюда или выберите файл</strong><span>PNG, JPG, JPEG до 15 МБ</span><input class="screenshots" type="file" accept="image/*" multiple></span></label><ul class="files"></ul></section>
|
||||
<section id="curatorFields" class="wide hidden"><label>Скриншот ЛУ с дробью<span class="upload"><strong>Прикрепите скриншот ЛУ с дробью</strong><span>Нужен хотя бы один файл</span><input class="screenshots" type="file" accept="image/*" multiple></span></label><ul class="files"></ul></section>
|
||||
<section id="flyingFields" class="wide hidden"><label>Сдавали практику ранее?<select name="practiced_before"><option value="no">Нет, не сдавал(-а)</option><option value="yes">Да, сдавал(-а)</option></select></label><label id="practiceLink" class="hidden">Ссылка на комментарий с прошлой практики<input name="practice_review_link" type="url" placeholder="https://catwar.su/... или https://vk.com/... "></label></section>
|
||||
|
||||
+1
-1
@@ -163,7 +163,7 @@
|
||||
<div class="grid">
|
||||
<label>Имя персонажа<input name="name" required></label>
|
||||
<label>ID персонажа<input name="character_id" type="number" min="1" inputmode="numeric" required></label>
|
||||
<label id="applicationVkField" class="wide hidden">Ссылка на страницу VK <span class="muted">(необязательно)</span><input name="vk_link" type="url" placeholder="https://vk.com/id…"></label>
|
||||
<label id="applicationVkField" class="wide hidden">Ссылка на страницу VK <span class="muted">(необязательно)</span><input name="vk_link" type="text" inputmode="url" placeholder="vk.com/id… или vk.ru/id…"></label>
|
||||
<section id="applicationKittenFields" class="wide hidden"><label>Скриншот активности<span class="upload"><strong>Выберите или перетащите изображение</strong><span>PNG, JPG или JPEG до 15 МБ</span><input class="application-screenshots" type="file" accept="image/png,image/jpeg" multiple></span></label><ul class="files"></ul></section>
|
||||
<section id="applicationCuratorFields" class="wide hidden"><label>Скриншот ЛУ с дробью<span class="upload"><strong>Прикрепите скриншот ЛУ с дробью</strong><span>Нужен хотя бы один файл</span><input class="application-screenshots" type="file" accept="image/png,image/jpeg" multiple></span></label><ul class="files"></ul></section>
|
||||
<section id="applicationFlyingFields" class="wide hidden"><label>Сдавали практику ранее?<select name="practiced_before"><option value="no">Нет, не сдавал(-а)</option><option value="yes">Да, сдавал(-а)</option></select></label><label id="applicationPracticeLink" class="hidden">Ссылка на комментарий с прошлой практики<input name="practice_review_link" type="url" placeholder="https://catwar.su/… или https://vk.com/…"></label></section>
|
||||
|
||||
Reference in new issue
Block a user