блоки 2 и 3 доработки интерфейса системы тестирования
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
"""Утилиты по цепочке теста (попытки/версии)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import TestAttempt, TestVersion
|
||||
|
||||
|
||||
def has_any_attempt_for_test(conn, test_id: str) -> bool:
|
||||
"""`conn` может быть Connection или Engine — обе поддерживают .execute()."""
|
||||
row = conn.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM test_attempts ta
|
||||
INNER JOIN test_versions tv ON ta.test_version_id = tv.id
|
||||
WHERE tv.test_id = :test_id
|
||||
) AS has_any
|
||||
"""
|
||||
),
|
||||
{'test_id': test_id},
|
||||
).first()
|
||||
return bool(row[0])
|
||||
def has_any_attempt_for_test(session: Session, test_id) -> bool:
|
||||
"""Возвращает True, если для теста есть хотя бы одна попытка."""
|
||||
import uuid as _uuid
|
||||
if not isinstance(test_id, _uuid.UUID):
|
||||
try:
|
||||
test_id = _uuid.UUID(str(test_id))
|
||||
except (ValueError, AttributeError):
|
||||
return False
|
||||
|
||||
return session.query(
|
||||
session.query(TestAttempt)
|
||||
.join(TestVersion, TestAttempt.test_version_id == TestVersion.id)
|
||||
.filter(TestVersion.test_id == test_id)
|
||||
.exists()
|
||||
).scalar()
|
||||
|
||||
Reference in New Issue
Block a user