Spaces:
Sleeping
Sleeping
File size: 4,769 Bytes
006d8f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | from django.db import models
import json
from django.db import models
import json
from authService.models import User
from classroomService.models import Classroom
class Test(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tests', null=True, blank=True)
title = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
subject = models.CharField(max_length=100, blank=True, null=True)
class_level = models.CharField(max_length=50, blank=True, null=True)
test_type = models.CharField(max_length=50, default='Written')
items = models.PositiveIntegerField(default=0)
assigned = models.PositiveIntegerField(default=0)
completed = models.PositiveIntegerField(default=0)
status = models.CharField(max_length=50, default='draft')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
class Meta:
ordering = ['-created_at']
class Question(models.Model):
QUESTION_TYPES = [
('mcq', 'Multiple Choice'),
('true_false', 'True/False'),
('short', 'Short Answer'),
('long', 'Long Answer'),
]
DIFFICULTY_LEVELS = [
('easy', 'Easy'),
('medium', 'Medium'),
('hard', 'Hard'),
]
test = models.ForeignKey(Test, related_name='questions', on_delete=models.CASCADE)
stem = models.TextField() # The question text
question_type = models.CharField(max_length=20, choices=QUESTION_TYPES)
difficulty = models.CharField(max_length=10, choices=DIFFICULTY_LEVELS)
answer = models.TextField(blank=True, null=True) # For short/long answer types
rationale = models.TextField(blank=True, null=True)
order = models.PositiveIntegerField(default=0)
def __str__(self):
return f"{self.test.title} - Question {self.order + 1}"
class Meta:
ordering = ['order']
class Choice(models.Model):
question = models.ForeignKey(Question, related_name='choices', on_delete=models.CASCADE)
content = models.TextField()
is_correct = models.BooleanField(default=False)
order = models.PositiveIntegerField(default=0)
def __str__(self):
return f"{self.question.stem[:50]}... - {self.content[:30]}..."
class Meta:
ordering = ['order']
class TestAssignment(models.Model):
STATUS_CHOICES = [
('pending', 'Pending'),
('in_progress', 'In Progress'),
('completed', 'Completed'),
('expired', 'Expired'),
]
test = models.ForeignKey(Test, on_delete=models.CASCADE, related_name='assignments')
student = models.ForeignKey(User, on_delete=models.CASCADE, related_name='test_assignments')
classroom = models.ForeignKey(Classroom, on_delete=models.CASCADE, related_name='test_assignments', null=True, blank=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_assignments')
start_date = models.DateTimeField()
end_date = models.DateTimeField()
duration = models.PositiveIntegerField(help_text="Duration in minutes")
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
started_at = models.DateTimeField(null=True, blank=True)
completed_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.test.title} assigned to {self.student.email}"
class Meta:
ordering = ['-created_at']
class TestResult(models.Model):
assignment = models.OneToOneField(TestAssignment, on_delete=models.CASCADE, related_name='result')
student = models.ForeignKey(User, on_delete=models.CASCADE, related_name='test_results')
test = models.ForeignKey(Test, on_delete=models.CASCADE, related_name='results')
score = models.FloatField(help_text="Score out of 100")
total_questions = models.PositiveIntegerField()
correct_answers = models.PositiveIntegerField()
time_taken = models.PositiveIntegerField(help_text="Time taken in minutes")
strengths = models.JSONField(default=list, help_text="List of strengths")
weaknesses = models.JSONField(default=list, help_text="List of weaknesses")
suggestions = models.JSONField(default=list, help_text="List of suggestions")
answers = models.JSONField(default=dict, help_text="Student's answers")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.student.email} - {self.test.title} - {self.score}%"
class Meta:
ordering = ['-created_at']
|