Skip to content

Commit

Permalink
Merge pull request #774 from openedx/asheehan-edx/updating-metadata-q…
Browse files Browse the repository at this point in the history
…ueryset

fix: properly fetching querysets for content metadata api endpoint
  • Loading branch information
alex-sheehan-edx authored Feb 14, 2024
2 parents 2401451 + 1900c9e commit ce5ed31
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
8 changes: 6 additions & 2 deletions enterprise_catalog/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2377,7 +2377,8 @@ def setUp(self):
super().setUp()
self.set_up_staff()
self.content_metadata_object = ContentMetadataFactory(
content_type='course'
content_type='course',
content_uuid=uuid.uuid4(),
)

def test_list_success(self):
Expand All @@ -2395,7 +2396,10 @@ def test_list_with_content_keys(self):
Test a successful, expected api response for the metadata list endpoint with a supplied content keys query
param
"""
query_param_string = f"?content_identifiers={self.content_metadata_object.content_key}"
ContentMetadataFactory(content_type='course')
junk_identifier = urlencode({'content_identifiers': 'edx+101'})
encoded_key = urlencode({'content_identifiers': self.content_metadata_object.content_key})
query_param_string = f"?{encoded_key}&{junk_identifier}"
url = reverse('api:v1:content-metadata-list') + query_param_string
response = self.client.get(url)
response_json = response.json()
Expand Down
34 changes: 30 additions & 4 deletions enterprise_catalog/apps/api/v1/views/content_metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db.models import Q
import uuid

from edx_rest_framework_extensions.auth.jwt.authentication import (
JwtAuthentication,
)
Expand All @@ -14,6 +15,27 @@
from enterprise_catalog.apps.catalog.models import ContentMetadata


# https://stackoverflow.com/questions/53847404/how-to-check-uuid-validity-in-python
def is_valid_uuid(val):
try:
uuid.UUID(str(val))
return True
except ValueError:
return False


# https://stackoverflow.com/questions/4578590/python-equivalent-of-filter-getting-two-output-lists-i-e-partition-of-a-list
def partition(pred, iterable):
trues = []
falses = []
for item in iterable:
if pred(item):
trues.append(item)
else:
falses.append(item)
return trues, falses


class ContentMetadataView(viewsets.ReadOnlyModelViewSet):
"""
View for retrieving and listing base content metadata.
Expand All @@ -29,8 +51,12 @@ def get_queryset(self, **kwargs):
"""
Returns all content metadata objects filtered by an optional request query param (LIST) ``content_identifiers``
"""
content_filter = kwargs.get('content_identifiers')
content_filters = self.request.query_params.getlist('content_identifiers')
queryset = self.queryset
if content_filter:
return queryset.filter(Q(content_key__in=content_filter) | Q(uuid__in=content_filter))
if content_filters:
content_uuids, content_keys = partition(is_valid_uuid, content_filters)
if content_keys:
queryset = queryset.filter(content_key__in=content_filters)
if content_uuids:
queryset = queryset.filter(content_uuid__in=content_filters)
return queryset

0 comments on commit ce5ed31

Please sign in to comment.