def get_program(program_uuid, siteconfiguration): """ Returns details for the program identified by the program_uuid. Data is retrieved from the Discovery Service, and cached for ``settings.PROGRAM_CACHE_TIMEOUT`` seconds. Args: siteconfiguration (SiteConfiguration): Configuration containing the requisite parameters to connect to the Discovery Service. program_uuid (uuid): id to query the specified program Returns: dict None if not found or another error occurs """ response = None try: client = ProgramsApiClient(siteconfiguration.discovery_api_client, siteconfiguration.site.domain) response = client.get_program(str(program_uuid)) except HttpNotFoundError: msg = 'No program data found for {}'.format(program_uuid) log.debug(msg) except (ReqConnectionError, SlumberBaseException, Timeout): msg = 'Failed to retrieve program details for {}'.format(program_uuid) log.debug(msg) return response
class ProgramsApiClientTests(ProgramTestMixin, TestCase): def setUp(self): super(ProgramsApiClientTests, self).setUp() httpretty.enable() self.mock_access_token_response() self.client = ProgramsApiClient( self.site.siteconfiguration.discovery_api_client, self.site.domain) def tearDown(self): super(ProgramsApiClientTests, self).tearDown() httpretty.disable() httpretty.reset() def test_get_program(self): """ The method should return data from the Programs API. Data should be cached for subsequent calls. """ program_uuid = uuid.uuid4() data = self.mock_program_detail_endpoint( program_uuid, self.site_configuration.discovery_api_url) self.assertEqual(self.client.get_program(program_uuid), data) # Subsequent calls should pull from the cache httpretty.disable() self.assertEqual(self.client.get_program(program_uuid), data) # Calls from different domains should not pull from cache self.client.site_domain = 'different-domain' with self.assertRaises(ConnectionError): self.client.get_program(program_uuid)
def setUp(self): super(ProgramsApiClientTests, self).setUp() httpretty.enable() self.mock_access_token_response() self.client = ProgramsApiClient( self.site.siteconfiguration.discovery_api_client, self.site.domain)
def save(self, commit=True): program_uuid = self.cleaned_data['program_uuid'] client = ProgramsApiClient(self.request.site.siteconfiguration.course_catalog_api_client) program = client.get_program(program_uuid) offer_name = _('Discount for the {program_title} {program_type} Program'.format( program_title=program['title'], program_type=program['type'] )) self.instance.name = offer_name self.instance.status = ConditionalOffer.OPEN self.instance.offer_type = ConditionalOffer.SITE self.instance.max_basket_applications = 1 if commit: benefit = getattr(self.instance, 'benefit', Benefit()) benefit.proxy_class = class_path(BENEFIT_MAP[self.cleaned_data['benefit_type']]) benefit.value = self.cleaned_data['benefit_value'] benefit.save() self.instance.benefit = benefit if hasattr(self.instance, 'condition'): self.instance.condition.program_uuid = program_uuid self.instance.condition.save() else: self.instance.condition = create_condition(ProgramCourseRunSeatsCondition, program_uuid=program_uuid) return super(ProgramOfferForm, self).save(commit)
def setUp(self): super(ProgramsApiClientTests, self).setUp() httpretty.enable() self.mock_access_token_response() self.client = ProgramsApiClient( self.site.siteconfiguration.course_catalog_api_client)
def get_program_details(self, program_uuid): details = { 'title': '(unknown)', 'uuid': program_uuid, } try: programs_api_client = ProgramsApiClient( self.request.site.siteconfiguration.course_catalog_api_client) details = programs_api_client.get_program(program_uuid) except: # pylint: disable=bare-except logger.exception( 'Failed to retrieve program [%s] from the Programs API!', program_uuid) return details
def get_program(self, site_configuration): """ Returns details for the program associated with this condition. Data is retrieved from the Catalog Service, and cached for ``settings.PROGRAM_CACHE_TIMEOUT`` seconds. Args: site_configuration (SiteConfiguration): Configuration containing the requisite parameters to connect to the Catalog Service. Returns: dict """ program_uuid = str(self.program_uuid) client = ProgramsApiClient(site_configuration.discovery_api_client, site_configuration.site.domain) return client.get_program(program_uuid)
def save(self, commit=True): program_uuid = self.cleaned_data['program_uuid'] site = self.request.site current_date = str(datetime.today().strftime('%Y-%m-%d')) client = ProgramsApiClient(site.siteconfiguration.discovery_api_client, site.domain) program = client.get_program(program_uuid) offer_name = _( u'{current_date} Discount for the {program_title} {program_type} Program' .format(current_date=current_date, program_title=program['title'], program_type=program['type'])) # Truncate offer_names down to 128 characters, as Oscar's AbstractConditionalOffer name is max_length 128 offer_name = (offer_name[:125] + '...') if len(offer_name) > 128 else offer_name # pylint: disable=unsubscriptable-object self.instance.name = offer_name self.instance.status = ConditionalOffer.OPEN self.instance.offer_type = ConditionalOffer.SITE self.instance.max_basket_applications = 1 self.instance.partner = site.siteconfiguration.partner if commit: benefit = getattr(self.instance, 'benefit', Benefit()) benefit.proxy_class = class_path( BENEFIT_MAP[self.cleaned_data['benefit_type']]) benefit.value = self.cleaned_data['benefit_value'] benefit.save() self.instance.benefit = benefit if hasattr(self.instance, 'condition'): self.instance.condition.program_uuid = program_uuid self.instance.condition.save() else: self.instance.condition = create_condition( ProgramCourseRunSeatsCondition, program_uuid=program_uuid) return super(ProgramOfferForm, self).save(commit)
def get_program(program_uuid, siteconfiguration): """ Returns details for the program identified by the program_uuid. Data is retrieved from the Discovery Service, and cached for ``settings.PROGRAM_CACHE_TIMEOUT`` seconds. Args: siteconfiguration (SiteConfiguration): Configuration containing the requisite parameters to connect to the Discovery Service. program_uuid (uuid): id to query the specified program Returns: dict None if not found or another error occurs """ response = None try: client = ProgramsApiClient(siteconfiguration) response = client.get_program(str(program_uuid)) except (ReqConnectionError, HTTPError, Timeout): log.debug("Failed to retrieve program details for %s", program_uuid) return response
class ProgramsApiClientTests(ProgramTestMixin, TestCase): def setUp(self): super(ProgramsApiClientTests, self).setUp() httpretty.enable() self.mock_access_token_response() self.client = ProgramsApiClient( self.site.siteconfiguration.course_catalog_api_client) def tearDown(self): super(ProgramsApiClientTests, self).tearDown() httpretty.disable() httpretty.reset() def test_get_program(self): """ The method should return data from the Programs API. Data should be cached for subsequent calls. """ self.mock_access_token_response() program_uuid = uuid.uuid4() data = self.mock_program_detail_endpoint(program_uuid) self.assertEqual(self.client.get_program(program_uuid), data) # Subsequent calls should pull from the cache httpretty.disable() self.assertEqual(self.client.get_program(program_uuid), data)