Skip to content

Commit

Permalink
Merge pull request #96 from codersforcauses/optimize-the-logic-of-att…
Browse files Browse the repository at this point in the history
…emping-a-comp

Optimize the logic of attemping a comp
  • Loading branch information
yunho7687 authored Jan 29, 2025
2 parents 2d0ad6b + edde25a commit 1cf66d0
Show file tree
Hide file tree
Showing 12 changed files with 506 additions and 86 deletions.
2 changes: 1 addition & 1 deletion server/api/question/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ def update(self, instance, validated_data):

class Meta:
model = Question
fields = "__all__"
fields = '__all__'
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.1.5 on 2025-01-27 04:05

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("quiz", "0009_quizattempt_team"),
("team", "0003_alter_teammember_student_alter_teammember_team"),
]

operations = [
migrations.AlterField(
model_name="quizattempt",
name="team",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="quiz_attempts",
to="team.team",
),
),
migrations.AlterField(
model_name="quizattempt",
name="time_finish",
field=models.DateTimeField(blank=True, null=True),
),
]
27 changes: 27 additions & 0 deletions server/api/quiz/migrations/0011_alter_quizattempt_student.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.1.5 on 2025-01-27 14:22

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("quiz", "0010_alter_quizattempt_team_alter_quizattempt_time_finish"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name="quizattempt",
name="student",
field=models.ForeignKey(
default=None,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="quiz_attempts",
to=settings.AUTH_USER_MODEL,
),
),
]
26 changes: 26 additions & 0 deletions server/api/quiz/migrations/0012_alter_quizattempt_student.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.1.5 on 2025-01-27 14:27

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("quiz", "0011_alter_quizattempt_student"),
("users", "0002_school_is_country_school_type_student_attendent_year"),
]

operations = [
migrations.AlterField(
model_name="quizattempt",
name="student",
field=models.ForeignKey(
default=None,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="quiz_attempts",
to="users.student",
),
),
]
18 changes: 18 additions & 0 deletions server/api/quiz/migrations/0013_quizattempt_dead_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.5 on 2025-01-28 08:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("quiz", "0012_alter_quizattempt_student"),
]

operations = [
migrations.AddField(
model_name="quizattempt",
name="dead_line",
field=models.DateTimeField(blank=True, default=None, null=True),
),
]
41 changes: 37 additions & 4 deletions server/api/quiz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from api.team.models import Team
from api.users.models import Student
from api.question.models import Question
from django.utils.timezone import now
from datetime import timedelta


class Quiz(models.Model):
Expand Down Expand Up @@ -87,14 +89,16 @@ class State(models.IntegerChoices):
Student, on_delete=models.CASCADE, related_name="quiz_attempts", default=None, null=True
)
current_page = models.IntegerField()
state = models.IntegerField(choices=State.choices, default=State.UNATTEMPTED)
state = models.IntegerField(
choices=State.choices, default=State.UNATTEMPTED)
time_start = models.DateTimeField(auto_now_add=True)
time_finish = models.DateTimeField(null=True)
time_finish = models.DateTimeField(null=True, blank=True)
time_modified = models.DateTimeField(auto_now=True)
total_marks = models.IntegerField()
team = models.ForeignKey(
Team, on_delete=models.CASCADE, related_name="quiz_attempts", default=None, null=True
Team, on_delete=models.CASCADE, related_name="quiz_attempts", default=None, null=True, blank=True
)
dead_line = models.DateTimeField(default=None, null=True, blank=True)

def __str__(self):
return f"{self.id} {self.quiz} "
Expand All @@ -104,6 +108,35 @@ def check_all_answer(self):
question_attempt.check_answer()
question_attempt.save()

@property
def is_available(self):
current_time = now()
end_time = self.quiz.open_time_date + \
timedelta(minutes=self.quiz.time_limit) + \
timedelta(minutes=self.quiz.time_window)
end_time = min(end_time, self.time_start +
timedelta(minutes=self.quiz.time_limit))
if int(self.student.extenstion_time) > 0:
print("extenstion time")
end_time = now() + \
timedelta(minutes=self.student.extenstion_time)
self.student.extenstion_time = 0
self.student.save()
if self.dead_line is None:
self.dead_line = end_time
self.save()
else:
self.dead_line = max(self.dead_line, end_time)

is_available = self.quiz.open_time_date <= current_time <= self.dead_line
if not is_available:
self.state = QuizAttempt.State.COMPLETED
self.save()
else:
self.state = QuizAttempt.State.IN_PROGRESS
self.save()
return is_available


class QuestionAttempt(models.Model):
id = models.AutoField(primary_key=True)
Expand All @@ -117,7 +150,7 @@ class QuestionAttempt(models.Model):
is_correct = models.BooleanField(default=None)

def __str__(self):
return f"{self.id} {self.question_id} {self.quiz_attempt_id}"
return f"{self.id} {self.question} {self.quiz_attempt}"

def check_answer(self):
if self.answer_student == self.question.answer:
Expand Down
31 changes: 30 additions & 1 deletion server/api/quiz/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ class Meta:
fields = '__all__'


class CompQuestionSerializer(serializers.ModelSerializer):
"""
Serializer for the Question model with no answer field.
"""
class Meta:
model = Question
fields = ['id', 'name', 'question_text', 'layout', 'image', 'mark']


class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
Expand Down Expand Up @@ -39,11 +48,28 @@ class Meta:
read_only_fields = ['quiz']


class CompQuizSlotSerializer(serializers.ModelSerializer):
question = CompQuestionSerializer(read_only=True)

class Meta:
model = QuizSlot
fields = '__all__'


class QuizSerializer(serializers.ModelSerializer):

class Meta:
model = Quiz
fields = ['id', 'name', 'intro', 'total_marks']
fields = '__all__'


class UserQuizSerializer(serializers.ModelSerializer):
"""
Serializer for the Quiz model with no is_comp, visible, and status fields.
"""
class Meta:
model = Quiz
exclude = ['is_comp', 'visible', 'status']


class AdminQuizSerializer(serializers.ModelSerializer):
Expand All @@ -53,6 +79,9 @@ class Meta:


class QuizAttemptSerializer(serializers.ModelSerializer):
current_page = serializers.IntegerField(default=0, required=False)
total_marks = serializers.IntegerField(default=0, required=False)

class Meta:
model = QuizAttempt
fields = '__all__'
Expand Down
Loading

0 comments on commit 1cf66d0

Please sign in to comment.