Ejemplo n.º 1
0
def update_course_with_enterprise_context(
        course,
        add_utm_info=True,
        enterprise_catalog_uuid=None,
        add_active_info=True
):
    """
    Populate a fake course response with any necessary Enterprise context for testing purposes.
    """
    url = urljoin(
        settings.LMS_ROOT_URL,
        reverse(
            'enterprise_course_enrollment_page',
            kwargs={'enterprise_uuid': FAKE_UUIDS[0], 'course_key': course['key']}
        )
    )

    course['enrollment_url'] = update_url_with_enterprise_context(
        url,
        add_utm_info=add_utm_info,
        enterprise_catalog_uuid=enterprise_catalog_uuid
    )

    course_runs = course.get('course_runs', [])

    if add_active_info:
        course['active'] = utils.has_course_run_available_for_enrollment(course_runs)
    for course_run in course_runs:
        update_course_run_with_enterprise_context(
            course_run,
            add_utm_info=add_utm_info,
            enterprise_catalog_uuid=enterprise_catalog_uuid
        )

    return course
Ejemplo n.º 2
0
    def to_representation(self, instance):
        """
        Serialize the EnterpriseCustomerCatalog object.

        Arguments:
            instance (EnterpriseCustomerCatalog): The EnterpriseCustomerCatalog to serialize.

        Returns:
            dict: The EnterpriseCustomerCatalog converted to a dict.
        """
        request = self.context['request']
        enterprise_customer = instance.enterprise_customer

        representation = super(EnterpriseCustomerCatalogDetailSerializer,
                               self).to_representation(instance)

        # Retrieve the EnterpriseCustomerCatalog search results from the discovery service.
        paginated_content = instance.get_paginated_content(request.GET)
        count = paginated_content['count']
        search_results = paginated_content['results']

        for item in search_results:
            content_type = item[ContentType.METADATA_KEY]
            marketing_url = item.get('marketing_url')
            if marketing_url:
                item['marketing_url'] = utils.update_query_parameters(
                    marketing_url,
                    utils.get_enterprise_utm_context(enterprise_customer))
            # Add the Enterprise enrollment URL to each content item returned from the discovery service.
            if content_type == ContentType.COURSE:
                item['enrollment_url'] = instance.get_course_enrollment_url(
                    item['key'])
                item['active'] = has_course_run_available_for_enrollment(
                    item['course_runs'])
            if content_type == ContentType.COURSE_RUN:
                item[
                    'enrollment_url'] = instance.get_course_run_enrollment_url(
                        item['key'], item[ContentType.COURSE])
            if content_type == ContentType.PROGRAM:
                item['enrollment_url'] = instance.get_program_enrollment_url(
                    item['uuid'])

        # Build pagination URLs
        previous_url = None
        next_url = None
        page = int(request.GET.get('page', '1'))
        request_uri = request.build_absolute_uri()
        if paginated_content['previous']:
            previous_url = utils.update_query_parameters(
                request_uri, {'page': page - 1})
        if paginated_content['next']:
            next_url = utils.update_query_parameters(request_uri,
                                                     {'page': page + 1})

        representation['count'] = count
        representation['previous'] = previous_url
        representation['next'] = next_url
        representation['results'] = search_results

        return representation