Skip to content

Commit

Permalink
exclude topics with no associated channel from Topic querySets (#1693)
Browse files Browse the repository at this point in the history
  • Loading branch information
gumaerc authored Oct 16, 2024
1 parent 9388929 commit 50e17e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
4 changes: 4 additions & 0 deletions learning_resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ class TopicViewSet(viewsets.ReadOnlyModelViewSet):
filter_backends = [DjangoFilterBackend]
filterset_class = TopicFilter

def filter_queryset(self, queryset):
queryset = queryset.exclude(channel_url__isnull=True)
return super().filter_queryset(queryset)

@method_decorator(
cache_page_for_all_users(
settings.SEARCH_PAGE_CACHE_DURATION, cache="redis", key_prefix="search"
Expand Down
17 changes: 9 additions & 8 deletions learning_resources/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ def test_topics_list_endpoint(client, django_assert_num_queries):
LearningResourceTopicFactory.create_batch(100),
key=lambda topic: topic.name,
)
for topic in topics:
ChannelTopicDetailFactory.create(topic=topic)

with django_assert_num_queries(2):
resp = client.get(reverse("lr:v1:topics_api-list"))
Expand All @@ -564,28 +566,27 @@ def test_topics_list_endpoint(client, django_assert_num_queries):
def test_topics_detail_endpoint(client):
"""Test topics detail endpoint"""
topic = LearningResourceTopicFactory.create()
ChannelTopicDetailFactory.create(topic=topic)
resp = client.get(reverse("lr:v1:topics_api-detail", args=[topic.pk]))
assert resp.data == LearningResourceTopicSerializer(instance=topic).data


@pytest.mark.parametrize("published", [True, False])
def test_topic_channel_url(client, published):
"""
Check that the topic API returns 'None' for channel_url of unpublished channels.
Note: The channel_url being None is also tested on the Channel model itself,
but the API may generate the channel_url in a slightly different manner (for
example, queryset annotation)
Test that topics with published channels return the channel_url,
and unpublished channels are not included in the response
"""
topic = LearningResourceTopicFactory.create()
channel = ChannelTopicDetailFactory.create(
topic=topic, is_unpublished=not published
).channel
resp = client.get(reverse("lr:v1:topics_api-detail", args=[topic.pk]))

assert resp.data["channel_url"] == channel.channel_url
if not published:
assert resp.data["channel_url"] is None
if published:
assert resp.data["channel_url"] == channel.channel_url
else:
assert resp.status_code == 404


def test_departments_list_endpoint(client):
Expand Down

0 comments on commit 50e17e1

Please sign in to comment.