Skip to content

Commit

Permalink
feat: v2 customer content-metadata endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Oct 29, 2024
1 parent 4722f06 commit 557b06d
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 64 deletions.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from rest_framework.reverse import reverse

from enterprise_catalog.apps.api.v1.tests.mixins import APITestMixin
from enterprise_catalog.apps.catalog.models import (
CatalogQuery,
ContentMetadata,
EnterpriseCatalog,
)
from enterprise_catalog.apps.catalog.tests.factories import (
EnterpriseCatalogFactory,
)


class BaseEnterpriseCustomerViewSetTests(APITestMixin):
"""
Tests for the EnterpriseCustomerViewSet
"""
def setUp(self):
super().setUp()
# clean up any stale test objects
CatalogQuery.objects.all().delete()
ContentMetadata.objects.all().delete()
EnterpriseCatalog.objects.all().delete()

self.enterprise_catalog = EnterpriseCatalogFactory(enterprise_uuid=self.enterprise_uuid)

# Set up catalog.has_learner_access permissions
self.set_up_catalog_learner()

def tearDown(self):
super().tearDown()
# clean up any stale test objects
CatalogQuery.objects.all().delete()
ContentMetadata.objects.all().delete()
EnterpriseCatalog.objects.all().delete()

def _get_contains_content_base_url(self, enterprise_uuid=None):
"""
Helper to construct the base url for the contains_content_items endpoint
"""
return reverse(
'api:v1:enterprise-customer-contains-content-items',
kwargs={'enterprise_uuid': enterprise_uuid or self.enterprise_uuid},
)

def _get_filter_content_base_url(self, enterprise_uuid=None):
"""
Helper to construct the base url for the filter_content_items endpoint
"""
return reverse(
'api:v1:enterprise-customer-filter-content-items',
kwargs={'enterprise_uuid': enterprise_uuid or self.enterprise_uuid},
)

def _get_generate_diff_base_url(self, enterprise_catalog_uuid=None):
"""
Helper to construct the base url for the catalog `generate_diff` endpoint
"""
return reverse(
'api:v1:generate-catalog-diff',
kwargs={'uuid': enterprise_catalog_uuid or self.enterprise_catalog.uuid},
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,21 @@
import pytest
import pytz
from rest_framework import status
from rest_framework.reverse import reverse

from enterprise_catalog.apps.api.v1.tests.mixins import APITestMixin
from enterprise_catalog.apps.api.base.tests.enterprise_customer_views import BaseEnterpriseCustomerViewSetTests
from enterprise_catalog.apps.catalog.constants import (
RESTRICTED_RUNS_ALLOWED_KEY,
)
from enterprise_catalog.apps.catalog.models import (
CatalogQuery,
ContentMetadata,
EnterpriseCatalog,
)
from enterprise_catalog.apps.catalog.tests.factories import (
ContentMetadataFactory,
EnterpriseCatalogFactory,
)


class EnterpriseCustomerViewSetTests(APITestMixin):
class EnterpriseCustomerViewSetTests(BaseEnterpriseCustomerViewSetTests):
"""
Tests for the EnterpriseCustomerViewSet
"""

def setUp(self):
super().setUp()
# clean up any stale test objects
CatalogQuery.objects.all().delete()
ContentMetadata.objects.all().delete()
EnterpriseCatalog.objects.all().delete()

self.enterprise_catalog = EnterpriseCatalogFactory(enterprise_uuid=self.enterprise_uuid)

# Set up catalog.has_learner_access permissions
self.set_up_catalog_learner()

def tearDown(self):
super().tearDown()
# clean up any stale test objects
CatalogQuery.objects.all().delete()
ContentMetadata.objects.all().delete()
EnterpriseCatalog.objects.all().delete()

def _get_contains_content_base_url(self, enterprise_uuid=None):
"""
Helper to construct the base url for the contains_content_items endpoint
"""
return reverse(
'api:v1:enterprise-customer-contains-content-items',
kwargs={'enterprise_uuid': enterprise_uuid or self.enterprise_uuid},
)

def _get_filter_content_base_url(self, enterprise_uuid=None):
"""
Helper to construct the base url for the filter_content_items endpoint
"""
return reverse(
'api:v1:enterprise-customer-filter-content-items',
kwargs={'enterprise_uuid': enterprise_uuid or self.enterprise_uuid},
)

def _get_generate_diff_base_url(self, enterprise_catalog_uuid=None):
"""
Helper to construct the base url for the catalog `generate_diff` endpoint
"""
return reverse(
'api:v1:generate-catalog-diff',
kwargs={'uuid': enterprise_catalog_uuid or self.enterprise_catalog.uuid},
)

def test_generate_diff_unauthorized_non_catalog_learner(self):
"""
Verify the generate_diff endpoint rejects users that are not catalog learners
Expand Down
25 changes: 17 additions & 8 deletions enterprise_catalog/apps/api/v1/views/enterprise_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def get_permission_object(self):
"""
return self.kwargs.get('enterprise_uuid')

def filter_content_keys(self, catalog, content_keys):
return catalog.filter_content_keys(content_keys)

def contains_content_keys(self, catalog, content_keys):
return catalog.contains_content_keys(content_keys)

def get_metadata_by_uuid(self, catalog, content_uuid):
return catalog.content_metadata.filter(content_uuid=content_uuid)

def get_metadata_by_content_key(self, catalog, content_key):
return catalog.get_matching_content(content_keys=[content_key])

@method_decorator(require_at_least_one_query_parameter('course_run_ids', 'program_uuids'))
@action(detail=True)
def contains_content_items(self, request, enterprise_uuid, course_run_ids, program_uuids, **kwargs):
Expand Down Expand Up @@ -105,9 +117,9 @@ def contains_content_items(self, request, enterprise_uuid, course_run_ids, progr

any_catalog_contains_content_items = False
catalogs_that_contain_course = []
content_keys = requested_course_or_run_keys + program_uuids
for catalog in customer_catalogs:
contains_content_items = catalog.contains_content_keys(requested_course_or_run_keys + program_uuids)
if contains_content_items:
if contains_content_items := self.contains_content_keys(catalog, content_keys):
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
Expand Down Expand Up @@ -136,8 +148,7 @@ def filter_content_items(self, request, enterprise_uuid, **kwargs):

filtered_content_keys = set()
for catalog in customer_catalogs:
items_included = catalog.filter_content_keys(content_keys)
if items_included:
if items_included := self.filter_content_keys(catalog, content_keys):
filtered_content_keys = filtered_content_keys.union(items_included)

response_data = {
Expand All @@ -164,17 +175,15 @@ def get_metadata_item_serializer(self):
# identifier is a valid UUID.
content_uuid = uuid.UUID(content_identifier)
for catalog in enterprise_catalogs:
content_with_uuid = catalog.content_metadata.filter(content_uuid=content_uuid)
if content_with_uuid:
if content_with_uuid := self.get_metadata_by_uuid(catalog, content_uuid):
return ContentMetadataSerializer(
content_with_uuid.first(),
context={'enterprise_catalog': catalog, **serializer_context},
)
except ValueError:
# Otherwise, search for matching metadata as a content key
for catalog in enterprise_catalogs:
content_with_key = catalog.get_matching_content(content_keys=[content_identifier])
if content_with_key:
if content_with_key := self.get_metadata_by_content_key(catalog, content_identifier):
return ContentMetadataSerializer(
content_with_key.first(),
context={'enterprise_catalog': catalog, **serializer_context},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from asyncio.log import logger
import logging

from enterprise_catalog.apps.api.v1.views.enterprise_catalog_get_content_metadata import (
EnterpriseCatalogGetContentMetadata,
)
from enterprise_catalog.apps.api.v2.utils import is_any_course_run_active


logger = logging.getLogger(__name__)


class EnterpriseCatalogGetContentMetadataV2(EnterpriseCatalogGetContentMetadata):
"""
View for retrieving all the content metadata associated with a catalog.
Expand Down
25 changes: 25 additions & 0 deletions enterprise_catalog/apps/api/v2/views/enterprise_customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logging

from enterprise_catalog.apps.api.v1.views.enterprise_customer import (
EnterpriseCustomerViewSet,
)


logger = logging.getLogger(__name__)


class EnterpriseCustomerViewSetV2(EnterpriseCustomerViewSet):
"""
V2 views for content metadata and catalog-content inclusion for retrieving.
"""
def get_metadata_by_uuid(self, catalog, content_uuid):
return catalog.content_metadata_with_restricted.filter(content_uuid=content_uuid)

def get_metadata_by_content_key(self, catalog, content_key):
return catalog.get_matching_content(content_keys=[content_key], include_restricted=True)

def filter_content_keys(self, catalog, content_keys):
return catalog.filter_content_keys(content_keys, include_restricted=True)

def contains_content_keys(self, catalog, content_keys):
return catalog.contains_content_keys(content_keys, include_restricted=True)

0 comments on commit 557b06d

Please sign in to comment.