Skip to content

Commit

Permalink
support v2 and v1 for remaining functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ozer550 committed Jan 5, 2024
1 parent 774fb4c commit ffd1352
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
21 changes: 13 additions & 8 deletions kolibri/core/exams/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ def exam_assignment_lookup(question_sources):
:return: a tuple of contentnode_id and metadata
"""
for question_source in question_sources:
questions = question_source.get("questions")
if questions is not None:
for question in question_source["questions"]:
yield (question["exercise_id"], None)
if "section_id" in question_source:
questions = question_source.get("questions")
if questions is not None:
for question in question_source["questions"]:
yield (question["exercise_id"], None)
else:
yield (question_source["exercise_id"], None)


class Exam(AbstractFacilityDataModel):
Expand Down Expand Up @@ -232,11 +235,13 @@ def get_questions(self):
Returns a list of all questions from all sections in the exam.
"""
questions = []

for section in self.question_sources:
for question in section.get("questions", []):
if self.data_model_version in {2, 1}:
for question in self.question_sources:
questions.append(question)

if self.data_model_version == 3:
for section in self.question_sources:
for question in section.get("questions", []):
questions.append(question)
return questions


Expand Down
6 changes: 2 additions & 4 deletions kolibri/core/exams/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,11 @@ def validate(self, attrs):
def to_representation(self, instance):
data = super().to_representation(instance)
if "question_sources" in data and data["question_sources"]:
first_question_source = data["question_sources"][0]
# Version 3 strictly requires section_id
if "section_id" in first_question_source:
if data["data_model_version"] == 3:
data["question_sources"] = QuizSectionSerializer(
instance.question_sources, many=True
).data
if "exercise_id" in first_question_source:
if data["data_model_version"] in {1, 2}:
data["question_sources"] = QuestionSourceSerializer(
instance.question_sources, many=True
).data
Expand Down
19 changes: 14 additions & 5 deletions kolibri/plugins/coach/class_summary_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,20 @@ def serialize_lessons(queryset):

def _map_exam(item):
item["assignments"] = item.pop("exam_assignments")
item["node_ids"] = [
question["exercise_id"]
for question_source in item.get("question_sources", [])
for question in question_source.get("questions", [])
]
data_model_version = item.pop("data_model_version")
if data_model_version == 3:
item["node_ids"] = [
question["exercise_id"]
for question_source in item.get("question_sources", [])
for question in question_source.get("questions", [])
if question.get("exercise_id") is not None
]
if data_model_version in {2, 1}:
item["node_ids"] = [
question["exercise_id"]
for question in item.get("question_sources", [])
if question.get("exercise_id") is not None
]
return item


Expand Down

0 comments on commit ffd1352

Please sign in to comment.