Skip to content

Commit

Permalink
Fixup. Format code with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jul 17, 2024
1 parent 54b4b33 commit 0eebb34
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
6 changes: 5 additions & 1 deletion pod/playlist/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ def __init__(self, *args, **kwargs) -> None:
super(PlaylistForm, self).__init__(*args, **kwargs)
self.fields = add_placeholder_and_asterisk(self.fields)
if self.user:
if RESTRICT_PROMOTED_PLAYLIST_ACCESS_TO_STAFF_ONLY and "promoted" in self.fields or self.user.is_superuser:
if (
RESTRICT_PROMOTED_PLAYLIST_ACCESS_TO_STAFF_ONLY
and "promoted" in self.fields
or self.user.is_superuser
):
del self.fields["promoted"]
else:
if "promoted" in self.fields:
Expand Down
62 changes: 50 additions & 12 deletions pod/quiz/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from django.db import transaction

QUESTION_DATA = {
'single_choice': [],
'multiple_choice': [],
'short_answer': [],
"single_choice": [],
"multiple_choice": [],
"short_answer": [],
}

EXECUTE_QUIZ_MIGRATIONS = False
Expand Down Expand Up @@ -48,17 +48,34 @@ def execute_query_mapping(self, query, mapping_dict, question_type) -> None:
question_data = question_result[5]
if question_type in {"single_choice", "multiple_choice"}:
question_data = json.loads(question_data)
mapping_dict.append({
"question_type": question_type, "quiz": quiz, "title": question_result[1], "explanation": question_result[2], "start_timestamp": question_result[3], "end_timestamp": question_result[4], "question_data": question_data
})
mapping_dict.append(
{
"question_type": question_type,
"quiz": quiz,
"title": question_result[1],
"explanation": question_result[2],
"start_timestamp": question_result[3],
"end_timestamp": question_result[4],
"question_data": question_data,
}
)
except Exception as e:
print(e)
pass

def check_quiz_migrations(self, sender, **kwargs) -> None:
"""Save previous questions from different tables."""
from pod.quiz.models import MultipleChoiceQuestion, ShortAnswerQuestion, SingleChoiceQuestion
QUESTION_MODELS = [MultipleChoiceQuestion, ShortAnswerQuestion, SingleChoiceQuestion]
from pod.quiz.models import (
MultipleChoiceQuestion,
ShortAnswerQuestion,
SingleChoiceQuestion,
)

QUESTION_MODELS = [
MultipleChoiceQuestion,
ShortAnswerQuestion,
SingleChoiceQuestion,
]

global EXECUTE_QUIZ_MIGRATIONS
quiz_exist = self.check_quiz_exist()
Expand All @@ -81,6 +98,7 @@ def check_quiz_migrations(self, sender, **kwargs) -> None:
def check_quiz_exist(self) -> bool:
"""Check if quiz exist in database."""
from pod.quiz.models import Quiz

try:
quiz = Quiz.objects.first()
if not quiz:
Expand All @@ -91,7 +109,11 @@ def check_quiz_exist(self) -> bool:

def save_previous_questions(self, sender, **kwargs) -> None:
"""Save previous questions from different tables."""
from pod.quiz.models import MultipleChoiceQuestion, ShortAnswerQuestion, SingleChoiceQuestion
from pod.quiz.models import (
MultipleChoiceQuestion,
ShortAnswerQuestion,
SingleChoiceQuestion,
)

if not EXECUTE_QUIZ_MIGRATIONS:
return
Expand All @@ -106,12 +128,20 @@ def save_previous_questions(self, sender, **kwargs) -> None:

def remove_previous_questions(self, sender, **kwargs) -> None:
"""Remove previous questions from different tables."""
from pod.quiz.models import MultipleChoiceQuestion, ShortAnswerQuestion, SingleChoiceQuestion
from pod.quiz.models import (
MultipleChoiceQuestion,
ShortAnswerQuestion,
SingleChoiceQuestion,
)

if not EXECUTE_QUIZ_MIGRATIONS:
return

QUESTION_MODELS = [MultipleChoiceQuestion, ShortAnswerQuestion, SingleChoiceQuestion]
QUESTION_MODELS = [
MultipleChoiceQuestion,
ShortAnswerQuestion,
SingleChoiceQuestion,
]

for model in QUESTION_MODELS:
model.objects.all().delete()
Expand All @@ -128,5 +158,13 @@ def send_previous_questions(self, sender, **kwargs) -> None:
for question_type, questions in QUESTION_DATA.items():
for question in questions:
print(question["question_data"])
create_question(question_type=question_type, quiz=question["quiz"], title=question["title"], explanation=question["explanation"], start_timestamp=question["start_timestamp"], end_timestamp=question["end_timestamp"], question_data=question["question_data"])
create_question(
question_type=question_type,
quiz=question["quiz"],
title=question["title"],
explanation=question["explanation"],
start_timestamp=question["start_timestamp"],
end_timestamp=question["end_timestamp"],
question_data=question["question_data"],
)
print("--- New questions saved successfuly ---")
23 changes: 18 additions & 5 deletions pod/quiz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Question(models.Model):
start_timestamp (IntegerField): Start timestamp of the answer in the video.
end_timestamp (IntegerField): End timestamp of the answer in the video.
"""

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
quiz = models.ForeignKey(
Quiz,
Expand Down Expand Up @@ -163,7 +164,9 @@ def get_question_form(self, data=None):
Returns:
QuestionForm: Form for the question.
"""
raise NotImplementedError("Subclasses of Question must implement get_question_form method.")
raise NotImplementedError(
"Subclasses of Question must implement get_question_form method."
)

def get_answer(self) -> None:
"""
Expand All @@ -172,7 +175,9 @@ def get_answer(self) -> None:
Returns:
str: Answer for the question.
"""
raise NotImplementedError("Subclasses of Question must implement get_answer method.")
raise NotImplementedError(
"Subclasses of Question must implement get_answer method."
)

def get_type(self) -> None:
"""
Expand All @@ -181,7 +186,9 @@ def get_type(self) -> None:
Returns:
str: Type of the question.
"""
raise NotImplementedError("Subclasses of Question must implement get_type method.")
raise NotImplementedError(
"Subclasses of Question must implement get_type method."
)

def calculate_score(self, user_answer):
"""
Expand All @@ -204,7 +211,9 @@ def _calculate_score(self, user_answer, correct_answer):
"""
Internal method to be implemented by subclasses to calculate score.
"""
raise NotImplementedError("Subclasses of Question must implement _calculate_score method.")
raise NotImplementedError(
"Subclasses of Question must implement _calculate_score method."
)


class SingleChoiceQuestion(Question):
Expand Down Expand Up @@ -486,6 +495,10 @@ def _calculate_score(self, user_answer, correct_answer):
Returns:
float: The calculated score, a value between 0 and 1.
"""
if user_answer is not None and user_answer.strip() != "" and correct_answer is not None:
if (
user_answer is not None
and user_answer.strip() != ""
and correct_answer is not None
):
return 1.0 if user_answer.lower() == correct_answer.lower() else 0.0
return 0.0

0 comments on commit 0eebb34

Please sign in to comment.