Skip to content

Commit

Permalink
Filter Subjects base on Partner (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
burhandodhy authored May 26, 2021
1 parent 94381f5 commit 8bc6a01
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
9 changes: 6 additions & 3 deletions course_discovery/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,23 @@ class Meta:

class SubjectSerializer(DynamicFieldsMixin, BaseModelSerializer):
"""Serializer for the ``Subject`` model."""
number_of_courses = serializers.SerializerMethodField()

@classmethod
def prefetch_queryset(cls):
return Subject.objects.all().prefetch_related('translations')
def prefetch_queryset(cls, partner):
return Subject.objects.filter(partner=partner).prefetch_related('translations')

class Meta:
model = Subject
fields = ('name', 'subtitle', 'description', 'banner_image_url', 'card_image_url', 'slug', 'uuid', 'marketing_url')
fields = ('name', 'subtitle', 'description', 'banner_image_url', 'card_image_url', 'slug', 'uuid', 'marketing_url', 'number_of_courses')

@property
def choices(self):
# choices shows the possible values via HTTP's OPTIONS verb
return OrderedDict(sorted([(x.slug, x.name) for x in Subject.objects.all()], key=lambda x: x[1]))

def get_number_of_courses(self, obj):
return obj.course_set.count()

class PrerequisiteSerializer(NamedModelSerializer):
"""Serializer for the ``Prerequisite`` model."""
Expand Down
2 changes: 2 additions & 0 deletions course_discovery/apps/api/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,8 @@ def test_data(self):
'subtitle': subject.subtitle,
'slug': subject.slug,
'uuid': str(subject.uuid),
'marketing_url': None,
'number_of_courses': 0
}

self.assertDictEqual(serializer.data, expected)
Expand Down
3 changes: 2 additions & 1 deletion course_discovery/apps/api/v1/views/subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class SubjectViewSet(viewsets.ReadOnlyModelViewSet):
pagination_class = ProxiedPagination

def get_queryset(self):
return serializers.SubjectSerializer.prefetch_queryset()
partner = self.request.site.partner
return serializers.SubjectSerializer.prefetch_queryset(partner=partner)

def list(self, request, *args, **kwargs):
""" Retrieve a list of all subjects. """
Expand Down

0 comments on commit 8bc6a01

Please sign in to comment.