Files
filwordspl-backend/routers/pronunciation.py
T
Drobysh b0308acb1f chore(lint): удалить неиспользуемые импорты (ruff F401)
- 11 файлов, 16 unused imports удалено
- pip 24.0 → 26.2.1, urllib3 2.7.0 → 2.8.0 (CVE fix)
- pip-audit: 0 vulnerabilities
- Логика не затронута, только imports
- Проверено: systemctl restart OK, /health 200
2026-10-05 11:16:24 +00:00

50 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from fastapi import APIRouter, Request, HTTPException
from auth import require_auth
from database import get_pool
router = APIRouter(prefix="/pronunciation", tags=["pronunciation"])
@router.get("/next")
async def get_next_word(request: Request):
"""Выдаёт случайное слово из всех уровней для раздела Произношение."""
telegram_id, _ = await require_auth(request)
pool = await get_pool()
async with pool.acquire() as conn:
user = await conn.fetchrow(
"SELECT app_language FROM users WHERE telegram_id = $1", telegram_id
)
if not user:
raise HTTPException(401, "User not registered")
lang = user["app_language"] or "pl"
# 🔧 ФИКС (30.09.2026): csv_words_source —
# источник слов переехал с level_words (старая схема level_id IS NULL)
# на csv_words. В csv_words нет колонки level — только level_code.
# Фронт ожидает поле "level", поэтому используем алиас level_code AS level.
# DISTINCT ON (word_pl) больше не нужен — в csv_words
# есть UNIQUE(level_code, word_pl), дубликатов по word_pl на одном
# уровне нет, но между уровнями могут быть. Поэтому слово может
# попасться несколько раз с разным level_code — оставляем как есть
# (для произношения уровень не критичен).
word = await conn.fetchrow("""
SELECT word_pl, translation_ru, translation_uk, translation_en,
level_code AS level
FROM csv_words
ORDER BY RANDOM()
LIMIT 1
""")
if not word:
raise HTTPException(404, "No words found")
translation_field = f"translation_{lang}"
translation = word.get(translation_field) or word.get("translation_en") or word["word_pl"]
return {
"word_pl": word["word_pl"],
"translation": translation,
"level": word["level"] or "A1",
}