def update_course_runs(self, course_runs, enterprise_customer, enterprise_context): """ Update Marketing urls in course metadata and return updated course. Arguments: course_runs (list): List of course runs. enterprise_customer (EnterpriseCustomer): enterprise customer instance. enterprise_context (dict): The context to inject into URLs. Returns: (dict): Dictionary containing updated course metadata. """ updated_course_runs = [] for course_run in course_runs: track_selection_url = utils.get_course_track_selection_url( course_run=course_run, query_parameters=dict(enterprise_context, **utils.get_enterprise_utm_context(enterprise_customer)), ) enrollment_url = enterprise_customer.get_course_run_enrollment_url(course_run.get('key')) course_run.update({ 'enrollment_url': enrollment_url, 'track_selection_url': track_selection_url, }) # Update marketing urls in course metadata to include enterprise related info. marketing_url = course_run.get('marketing_url') if marketing_url: query_parameters = dict(enterprise_context, **utils.get_enterprise_utm_context(enterprise_customer)) course_run.update({'marketing_url': utils.update_query_parameters(marketing_url, query_parameters)}) # Add updated course run to the list. updated_course_runs.append(course_run) return updated_course_runs
def update_course(self, course, enterprise_customer, enterprise_context): """ Update course metadata of the given course and return updated course. Arguments: course (dict): Course Metadata returned by course catalog API enterprise_customer (EnterpriseCustomer): enterprise customer instance. enterprise_context (dict): Enterprise context to be added to course runs and URLs.. Returns: (dict): Updated course metadata """ course['course_runs'] = self.update_course_runs( course_runs=course.get('course_runs') or [], enterprise_customer=enterprise_customer, enterprise_context=enterprise_context, parent_course_key=course['key'], ) # Update marketing urls in course metadata to include enterprise related info (i.e. our global context). marketing_url = course.get('marketing_url') if marketing_url: query_parameters = dict( enterprise_context, **utils.get_enterprise_utm_context(enterprise_customer)) course.update({ 'marketing_url': utils.update_query_parameters(marketing_url, query_parameters) }) # Finally, add context to the course as a whole. course.update(enterprise_context) return course
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