def courses(self, request, enterprise_customer, pk=None): # pylint: disable=invalid-name """ Retrieve the list of courses contained within this catalog. Only courses with active course runs are returned. A course run is considered active if it is currently open for enrollment, or will open in the future. """ catalog_api = CourseCatalogApiClient(request.user, enterprise_customer.site) courses = catalog_api.get_paginated_catalog_courses(pk, request.GET) # If the API returned an empty response, that means pagination has ended. # An empty response can also mean that there was a problem fetching data from catalog API. self.ensure_data_exists( request, courses, error_message= ("Unable to fetch API response for catalog courses from endpoint '{endpoint}'. " "The resource you are looking for does not exist.".format( endpoint=request.get_full_path()))) serializer = serializers.EnterpriseCatalogCoursesReadOnlySerializer( courses) # Add enterprise related context for the courses. serializer.update_enterprise_courses(enterprise_customer, catalog_id=pk) return get_paginated_response(serializer.data, request)
def courses(self, request, pk=None): # pylint: disable=invalid-name """ Retrieve the list of courses contained within this catalog. Only courses with active course runs are returned. A course run is considered active if it is currently open for enrollment, or will open in the future. --- serializer: serializers.CourseSerializerExcludingClosedRuns """ page = request.GET.get('page', 1) catalog_api = CourseCatalogApiClient(request.user) courses = catalog_api.get_paginated_catalog_courses(pk, page) # if API returned an empty response, that means pagination has ended. # An empty response can also means that there was a problem fetching data from catalog API. if not courses: logger.error( "Unable to fetch API response for catalog courses from endpoint '/catalog/%s/courses?page=%s'.", pk, page, ) raise NotFound("The resource you are looking for does not exist.") serializer = serializers.EnterpriseCatalogCoursesReadOnlySerializer( courses) # Add enterprise related context for the courses. serializer.update_enterprise_courses(request, pk) return get_paginated_response(serializer.data, request)
def test_get_paginated_response(self, previous_page, next_page, expected_previous, expected_next): """ Verify get_paginated_response returns correct response. """ self.data['next'] = next_page self.data['previous'] = previous_page # Update authentication parameters based in ddt data. response = get_paginated_response(self.data, self.request) assert response.data.get('next') == expected_next assert response.data.get('previous') == expected_previous
def list(self, request): """ DRF view to list all catalogs. Arguments: request (HttpRequest): Current request Returns: (Response): DRF response object containing course catalogs. """ catalog_api = CourseCatalogApiClient(request.user) catalogs = catalog_api.get_paginated_catalogs(request.GET) self.ensure_data_exists(request, catalogs) serializer = serializers.ResponsePaginationSerializer(catalogs) return get_paginated_response(serializer.data, request)
def courses(self, request, pk=None): # pylint: disable=invalid-name,unused-argument """ Retrieve the list of courses contained within the catalog linked to this enterprise. Only courses with active course runs are returned. A course run is considered active if it is currently open for enrollment, or will open in the future. """ enterprise_customer = self.get_object() self.check_object_permissions(request, enterprise_customer) self.ensure_data_exists( request, enterprise_customer.catalog, error_message= "No catalog is associated with Enterprise {enterprise_name} from endpoint '{path}'." .format(enterprise_name=enterprise_customer.name, path=request.get_full_path())) # We have handled potential error cases and are now ready to call out to the Catalog API. catalog_api = CourseCatalogApiClient(request.user, enterprise_customer.site) courses = catalog_api.get_paginated_catalog_courses( enterprise_customer.catalog, request.GET) # An empty response means that there was a problem fetching data from Catalog API, since # a Catalog with no courses has a non empty response indicating that there are no courses. self.ensure_data_exists( request, courses, error_message=( "Unable to fetch API response for catalog courses for " "Enterprise {enterprise_name} from endpoint '{path}'.".format( enterprise_name=enterprise_customer.name, path=request.get_full_path()))) serializer = serializers.EnterpriseCatalogCoursesReadOnlySerializer( courses) # Add enterprise related context for the courses. serializer.update_enterprise_courses( enterprise_customer, catalog_id=enterprise_customer.catalog) return get_paginated_response(serializer.data, request)
def test_get_paginated_response_correct_query_parameters(self): """ Verify get_paginated_response returns correct response. """ self.data['next'] = '{discovery_uri}?page=3'.format( discovery_uri=DISCOVERY_URI) self.data['previous'] = '{discovery_uri}?page=1'.format( discovery_uri=DISCOVERY_URI) expected_next = '{enterprise_uri}?page=3'.format( enterprise_uri=ENTERPRISE_URI) expected_previous = '{enterprise_uri}?page=1'.format( enterprise_uri=ENTERPRISE_URI) request = APIRequestFactory().get( reverse('catalogs-list') + "?page=2", SERVER_NAME="testserver.enterprise", ) # Update authentication parameters based in ddt data. response = get_paginated_response(self.data, request) assert response.data.get('next') == expected_next assert response.data.get('previous') == expected_previous