Skip to content

Commit

Permalink
feat: add get_content_filter_hash endpoint
Browse files Browse the repository at this point in the history
style: fix pycodestyle issues

chore: pylint disable=no-member
  • Loading branch information
marlonkeating committed Jun 26, 2024
1 parent 07d4a04 commit 3ccb613
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions enterprise_catalog/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,15 @@ def test_get_query_by_hash_requires_hash(self):
response_json = response.json()
assert response_json == ['You must provide at least one of the following query parameters: hash.']

def test_get_content_filter_hash(self):
"""
Test that get content filter hash returns md5 hash of query
"""
url = reverse('api:v1:get-content-filter-hash')
test_query = '{ "content_type": [ "political", "unit", "market" ] }'
response = self.client.generic('GET', url, data=test_query)
assert response.json() == '35584b583415a5bd4e51cc70d898a0eb' # pylint: disable=no-member

def test_catalog_query_retrieve(self):
"""
Test that the Catalog Query viewset supports retrieving individual queries
Expand Down
5 changes: 5 additions & 0 deletions enterprise_catalog/apps/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
CatalogQueryViewSet.as_view({'get': 'get_query_by_hash'}),
name='get-query-by-hash'
),
path(
'catalog-queries/get_content_filter_hash',
CatalogQueryViewSet.as_view({'get': 'get_content_filter_hash'}),
name='get-content-filter-hash'
),
]

urlpatterns += router.urls
11 changes: 11 additions & 0 deletions enterprise_catalog/apps/api/v1/views/catalog_query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from django.utils.decorators import method_decorator
from django.utils.functional import cached_property
from edx_rbac.mixins import PermissionRequiredForListingMixin
Expand All @@ -24,6 +26,7 @@
enterprises_with_admin_access,
has_access_to_all_enterprises,
)
from enterprise_catalog.apps.catalog.utils import get_content_filter_hash


class CatalogQueryViewSet(viewsets.ReadOnlyModelViewSet, BaseViewSet, PermissionRequiredForListingMixin):
Expand Down Expand Up @@ -106,3 +109,11 @@ def get_query_by_hash(self, request, **kwargs):
raise NotFound('Catalog query not found.') from exc
serialized_data = self.serializer_class(query)
return Response(serialized_data.data)

@action(detail=True, methods=['get'])
def get_content_filter_hash(self, request, **kwargs):
"""
Get md5 hash of a catalog query
"""
content_filter_hash = get_content_filter_hash(json.loads(request.body))
return Response(content_filter_hash)

0 comments on commit 3ccb613

Please sign in to comment.