def get_credit_providers(providers_list=None): """Retrieve all available credit providers or filter on given providers_list. Arguments: providers_list (list of strings or None): contains list of ids of credit providers or None. Returns: list of credit providers represented as dictionaries Response Values: >>> get_credit_providers(['hogwarts']) [ { "id": "hogwarts", "name": "Hogwarts School of Witchcraft and Wizardry", "url": "https://credit.example.com/", "status_url": "https://credit.example.com/status/", "description: "A new model for the Witchcraft and Wizardry School System.", "enable_integration": false, "fulfillment_instructions": " <p>In order to fulfill credit, Hogwarts School of Witchcraft and Wizardry requires learners to:</p> <ul> <li>Sample instruction abc</li> <li>Sample instruction xyz</li> </ul>", }, ... ] """ return CreditProvider.get_credit_providers(providers_list=providers_list)
def get_credit_provider_attribute_values(course_key, attribute_name): """Get the course information from ecommerce and parse the data to get providers. Arguments: course_key (CourseKey): The identifier for the course. attribute_name (String): Name of the attribute of credit provider. Returns: List of provided credit provider attribute values. """ course_id = six.text_type(course_key) credit_config = CreditConfig.current() cache_key = None attribute_values = None if credit_config.is_cache_enabled: cache_key = '{key_prefix}.{course_key}.{attribute_name}'.format( key_prefix=credit_config.CACHE_KEY, course_key=course_id, attribute_name=attribute_name) attribute_values = cache.get(cache_key) if attribute_values is not None: return attribute_values try: user = User.objects.get( username=settings.ECOMMERCE_SERVICE_WORKER_USERNAME) response = ecommerce_api_client(user).courses(course_id).get( include_products=1) except Exception: # pylint: disable=broad-except log.exception( u"Failed to receive data from the ecommerce course API for Course ID '%s'.", course_id) return attribute_values if not response: log.info( u"No Course information found from ecommerce API for Course ID '%s'.", course_id) return attribute_values provider_ids = [] for product in response.get('products'): provider_ids += [ attr.get('value') for attr in product.get('attribute_values') if attr.get('name') == 'credit_provider' ] attribute_values = [] credit_providers = CreditProvider.get_credit_providers() for provider in credit_providers: if provider['id'] in provider_ids: attribute_values.append(provider[attribute_name]) if credit_config.is_cache_enabled: cache.set(cache_key, attribute_values, credit_config.cache_ttl) return attribute_values
def get_credit_provider_attribute_values(course_key, attribute_name): """Get the course information from ecommerce and parse the data to get providers. Arguments: course_key (CourseKey): The identifier for the course. attribute_name (String): Name of the attribute of credit provider. Returns: List of provided credit provider attribute values. """ course_id = six.text_type(course_key) credit_config = CreditConfig.current() cache_key = None attribute_values = None if credit_config.is_cache_enabled: cache_key = '{key_prefix}.{course_key}.{attribute_name}'.format( key_prefix=credit_config.CACHE_KEY, course_key=course_id, attribute_name=attribute_name ) attribute_values = cache.get(cache_key) if attribute_values is not None: return attribute_values try: user = User.objects.get(username=settings.ECOMMERCE_SERVICE_WORKER_USERNAME) response = ecommerce_api_client(user).courses(course_id).get(include_products=1) except Exception: # pylint: disable=broad-except log.exception(u"Failed to receive data from the ecommerce course API for Course ID '%s'.", course_id) return attribute_values if not response: log.info(u"No Course information found from ecommerce API for Course ID '%s'.", course_id) return attribute_values provider_ids = [] for product in response.get('products'): provider_ids += [ attr.get('value') for attr in product.get('attribute_values') if attr.get('name') == 'credit_provider' ] attribute_values = [] credit_providers = CreditProvider.get_credit_providers() for provider in credit_providers: if provider['id'] in provider_ids: attribute_values.append(provider[attribute_name]) if credit_config.is_cache_enabled: cache.set(cache_key, attribute_values, credit_config.cache_ttl) return attribute_values
def get_credit_provider_display_names(course_key): """Get the course information from ecommerce and parse the data to get providers. Arguments: course_key (CourseKey): The identifier for the course. Returns: List of credit provider display names. """ course_id = unicode(course_key) credit_config = CreditConfig.current() cache_key = None provider_names = None if credit_config.is_cache_enabled: cache_key = "{key_prefix}.{course_key}".format(key_prefix=credit_config.CACHE_KEY, course_key=course_id) provider_names = cache.get(cache_key) if provider_names is not None: return provider_names try: user = User.objects.get(username=settings.ECOMMERCE_SERVICE_WORKER_USERNAME) response = ecommerce_api_client(user).courses(course_id).get(include_products=1) except Exception: # pylint: disable=broad-except log.exception("Failed to receive data from the ecommerce course API for Course ID '%s'.", course_id) return provider_names if not response: log.info("No Course information found from ecommerce API for Course ID '%s'.", course_id) return provider_names provider_ids = [] for product in response.get("products"): provider_ids += [ attr.get("value") for attr in product.get("attribute_values") if attr.get("name") == "credit_provider" ] provider_names = [] credit_providers = CreditProvider.get_credit_providers() for provider in credit_providers: if provider["id"] in provider_ids: provider_names.append(provider["display_name"]) if credit_config.is_cache_enabled: cache.set(cache_key, provider_names, credit_config.cache_ttl) return provider_names
def get_credit_providers(): """ Retrieve all available credit providers. Example: >>> get_credit_providers() [ { "id": "hogwarts", "display_name": "Hogwarts School of Witchcraft and Wizardry" }, ... ] Returns: list """ return CreditProvider.get_credit_providers()
def get_credit_provider_info(request, provider_id): # pylint: disable=unused-argument """Retrieve the 'CreditProvider' model data against provided credit provider. Args: provider_id (str): The identifier for the credit provider Returns: 'CreditProvider' data dictionary Example Usage: >>> get_credit_provider_info("hogwarts") { "provider_id": "hogwarts", "display_name": "Hogwarts School of Witchcraft and Wizardry", "provider_url": "https://credit.example.com/", "provider_status_url": "https://credit.example.com/status/", "provider_description: "A new model for the Witchcraft and Wizardry School System.", "enable_integration": False, "fulfillment_instructions": " <p>In order to fulfill credit, Hogwarts School of Witchcraft and Wizardry requires learners to:</p> <ul> <li>Sample instruction abc</li> <li>Sample instruction xyz</li> </ul>", "thumbnail_url": "https://credit.example.com/logo.png" } """ credit_provider = CreditProvider.get_credit_provider( provider_id=provider_id) credit_provider_data = {} if credit_provider: credit_provider_data = { "provider_id": credit_provider.provider_id, "display_name": credit_provider.display_name, "provider_url": credit_provider.provider_url, "provider_status_url": credit_provider.provider_status_url, "provider_description": credit_provider.provider_description, "enable_integration": credit_provider.enable_integration, "fulfillment_instructions": credit_provider.fulfillment_instructions, "thumbnail_url": credit_provider.thumbnail_url } return JsonResponse(credit_provider_data)
def get_credit_provider_info(request, provider_id): # pylint: disable=unused-argument """Retrieve the 'CreditProvider' model data against provided credit provider. Args: provider_id (str): The identifier for the credit provider Returns: 'CreditProvider' data dictionary Example Usage: >>> get_credit_provider_info("hogwarts") { "provider_id": "hogwarts", "display_name": "Hogwarts School of Witchcraft and Wizardry", "provider_url": "https://credit.example.com/", "provider_status_url": "https://credit.example.com/status/", "provider_description: "A new model for the Witchcraft and Wizardry School System.", "enable_integration": False, "fulfillment_instructions": " <p>In order to fulfill credit, Hogwarts School of Witchcraft and Wizardry requires learners to:</p> <ul> <li>Sample instruction abc</li> <li>Sample instruction xyz</li> </ul>", "thumbnail_url": "https://credit.example.com/logo.png" } """ credit_provider = CreditProvider.get_credit_provider(provider_id=provider_id) credit_provider_data = {} if credit_provider: credit_provider_data = { "provider_id": credit_provider.provider_id, "display_name": credit_provider.display_name, "provider_url": credit_provider.provider_url, "provider_status_url": credit_provider.provider_status_url, "provider_description": credit_provider.provider_description, "enable_integration": credit_provider.enable_integration, "fulfillment_instructions": credit_provider.fulfillment_instructions, "thumbnail_url": credit_provider.thumbnail_url } return JsonResponse(credit_provider_data)