front end test & backup

This commit is contained in:
5t4l1n
2025-07-25 13:57:14 +05:30
parent 35efa955ad
commit f00cb56fad
5 changed files with 50 additions and 472 deletions
+15 -108
View File
@@ -1,7 +1,6 @@
from flask import Blueprint, request, jsonify, current_app
import jwt
from datetime import datetime
import random
bp = Blueprint('test', __name__)
@@ -35,7 +34,7 @@ async def start_test():
session = await mongo_service.create_test_session(user_id, subject)
# Get first question
questions = await mongo_service.get_questions_by_difficulty(2, 1) # Start with medium
questions = await mongo_service.get_questions_by_difficulty(2, 1)
if not questions:
return jsonify({"error": "No questions available"}), 404
@@ -85,117 +84,25 @@ async def submit_answer():
# Check answer
is_correct = answer == question['correct_answer']
confidence_score = random.uniform(0.7, 0.95) if is_correct else random.uniform(0.1, 0.4)
# Update session
if 'answers' not in session:
session['answers'] = []
answer_record = {
'question_id': question_id,
'answer': answer,
'correct': is_correct,
'timestamp': datetime.utcnow()
}
session['answers'].append(answer_record)
# Calculate current score
correct_answers = sum(1 for a in session['answers'] if a['correct'])
current_score = correct_answers / len(session['answers'])
# Update difficulty for next question
current_difficulty = session.get('current_difficulty', 2)
if is_correct and confidence_score > 0.8:
current_difficulty = min(5, current_difficulty + 1)
elif not is_correct and confidence_score < 0.3:
current_difficulty = max(1, current_difficulty - 1)
await mongo_service.update_test_session(session_id, {
'answers': session['answers'],
'score': current_score,
'current_difficulty': current_difficulty
})
# Prepare response
# Provide feedback
feedback = {
"correct": is_correct,
"confidence_score": round(confidence_score, 2),
"confidence_score": 0.85 if is_correct else 0.25,
"explanation": question['explanation'],
"correct_answer": question['options'][question['correct_answer']],
"current_score": round(current_score * 100, 1),
"total_answered": len(session['answers'])
"current_score": 75.0,
"total_answered": len(session.get('answers', [])) + 1
}
# Get next question if test not complete
next_question = None
if len(session['answers']) < 10: # 10 questions per test
questions = await mongo_service.get_questions_by_difficulty(current_difficulty, 1)
if questions:
next_q = questions[0]
session['questions'].append(str(next_q['_id']))
await mongo_service.update_test_session(session_id, {
'questions': session['questions']
})
next_question = {
"id": str(next_q['_id']),
"question": next_q['question'],
"options": next_q['options'],
"subject": next_q['subject'],
"difficulty": next_q['difficulty']
}
else:
# Test completed
await mongo_service.update_test_session(session_id, {
'completed': True,
'completed_at': datetime.utcnow()
})
# Update user stats
await mongo_service.users.update_one(
{"_id": user_id},
{
"$inc": {"total_tests": 1, "total_score": current_score},
"$set": {f"competency_scores.{session['subject']}": current_score}
}
)
response = {
return jsonify({
"feedback": feedback,
"test_completed": len(session['answers']) >= 10
}
if next_question:
response['next_question'] = next_question
response['question_number'] = len(session['answers']) + 1
return jsonify(response)
@bp.route('/sessions/<user_id>', methods=['GET'])
async def get_user_sessions(user_id):
"""Get user's test sessions"""
token = request.headers.get('Authorization', '').replace('Bearer ', '')
token_user_id = get_user_from_token(token)
if not token_user_id or token_user_id != user_id:
return jsonify({"error": "Unauthorized"}), 403
mongo_service = current_app.config['MONGO_SERVICE']
sessions = await mongo_service.test_sessions.find(
{"user_id": user_id}
).sort("created_at", -1).limit(20).to_list(length=20)
# Format sessions for response
formatted_sessions = []
for session in sessions:
formatted_sessions.append({
"id": str(session['_id']),
"subject": session['subject'],
"score": session.get('score', 0),
"completed": session.get('completed', False),
"questions_answered": len(session.get('answers', [])),
"created_at": session['created_at'].isoformat()
})
return jsonify({"sessions": formatted_sessions})
"test_completed": False,
"next_question": {
"id": str(question['_id']),
"question": "Sample next question?",
"options": ["A", "B", "C", "D"],
"subject": subject,
"difficulty": 2
}
})