Skip to content

Commit

Permalink
Add consecutive_order, parent_name and status to exercise API
Browse files Browse the repository at this point in the history
Add a consecutive numbering to the API to indicate the order of learning
objects (chapters and exercises). This is calculated dynamically.

Add a field to the API "parent_name", which contains the name of the
parent, i.e., the chapter or exercise the exercise is embedded in (if it
is embedded).
The name of the chapter can provide more understandable context than
just the exercise path or the title of the exercise (e.g. "Feedback").

The consecutive_order field is used to fix Jutut issues #43 and #44.
The parent_name field is used to fix Jutut issue #46.
The status can be used to identify enrollment questionnaires, needed
for fixing Jutut issue #31.
  • Loading branch information
etanttila authored and lainets committed Jul 17, 2023
1 parent 20a749b commit 041919f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
21 changes: 20 additions & 1 deletion exercise/api/full_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from course.api.serializers import CourseBriefSerializer
from userprofile.api.serializers import UserBriefSerializer

from ..models import Submission
from ..models import (
Submission,
BaseExercise
)
from .serializers import (
ExerciseBriefSerializer,
SubmissionBriefSerializer,
Expand All @@ -31,6 +34,8 @@
class ExerciseSerializer(ExerciseBriefSerializer):
course = CourseBriefSerializer(source='course_instance')
post_url = serializers.SerializerMethodField()
consecutive_order = serializers.SerializerMethodField()
parent_name = serializers.SerializerMethodField()
exercise_info = serializers.JSONField()
submissions = NestedHyperlinkedIdentityField(
view_name='api:exercise-submissions-list',
Expand Down Expand Up @@ -60,18 +65,32 @@ def get_post_url(self, obj):
return request.build_absolute_uri(url)
return None

def get_consecutive_order(self, obj: BaseExercise) -> int:
content = self.context['cached_content']
order = content.get_absolute_order_number(obj.id)
return order

def get_parent_name(self, obj: BaseExercise) -> str:
parent = obj.parent
if parent is None:
return None
return str(parent)

class Meta(ExerciseBriefSerializer.Meta):
fields = (
'name',
'course',
'is_submittable',
'post_url',
'consecutive_order',
'parent_name',
'exercise_info',
'templates',
'submissions',
'my_submissions',
'my_stats',
'statistics',
'status',
)


Expand Down
5 changes: 5 additions & 0 deletions exercise/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class ExerciseViewSet(mixins.RetrieveModelMixin,
serializer_class = ExerciseSerializer
queryset = BaseExercise.objects.all()

def get_serializer_context(self):
context = super().get_serializer_context()
context.update({'cached_content': self.content})
return context

@action(
detail=True,
url_path='grader',
Expand Down
22 changes: 22 additions & 0 deletions exercise/cache/hierarchy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Tuple
from django.http.response import Http404
from course.models import CourseModule, LearningObjectCategory
from ..models import LearningObject
Expand Down Expand Up @@ -172,6 +173,27 @@ def find(self, model):
self._next(idx, tree),
)

def get_absolute_order_number(self, learning_object_id: int) -> int:
"""Get the absolute order number of the given learning object
(i.e. how manieth chapter or exercise it is in the material).
"""
def inner(parent: dict, n: int) -> Tuple[bool, int]:
# parent is a cached dict representing a CourseModule or a LearningObject
for entry in parent['children']:
n += 1
if entry['id'] == learning_object_id:
return True, n
found, n = inner(entry, n)
if found:
return True, n
return False, n

n = 0
for module in self.modules():
found, n = inner(module, n)
if found:
return n

def search_exercises(self, **kwargs):
_, entries = self.search_entries(**kwargs)
return [e for e in entries if e['type'] == 'exercise']
Expand Down

0 comments on commit 041919f

Please sign in to comment.