Skip to content

Commit

Permalink
Merge pull request #928 from openedx/pwnage101/revert-contains-conten…
Browse files Browse the repository at this point in the history
…t-items-changes-for-restricted-runs

revert: restricted runs support in contains_content_items endpoints
  • Loading branch information
pwnage101 authored Sep 4, 2024
2 parents 3877224 + 4231e48 commit d1e0a52
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime, timedelta
from unittest import mock

import pytest
import pytz
from rest_framework import status
from rest_framework.reverse import reverse
Expand Down Expand Up @@ -356,6 +357,7 @@ def test_contains_catalog_list_with_catalog_list_param(self):
catalog_list = response.json()['catalog_list']
assert set(catalog_list) == {str(second_catalog.uuid)}

@pytest.mark.skip(reason="We need a version of this test for the v2 API.")
def test_contains_catalog_list_with_content_ids_param(self):
"""
Verify the contains_content_items endpoint returns a list of catalogs the course is in if the correct
Expand All @@ -367,11 +369,6 @@ def test_contains_catalog_list_with_content_ids_param(self):
# Create a two catalogs that have the content we're looking for
content_key = 'fake-key+101x'
second_catalog = EnterpriseCatalogFactory(enterprise_uuid=self.enterprise_uuid)
# Add some non-null, but irrelevant restricted runs to this catalog
second_catalog.catalog_query.content_filter[RESTRICTED_RUNS_ALLOWED_KEY] = {
'something+else1': ['course-v1:something+else+restrictedrun']
}
second_catalog.catalog_query.save()

relevant_content = ContentMetadataFactory(content_key=content_key)
self.add_metadata_to_catalog(second_catalog, [relevant_content])
Expand All @@ -385,6 +382,7 @@ def test_contains_catalog_list_with_content_ids_param(self):
assert set(catalog_list) == {str(second_catalog.uuid)}
self.assertIsNone(response_payload['restricted_runs_allowed'])

@pytest.mark.skip(reason="We need a version of this test for the v2 API.")
def test_contains_catalog_key_restricted_runs_allowed(self):
"""
Tests that, when a course key is requested, we also get a response
Expand Down Expand Up @@ -442,6 +440,7 @@ def test_contains_catalog_key_restricted_runs_allowed(self):
}
)

@pytest.mark.skip(reason="We need a version of this test for the v2 API.")
def test_restricted_course_disallowed_if_course_not_in_catalog(self):
"""
Tests that a requested course with restricted runs is "disallowed"
Expand All @@ -463,6 +462,7 @@ def test_restricted_course_disallowed_if_course_not_in_catalog(self):
self.assertFalse(response_payload.get('contains_content_items'))
self.assertIsNone(response_payload['restricted_runs_allowed'])

@pytest.mark.skip(reason="We need a version of this test for the v2 API.")
def test_restricted_course_run_allowed_even_if_course_not_in_catalog(self):
"""
Tests that a requested restricted course run is "allowed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
)
from enterprise_catalog.apps.api.v1.utils import unquote_course_keys
from enterprise_catalog.apps.api.v1.views.base import BaseViewSet
from enterprise_catalog.apps.catalog.api import (
catalog_contains_any_restricted_course_run,
)
from enterprise_catalog.apps.catalog.models import EnterpriseCatalog


Expand Down Expand Up @@ -61,9 +58,4 @@ def contains_content_items(self, request, uuid, course_run_ids, program_uuids, *

enterprise_catalog = self.get_object()
contains_content_items = enterprise_catalog.contains_content_keys(course_run_ids + program_uuids)
contains_requested_restricted_items = catalog_contains_any_restricted_course_run(
enterprise_catalog, course_run_ids,
)
return Response({
'contains_content_items': contains_content_items or contains_requested_restricted_items
})
return Response({'contains_content_items': contains_content_items})
33 changes: 10 additions & 23 deletions enterprise_catalog/apps/api/v1/views/enterprise_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
from enterprise_catalog.apps.api.v1.serializers import ContentMetadataSerializer
from enterprise_catalog.apps.api.v1.utils import unquote_course_keys
from enterprise_catalog.apps.api.v1.views.base import BaseViewSet
from enterprise_catalog.apps.catalog.api import (
catalog_contains_any_restricted_course_run,
get_restricted_runs_allowed_for_query,
)
from enterprise_catalog.apps.catalog.constants import (
RESTRICTED_RUNS_ALLOWED_KEY,
)
from enterprise_catalog.apps.catalog.models import EnterpriseCatalog


Expand Down Expand Up @@ -108,31 +101,25 @@ def contains_content_items(self, request, enterprise_uuid, course_run_ids, progr
f'Error: invalid enterprice customer uuid: "{enterprise_uuid}" provided.',
status=HTTP_400_BAD_REQUEST
)
customer_catalogs = list(
EnterpriseCatalog.objects.select_related(
'catalog_query',
).filter(
enterprise_uuid=enterprise_uuid,
))
customer_catalogs = EnterpriseCatalog.objects.filter(enterprise_uuid=enterprise_uuid)

any_catalog_contains_content_items = False
catalogs_that_contain_course = []
for catalog in customer_catalogs:
contains_content_items = catalog.contains_content_keys(requested_course_or_run_keys + program_uuids)
contains_requested_restricted_items = catalog_contains_any_restricted_course_run(
catalog, requested_course_or_run_keys,
)
if contains_content_items or contains_requested_restricted_items:
catalogs_that_contain_course.append(catalog)
if contains_content_items:
any_catalog_contains_content_items = True
if not (get_catalogs_containing_specified_content_ids or get_catalog_list):
# Break as soon as we find a catalog that contains the specified content
break
catalogs_that_contain_course.append(catalog.uuid)

response_data = {
'contains_content_items': bool(catalogs_that_contain_course),
'contains_content_items': any_catalog_contains_content_items,
}
if (get_catalogs_containing_specified_content_ids or get_catalog_list):
response_data['catalog_list'] = [str(catalog.uuid) for catalog in catalogs_that_contain_course]
response_data['catalog_list'] = catalogs_that_contain_course

response_data[RESTRICTED_RUNS_ALLOWED_KEY] = get_restricted_runs_allowed_for_query(
requested_course_or_run_keys, catalogs_that_contain_course,
)
return Response(response_data)

@action(detail=True, methods=['post'])
Expand Down
73 changes: 0 additions & 73 deletions enterprise_catalog/apps/catalog/api.py

This file was deleted.

0 comments on commit d1e0a52

Please sign in to comment.