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

chore: pycodestyle fix

chore: add bad json error handling for get_content_filter_hash

chore: address review comments
  • Loading branch information
marlonkeating committed Jul 15, 2024
1 parent 07d4a04 commit a2707dc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
19 changes: 19 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,25 @@ 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 = json.dumps({"content_type": ["political", "unit", "market"]})
response = self.client.generic('GET', url, content_type='application/json', data=test_query)
assert response.json() == '35584b583415a5bd4e51cc70d898a0eb' # pylint: disable=no-member

def test_get_content_filter_hash_bad_query(self):
"""
Test that get content filter hash returns md5 hash of query
"""
url = reverse('api:v1:get-content-filter-hash')
test_query = 'bad query'
response = self.client.generic('GET', url, content_type='application/json', data=test_query)
err_detail = "Failed to parse catalog query: JSON parse error - Expecting value: line 1 column 1 (char 0)"
assert response.json() == {"detail": err_detail} # 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
15 changes: 14 additions & 1 deletion enterprise_catalog/apps/api/v1/views/catalog_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from edx_rbac.mixins import PermissionRequiredForListingMixin
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import NotFound
from rest_framework.exceptions import NotFound, ParseError
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response

Expand All @@ -24,6 +24,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 +107,15 @@ 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
"""
try:
content_filter = request.data
content_filter_hash = get_content_filter_hash(content_filter)
except ParseError as exc:
raise ParseError(f"Failed to parse catalog query: {exc}") from exc
return Response(content_filter_hash)

0 comments on commit a2707dc

Please sign in to comment.