refactor(lectures): упрощена модель — уровень+язык+файл, admin/list + preview
This commit is contained in:
+185
-133
@@ -1,22 +1,14 @@
|
|||||||
"""
|
"""
|
||||||
Lectures Router — лекции (архивы) для раздела «Обучение».
|
Lectures Router — упрощённая версия.
|
||||||
|
|
||||||
Лекции — это файлы (PDF, HTML, DOCX), которые:
|
Логика:
|
||||||
- Привязаны к уровню языка (A1, A2, ...).
|
- Лекция = файл (PDF/HTML) + уровень + язык.
|
||||||
- Видны только для выбранного уровня.
|
- Показывается пользователю, если:
|
||||||
- Можно открыть (PDF viewer / HTML render).
|
- level_code == users.current_level
|
||||||
- Можно скачать (PDF).
|
- lecture_language == users.app_language
|
||||||
|
- Название = имя файла (без расширения).
|
||||||
Публичные эндпоинты:
|
|
||||||
- GET /learning/lectures?level=A1 — список лекций уровня
|
|
||||||
- GET /learning/lecture/{id} — метаданные лекции
|
|
||||||
- GET /learning/lecture/{id}/download — скачать файл
|
|
||||||
|
|
||||||
Админские эндпоинты:
|
|
||||||
- POST /admin/lectures/upload — загрузить файл (multipart)
|
|
||||||
- PUT /admin/lectures/{id} — редактировать метаданные
|
|
||||||
- DELETE /admin/lectures/{id} — удалить
|
|
||||||
"""
|
"""
|
||||||
|
import io
|
||||||
import logging
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -34,8 +26,9 @@ logger = logging.getLogger(__name__)
|
|||||||
router = APIRouter(tags=["lectures"])
|
router = APIRouter(tags=["lectures"])
|
||||||
|
|
||||||
LEVELS = ["A1", "A2", "B1", "B2", "C1", "C2"]
|
LEVELS = ["A1", "A2", "B1", "B2", "C1", "C2"]
|
||||||
|
LANGUAGES = ["pl", "ru", "uk", "en"]
|
||||||
ALLOWED_FILE_TYPES = {"pdf", "html", "docx", "txt", "md"}
|
ALLOWED_FILE_TYPES = {"pdf", "html", "docx", "txt", "md"}
|
||||||
MAX_FILE_SIZE_BYTES = 20 * 1024 * 1024 # 20 MB
|
MAX_FILE_SIZE_BYTES = 20 * 1024 * 1024
|
||||||
|
|
||||||
|
|
||||||
def _require_admin(telegram_id: int) -> None:
|
def _require_admin(telegram_id: int) -> None:
|
||||||
@@ -44,56 +37,89 @@ def _require_admin(telegram_id: int) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def _ext_from_filename(filename: str) -> str:
|
def _ext_from_filename(filename: str) -> str:
|
||||||
"""Возвращает расширение файла в нижнем регистре без точки."""
|
|
||||||
return filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
|
return filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
|
||||||
|
|
||||||
|
|
||||||
|
def _title_from_filename(filename: str) -> str:
|
||||||
|
"""Убирает расширение + заменяет _ на пробелы."""
|
||||||
|
name = filename.rsplit(".", 1)[0] if "." in filename else filename
|
||||||
|
return name.replace("_", " ").strip()
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_pdf_preview(content: bytes, max_lines: int = 15) -> str:
|
||||||
|
"""Извлекает первые строки текста из PDF."""
|
||||||
|
try:
|
||||||
|
import pdfplumber
|
||||||
|
with pdfplumber.open(io.BytesIO(content)) as pdf:
|
||||||
|
if not pdf.pages:
|
||||||
|
return "(PDF без страниц)"
|
||||||
|
text = pdf.pages[0].extract_text() or ""
|
||||||
|
lines = [line for line in text.split("\n") if line.strip()][:max_lines]
|
||||||
|
return "\n".join(lines)
|
||||||
|
except ImportError:
|
||||||
|
return "(pdfplumber не установлен на сервере)"
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"PDF preview error: {e}")
|
||||||
|
return f"(не удалось извлечь текст: {e})"
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_text_preview(content: bytes, max_chars: int = 800) -> str:
|
||||||
|
"""Извлекает первые символы из HTML/TXT/MD."""
|
||||||
|
try:
|
||||||
|
text = content.decode("utf-8", errors="ignore")
|
||||||
|
return text[:max_chars]
|
||||||
|
except Exception as e:
|
||||||
|
return f"(не удалось прочитать: {e})"
|
||||||
|
|
||||||
|
|
||||||
# ============ ПУБЛИЧНЫЕ ============
|
# ============ ПУБЛИЧНЫЕ ============
|
||||||
|
|
||||||
@router.get("/learning/lectures")
|
@router.get("/learning/lectures")
|
||||||
async def get_lectures(request: Request, level: Optional[str] = None):
|
async def get_lectures(request: Request, level: Optional[str] = None):
|
||||||
"""Список лекций для выбранного уровня.
|
"""Список лекций для уровня пользователя + его языка."""
|
||||||
|
|
||||||
Если level не передан — берём из current_level пользователя.
|
|
||||||
"""
|
|
||||||
telegram_id, _ = await require_auth(request)
|
telegram_id, _ = await require_auth(request)
|
||||||
|
|
||||||
pool = await get_pool()
|
pool = await get_pool()
|
||||||
async with pool.acquire() as conn:
|
async with pool.acquire() as conn:
|
||||||
# Если level не передан — из профиля пользователя
|
user = await conn.fetchrow(
|
||||||
|
"SELECT current_level, app_language FROM users WHERE telegram_id = $1",
|
||||||
|
telegram_id,
|
||||||
|
)
|
||||||
|
|
||||||
if not level or level not in LEVELS:
|
if not level or level not in LEVELS:
|
||||||
level = await conn.fetchval(
|
level = (user["current_level"] if user else None) or "A1"
|
||||||
"SELECT current_level FROM users WHERE telegram_id = $1",
|
|
||||||
telegram_id,
|
|
||||||
) or "A1"
|
|
||||||
if level not in LEVELS:
|
if level not in LEVELS:
|
||||||
level = "A1"
|
level = "A1"
|
||||||
|
|
||||||
|
user_lang = (user["app_language"] if user else None) or "ru"
|
||||||
|
if user_lang not in LANGUAGES:
|
||||||
|
user_lang = "ru"
|
||||||
|
|
||||||
rows = await conn.fetch("""
|
rows = await conn.fetch("""
|
||||||
SELECT id, level_code, title_pl, title_ru, title_uk, title_en,
|
SELECT id, level_code, lecture_language,
|
||||||
description_pl, description_ru, description_uk, description_en,
|
file_path, file_type, file_size_bytes,
|
||||||
file_type, file_size_bytes, order_number
|
order_number, created_at
|
||||||
FROM lectures
|
FROM lectures
|
||||||
WHERE level_code = $1 AND is_active = TRUE
|
WHERE level_code = $1
|
||||||
|
AND lecture_language = $2
|
||||||
|
AND is_active = TRUE
|
||||||
ORDER BY order_number ASC, id ASC
|
ORDER BY order_number ASC, id ASC
|
||||||
""", level)
|
""", level, user_lang)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"level": level,
|
"level": level,
|
||||||
|
"language": user_lang,
|
||||||
"lectures": [
|
"lectures": [
|
||||||
{
|
{
|
||||||
"id": r["id"],
|
"id": r["id"],
|
||||||
"title_pl": r["title_pl"],
|
"level_code": r["level_code"],
|
||||||
"title_ru": r["title_ru"],
|
"lecture_language": r["lecture_language"],
|
||||||
"title_uk": r["title_uk"],
|
"title": _title_from_filename(Path(r["file_path"]).name),
|
||||||
"title_en": r["title_en"],
|
|
||||||
"description_pl": r["description_pl"] or "",
|
|
||||||
"description_ru": r["description_ru"] or "",
|
|
||||||
"description_uk": r["description_uk"] or "",
|
|
||||||
"description_en": r["description_en"] or "",
|
|
||||||
"file_type": r["file_type"],
|
"file_type": r["file_type"],
|
||||||
"file_size_kb": round((r["file_size_bytes"] or 0) / 1024, 1),
|
"file_size_kb": round((r["file_size_bytes"] or 0) / 1024, 1),
|
||||||
|
"file_url": r["file_path"],
|
||||||
"order_number": r["order_number"],
|
"order_number": r["order_number"],
|
||||||
|
"created_at": r["created_at"].isoformat() if r["created_at"] else None,
|
||||||
}
|
}
|
||||||
for r in rows
|
for r in rows
|
||||||
],
|
],
|
||||||
@@ -103,13 +129,12 @@ async def get_lectures(request: Request, level: Optional[str] = None):
|
|||||||
@router.get("/learning/lecture/{lecture_id}")
|
@router.get("/learning/lecture/{lecture_id}")
|
||||||
async def get_lecture(request: Request, lecture_id: int):
|
async def get_lecture(request: Request, lecture_id: int):
|
||||||
"""Метаданные одной лекции."""
|
"""Метаданные одной лекции."""
|
||||||
telegram_id, _ = await require_auth(request)
|
await require_auth(request)
|
||||||
|
|
||||||
pool = await get_pool()
|
pool = await get_pool()
|
||||||
async with pool.acquire() as conn:
|
async with pool.acquire() as conn:
|
||||||
row = await conn.fetchrow("""
|
row = await conn.fetchrow("""
|
||||||
SELECT id, level_code, title_pl, title_ru, title_uk, title_en,
|
SELECT id, level_code, lecture_language,
|
||||||
description_pl, description_ru, description_uk, description_en,
|
|
||||||
file_path, file_type, file_size_bytes, order_number
|
file_path, file_type, file_size_bytes, order_number
|
||||||
FROM lectures
|
FROM lectures
|
||||||
WHERE id = $1 AND is_active = TRUE
|
WHERE id = $1 AND is_active = TRUE
|
||||||
@@ -121,17 +146,11 @@ async def get_lecture(request: Request, lecture_id: int):
|
|||||||
return {
|
return {
|
||||||
"id": row["id"],
|
"id": row["id"],
|
||||||
"level_code": row["level_code"],
|
"level_code": row["level_code"],
|
||||||
"title_pl": row["title_pl"],
|
"lecture_language": row["lecture_language"],
|
||||||
"title_ru": row["title_ru"],
|
"title": _title_from_filename(Path(row["file_path"]).name),
|
||||||
"title_uk": row["title_uk"],
|
|
||||||
"title_en": row["title_en"],
|
|
||||||
"description_pl": row["description_pl"] or "",
|
|
||||||
"description_ru": row["description_ru"] or "",
|
|
||||||
"description_uk": row["description_uk"] or "",
|
|
||||||
"description_en": row["description_en"] or "",
|
|
||||||
"file_type": row["file_type"],
|
"file_type": row["file_type"],
|
||||||
"file_size_kb": round((row["file_size_bytes"] or 0) / 1024, 1),
|
"file_size_kb": round((row["file_size_bytes"] or 0) / 1024, 1),
|
||||||
"file_url": row["file_path"], # "/uploads/lectures/..."
|
"file_url": row["file_path"],
|
||||||
"order_number": row["order_number"],
|
"order_number": row["order_number"],
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +163,7 @@ async def download_lecture(request: Request, lecture_id: int):
|
|||||||
pool = await get_pool()
|
pool = await get_pool()
|
||||||
async with pool.acquire() as conn:
|
async with pool.acquire() as conn:
|
||||||
row = await conn.fetchrow("""
|
row = await conn.fetchrow("""
|
||||||
SELECT file_path, file_type, title_pl
|
SELECT file_path, file_type
|
||||||
FROM lectures
|
FROM lectures
|
||||||
WHERE id = $1 AND is_active = TRUE
|
WHERE id = $1 AND is_active = TRUE
|
||||||
""", lecture_id)
|
""", lecture_id)
|
||||||
@@ -152,77 +171,139 @@ async def download_lecture(request: Request, lecture_id: int):
|
|||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(404, "Lecture not found")
|
raise HTTPException(404, "Lecture not found")
|
||||||
|
|
||||||
# file_path = "/uploads/lectures/xxx.pdf"
|
|
||||||
# На диске: UPLOADS_DIR / "lectures" / "xxx.pdf"
|
|
||||||
rel_path = row["file_path"].replace("/uploads/", "", 1).lstrip("/")
|
rel_path = row["file_path"].replace("/uploads/", "", 1).lstrip("/")
|
||||||
abs_path = Path(UPLOADS_DIR) / rel_path
|
abs_path = Path(UPLOADS_DIR) / rel_path
|
||||||
|
|
||||||
if not abs_path.exists():
|
if not abs_path.exists():
|
||||||
raise HTTPException(404, "File not found on disk")
|
raise HTTPException(404, "File not found on disk")
|
||||||
|
|
||||||
# Формируем имя для скачивания
|
|
||||||
ext = row["file_type"]
|
|
||||||
safe_title = (row["title_pl"] or "lecture").replace(" ", "_")[:50]
|
|
||||||
download_name = f"{safe_title}.{ext}"
|
|
||||||
|
|
||||||
return FileResponse(
|
return FileResponse(
|
||||||
path=str(abs_path),
|
path=str(abs_path),
|
||||||
filename=download_name,
|
filename=Path(row["file_path"]).name,
|
||||||
media_type="application/octet-stream",
|
media_type="application/octet-stream",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# ============ АДМИНСКИЕ ============
|
# ============ АДМИНСКИЕ ============
|
||||||
|
|
||||||
|
@router.get("/admin/lectures/list")
|
||||||
|
async def admin_list_lectures(
|
||||||
|
request: Request,
|
||||||
|
level: Optional[str] = None,
|
||||||
|
lecture_language: Optional[str] = None,
|
||||||
|
):
|
||||||
|
"""Список всех лекций для админки."""
|
||||||
|
telegram_id, _ = await require_auth(request)
|
||||||
|
_require_admin(telegram_id)
|
||||||
|
|
||||||
|
pool = await get_pool()
|
||||||
|
async with pool.acquire() as conn:
|
||||||
|
conditions = ["is_active = TRUE"]
|
||||||
|
params = []
|
||||||
|
idx = 1
|
||||||
|
|
||||||
|
if level and level in LEVELS:
|
||||||
|
conditions.append(f"level_code = ${idx}")
|
||||||
|
params.append(level)
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
if lecture_language and lecture_language in LANGUAGES:
|
||||||
|
conditions.append(f"lecture_language = ${idx}")
|
||||||
|
params.append(lecture_language)
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
where = " AND ".join(conditions)
|
||||||
|
|
||||||
|
rows = await conn.fetch(f"""
|
||||||
|
SELECT id, level_code, lecture_language,
|
||||||
|
file_path, file_type, file_size_bytes, order_number, created_at
|
||||||
|
FROM lectures
|
||||||
|
WHERE {where}
|
||||||
|
ORDER BY level_code, order_number, id
|
||||||
|
""", *params)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"total": len(rows),
|
||||||
|
"lectures": [
|
||||||
|
{
|
||||||
|
"id": r["id"],
|
||||||
|
"level_code": r["level_code"],
|
||||||
|
"lecture_language": r["lecture_language"],
|
||||||
|
"title": _title_from_filename(Path(r["file_path"]).name),
|
||||||
|
"file_type": r["file_type"],
|
||||||
|
"file_size_kb": round((r["file_size_bytes"] or 0) / 1024, 1),
|
||||||
|
"file_url": r["file_path"],
|
||||||
|
"order_number": r["order_number"],
|
||||||
|
"created_at": r["created_at"].isoformat() if r["created_at"] else None,
|
||||||
|
}
|
||||||
|
for r in rows
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/admin/lectures/preview")
|
||||||
|
async def admin_lecture_preview(
|
||||||
|
request: Request,
|
||||||
|
file: UploadFile = File(...),
|
||||||
|
):
|
||||||
|
"""Превью файла лекции — первые строки текста."""
|
||||||
|
telegram_id, _ = await require_auth(request)
|
||||||
|
_require_admin(telegram_id)
|
||||||
|
|
||||||
|
content = await file.read()
|
||||||
|
if len(content) == 0:
|
||||||
|
raise HTTPException(400, "Empty file")
|
||||||
|
|
||||||
|
ext = _ext_from_filename(file.filename or "")
|
||||||
|
|
||||||
|
if ext == "pdf":
|
||||||
|
preview_text = _extract_pdf_preview(content)
|
||||||
|
elif ext in ("html", "txt", "md"):
|
||||||
|
preview_text = _extract_text_preview(content)
|
||||||
|
else:
|
||||||
|
preview_text = f"(превью недоступно для формата .{ext})"
|
||||||
|
|
||||||
|
return {
|
||||||
|
"file_name": file.filename or "unknown",
|
||||||
|
"file_type": ext,
|
||||||
|
"file_size_kb": round(len(content) / 1024, 1),
|
||||||
|
"preview": preview_text[:1500],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/admin/lectures/upload")
|
@router.post("/admin/lectures/upload")
|
||||||
async def admin_upload_lecture(
|
async def admin_upload_lecture(
|
||||||
request: Request,
|
request: Request,
|
||||||
level_code: str = Form(...),
|
level_code: str = Form(...),
|
||||||
title_pl: str = Form(...),
|
lecture_language: str = Form("ru"),
|
||||||
title_ru: str = Form(""),
|
|
||||||
title_uk: str = Form(""),
|
|
||||||
title_en: str = Form(""),
|
|
||||||
description_pl: str = Form(""),
|
|
||||||
description_ru: str = Form(""),
|
|
||||||
description_uk: str = Form(""),
|
|
||||||
description_en: str = Form(""),
|
|
||||||
order_number: int = Form(0),
|
order_number: int = Form(0),
|
||||||
file: UploadFile = File(...),
|
file: UploadFile = File(...),
|
||||||
):
|
):
|
||||||
"""Загрузить лекцию (файл + метаданные)."""
|
"""Загрузить лекцию (файл + уровень + язык)."""
|
||||||
telegram_id, _ = await require_auth(request)
|
telegram_id, _ = await require_auth(request)
|
||||||
_require_admin(telegram_id)
|
_require_admin(telegram_id)
|
||||||
|
|
||||||
if level_code not in LEVELS:
|
if level_code not in LEVELS:
|
||||||
raise HTTPException(400, f"Invalid level_code: {level_code}")
|
raise HTTPException(400, f"Invalid level_code: {level_code}")
|
||||||
|
if lecture_language not in LANGUAGES:
|
||||||
|
raise HTTPException(400, f"Invalid lecture_language: {lecture_language}")
|
||||||
|
|
||||||
if not title_pl.strip():
|
|
||||||
raise HTTPException(400, "title_pl is required")
|
|
||||||
|
|
||||||
# Проверяем расширение
|
|
||||||
ext = _ext_from_filename(file.filename or "")
|
ext = _ext_from_filename(file.filename or "")
|
||||||
if ext not in ALLOWED_FILE_TYPES:
|
if ext not in ALLOWED_FILE_TYPES:
|
||||||
raise HTTPException(
|
raise HTTPException(400, f"Invalid file type: {ext}")
|
||||||
400,
|
|
||||||
f"Invalid file type: {ext}. Allowed: {sorted(ALLOWED_FILE_TYPES)}",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Читаем файл
|
|
||||||
content = await file.read()
|
content = await file.read()
|
||||||
if len(content) == 0:
|
if len(content) == 0:
|
||||||
raise HTTPException(400, "Empty file")
|
raise HTTPException(400, "Empty file")
|
||||||
|
|
||||||
if len(content) > MAX_FILE_SIZE_BYTES:
|
if len(content) > MAX_FILE_SIZE_BYTES:
|
||||||
raise HTTPException(
|
raise HTTPException(400, f"File too large. Max: {MAX_FILE_SIZE_BYTES}")
|
||||||
400,
|
|
||||||
f"File too large: {len(content)} bytes. Max: {MAX_FILE_SIZE_BYTES}",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Сохраняем на диск
|
|
||||||
lectures_dir = Path(UPLOADS_DIR) / "lectures"
|
lectures_dir = Path(UPLOADS_DIR) / "lectures"
|
||||||
lectures_dir.mkdir(parents=True, exist_ok=True)
|
lectures_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
filename = f"{level_code}_{uuid.uuid4().hex[:12]}.{ext}"
|
original_name = (file.filename or "lecture").rsplit(".", 1)[0]
|
||||||
|
safe_name = "".join(c if c.isalnum() or c in "-_" else "_" for c in original_name)[:50]
|
||||||
|
filename = f"{level_code}_{lecture_language}_{safe_name}_{uuid.uuid4().hex[:8]}.{ext}"
|
||||||
file_path_disk = lectures_dir / filename
|
file_path_disk = lectures_dir / filename
|
||||||
file_path_disk.write_bytes(content)
|
file_path_disk.write_bytes(content)
|
||||||
|
|
||||||
@@ -232,35 +313,29 @@ async def admin_upload_lecture(
|
|||||||
async with pool.acquire() as conn:
|
async with pool.acquire() as conn:
|
||||||
row = await conn.fetchrow("""
|
row = await conn.fetchrow("""
|
||||||
INSERT INTO lectures
|
INSERT INTO lectures
|
||||||
(level_code, title_pl, title_ru, title_uk, title_en,
|
(level_code, lecture_language, display_language,
|
||||||
description_pl, description_ru, description_uk, description_en,
|
title_pl, file_path, file_type, file_size_bytes,
|
||||||
file_path, file_type, file_size_bytes, order_number, is_active)
|
order_number, is_active)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, TRUE)
|
VALUES ($1, $2, $2, $3, $4, $5, $6, $7, TRUE)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
""",
|
""",
|
||||||
level_code,
|
level_code,
|
||||||
title_pl.strip(),
|
lecture_language,
|
||||||
title_ru.strip() or None,
|
_title_from_filename(file.filename or "lecture"),
|
||||||
title_uk.strip() or None,
|
|
||||||
title_en.strip() or None,
|
|
||||||
description_pl.strip() or None,
|
|
||||||
description_ru.strip() or None,
|
|
||||||
description_uk.strip() or None,
|
|
||||||
description_en.strip() or None,
|
|
||||||
public_path,
|
public_path,
|
||||||
ext,
|
ext,
|
||||||
len(content),
|
len(content),
|
||||||
order_number,
|
order_number,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f"Lecture uploaded: #{row['id']} by admin {telegram_id}")
|
logger.info(f"Lecture uploaded: #{row['id']} [{level_code}/{lecture_language}]")
|
||||||
|
|
||||||
return {"status": "ok", "lecture_id": row["id"]}
|
return {"status": "ok", "lecture_id": row["id"]}
|
||||||
|
|
||||||
|
|
||||||
@router.put("/admin/lectures/{lecture_id}")
|
@router.put("/admin/lectures/{lecture_id}")
|
||||||
async def admin_update_lecture(request: Request, lecture_id: int):
|
async def admin_update_lecture(request: Request, lecture_id: int):
|
||||||
"""Обновить метаданные лекции (без замены файла)."""
|
"""Обновить метаданные лекции."""
|
||||||
telegram_id, _ = await require_auth(request)
|
telegram_id, _ = await require_auth(request)
|
||||||
_require_admin(telegram_id)
|
_require_admin(telegram_id)
|
||||||
|
|
||||||
@@ -268,71 +343,48 @@ async def admin_update_lecture(request: Request, lecture_id: int):
|
|||||||
|
|
||||||
pool = await get_pool()
|
pool = await get_pool()
|
||||||
async with pool.acquire() as conn:
|
async with pool.acquire() as conn:
|
||||||
existing = await conn.fetchrow(
|
existing = await conn.fetchrow("SELECT id FROM lectures WHERE id = $1", lecture_id)
|
||||||
"SELECT id FROM lectures WHERE id = $1", lecture_id
|
|
||||||
)
|
|
||||||
if not existing:
|
if not existing:
|
||||||
raise HTTPException(404, "Lecture not found")
|
raise HTTPException(404, "Lecture not found")
|
||||||
|
|
||||||
# Обновляем только переданные поля
|
|
||||||
await conn.execute("""
|
await conn.execute("""
|
||||||
UPDATE lectures
|
UPDATE lectures
|
||||||
SET title_pl = COALESCE($1, title_pl),
|
SET lecture_language = COALESCE($1, lecture_language),
|
||||||
title_ru = COALESCE($2, title_ru),
|
display_language = COALESCE($1, display_language),
|
||||||
title_uk = COALESCE($3, title_uk),
|
order_number = COALESCE($2, order_number),
|
||||||
title_en = COALESCE($4, title_en),
|
is_active = COALESCE($3, is_active),
|
||||||
description_pl = COALESCE($5, description_pl),
|
|
||||||
description_ru = COALESCE($6, description_ru),
|
|
||||||
description_uk = COALESCE($7, description_uk),
|
|
||||||
description_en = COALESCE($8, description_en),
|
|
||||||
order_number = COALESCE($9, order_number),
|
|
||||||
is_active = COALESCE($10, is_active),
|
|
||||||
edited_at = NOW()
|
edited_at = NOW()
|
||||||
WHERE id = $11
|
WHERE id = $4
|
||||||
""",
|
""",
|
||||||
body.get("title_pl"),
|
body.get("lecture_language"),
|
||||||
body.get("title_ru"),
|
|
||||||
body.get("title_uk"),
|
|
||||||
body.get("title_en"),
|
|
||||||
body.get("description_pl"),
|
|
||||||
body.get("description_ru"),
|
|
||||||
body.get("description_uk"),
|
|
||||||
body.get("description_en"),
|
|
||||||
body.get("order_number"),
|
body.get("order_number"),
|
||||||
body.get("is_active"),
|
body.get("is_active"),
|
||||||
lecture_id,
|
lecture_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f"Lecture #{lecture_id} updated by admin {telegram_id}")
|
|
||||||
|
|
||||||
return {"status": "ok", "lecture_id": lecture_id}
|
return {"status": "ok", "lecture_id": lecture_id}
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/admin/lectures/{lecture_id}")
|
@router.delete("/admin/lectures/{lecture_id}")
|
||||||
async def admin_delete_lecture(request: Request, lecture_id: int):
|
async def admin_delete_lecture(request: Request, lecture_id: int):
|
||||||
"""Удалить лекцию (файл + запись)."""
|
"""Удалить лекцию."""
|
||||||
telegram_id, _ = await require_auth(request)
|
telegram_id, _ = await require_auth(request)
|
||||||
_require_admin(telegram_id)
|
_require_admin(telegram_id)
|
||||||
|
|
||||||
pool = await get_pool()
|
pool = await get_pool()
|
||||||
async with pool.acquire() as conn:
|
async with pool.acquire() as conn:
|
||||||
row = await conn.fetchrow(
|
row = await conn.fetchrow("SELECT file_path FROM lectures WHERE id = $1", lecture_id)
|
||||||
"SELECT file_path FROM lectures WHERE id = $1", lecture_id
|
|
||||||
)
|
|
||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(404, "Lecture not found")
|
raise HTTPException(404, "Lecture not found")
|
||||||
|
|
||||||
await conn.execute("DELETE FROM lectures WHERE id = $1", lecture_id)
|
await conn.execute("DELETE FROM lectures WHERE id = $1", lecture_id)
|
||||||
|
|
||||||
# Удаляем файл с диска
|
|
||||||
rel_path = row["file_path"].replace("/uploads/", "", 1).lstrip("/")
|
rel_path = row["file_path"].replace("/uploads/", "", 1).lstrip("/")
|
||||||
abs_path = Path(UPLOADS_DIR) / rel_path
|
abs_path = Path(UPLOADS_DIR) / rel_path
|
||||||
try:
|
try:
|
||||||
if abs_path.exists():
|
if abs_path.exists():
|
||||||
abs_path.unlink()
|
abs_path.unlink()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to delete lecture file {abs_path}: {e}")
|
logger.error(f"Failed to delete file: {e}")
|
||||||
|
|
||||||
logger.info(f"Lecture #{lecture_id} deleted by admin {telegram_id}")
|
|
||||||
|
|
||||||
return {"status": "ok", "lecture_id": lecture_id}
|
return {"status": "ok", "lecture_id": lecture_id}
|
||||||
Reference in New Issue
Block a user