Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add get_content_filter_hash endpoint #858

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to reverse the URL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly needed, but if we hypothetically need to change the url scheme in the future, we can more easily track references this way rather than having to hunt down url snippets.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using reverse() to turn a view name into a full URL is a pretty standard practice for django unit tests.
https://stackoverflow.com/a/11241936 has a good overview of what/why reverse is.

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)
Loading