Fix CBT history react-markdown crash, fix test deletion cascade, fix cbt theme, add 180 NEET questions
This commit is contained in:
@@ -628,8 +628,6 @@ def create_manual_test(
|
||||
created_by=data.admin_id
|
||||
)
|
||||
session.add(test_obj)
|
||||
session.commit()
|
||||
session.refresh(test_obj)
|
||||
|
||||
# 2. Parse and create questions
|
||||
sections_dict = {}
|
||||
@@ -649,8 +647,6 @@ def create_manual_test(
|
||||
created_by=data.admin_id
|
||||
)
|
||||
session.add(q_obj)
|
||||
session.commit()
|
||||
session.refresh(q_obj)
|
||||
|
||||
sec_name = q_data.section
|
||||
if sec_name not in sections_dict:
|
||||
@@ -678,4 +674,69 @@ def toggle_test_status(test_id: UUID, session: Session = Depends(get_session)):
|
||||
session.add(test_obj)
|
||||
session.commit()
|
||||
|
||||
return {"status": "success", "is_active": test_obj.is_active}
|
||||
return {"status": "success", "is_active": test_obj.is_active}
|
||||
|
||||
@app.delete("/api/admin/tests/{test_id}", tags=["Admin"])
|
||||
def delete_test(test_id: UUID, session: Session = Depends(get_session)):
|
||||
from sqlmodel import select
|
||||
test_obj = session.get(models.Test, test_id)
|
||||
if not test_obj:
|
||||
raise HTTPException(status_code=404, detail="Test not found")
|
||||
|
||||
# 1. Identify all question IDs associated with the test
|
||||
question_ids = []
|
||||
if test_obj.sections:
|
||||
for section_name, section_data in test_obj.sections.items():
|
||||
question_ids.extend(section_data.get("questions", []))
|
||||
|
||||
# Convert question IDs to UUIDs
|
||||
import uuid
|
||||
q_uuids = []
|
||||
for q_id in question_ids:
|
||||
try:
|
||||
q_uuids.append(uuid.UUID(q_id) if isinstance(q_id, str) else q_id)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
# 2. Identify all attempts associated with this test
|
||||
attempts_query = select(models.Attempt).where(
|
||||
models.Attempt.source_id == test_id
|
||||
)
|
||||
attempts = session.exec(attempts_query).all()
|
||||
attempt_ids = [att.id for att in attempts]
|
||||
|
||||
# 3. Delete Mistake records referencing these attempts
|
||||
if attempt_ids:
|
||||
mistakes_by_attempt_query = select(models.Mistake).where(
|
||||
models.Mistake.attempt_id.in_(attempt_ids)
|
||||
)
|
||||
mistakes_by_attempt = session.exec(mistakes_by_attempt_query).all()
|
||||
for mistake in mistakes_by_attempt:
|
||||
session.delete(mistake)
|
||||
|
||||
# 4. Delete Mistake records referencing these questions
|
||||
if q_uuids:
|
||||
mistakes_by_q_query = select(models.Mistake).where(
|
||||
models.Mistake.question_id.in_(q_uuids)
|
||||
)
|
||||
mistakes_by_q = session.exec(mistakes_by_q_query).all()
|
||||
for mistake in mistakes_by_q:
|
||||
try:
|
||||
session.delete(mistake)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 5. Delete the Attempt records
|
||||
for att in attempts:
|
||||
session.delete(att)
|
||||
|
||||
# 6. Delete the Question records
|
||||
for q_uuid in q_uuids:
|
||||
q_obj = session.get(models.Question, q_uuid)
|
||||
if q_obj:
|
||||
session.delete(q_obj)
|
||||
|
||||
# 7. Delete the Test itself
|
||||
session.delete(test_obj)
|
||||
session.commit()
|
||||
return {"status": "success", "message": "Test and all associated questions, attempts, and mistakes deleted successfully"}
|
||||
Reference in New Issue
Block a user