def test_update_course_run_image_in_studio_without_course_image(): publisher_course_run = CourseRunFactory(course__image=None) api = StudioAPI(None) with mock.patch('course_discovery.apps.api.utils.logger') as mock_logger: api.update_course_run_image_in_studio(publisher_course_run) mock_logger.warning.assert_called_with( 'Card image for course run [%d] cannot be updated. The related course [%d] has no image defined.', publisher_course_run.id, publisher_course_run.course.id)
def create_course_run_in_studio_receiver(sender, instance, created, **kwargs): # pylint: disable=unused-argument if created and waffle.switch_is_active( 'enable_publisher_create_course_run_in_studio'): course = instance.course for organization in course.organizations.all(): try: if not organization.organization_extension.auto_create_in_studio: logger.warning( ('Course run [%d] will not be automatically created in studio.' 'Organization [%s] has opted out of this feature.'), course.id, organization.key, ) return except ObjectDoesNotExist: logger.exception( 'Organization [%s] does not have an associated OrganizationExtension', organization.key, ) partner = course.partner if not partner: logger.error( 'Failed to publish course run [%d] to Studio. Related course [%d] has no associated Partner.', instance.id, course.id) return logger.info('Publishing course run [%d] to Studio...', instance.id) api = StudioAPI(instance.course.partner.studio_api_client) discovery_course_run = instance.discovery_counterpart_latest_by_start_date try: response = api.push_to_studio(instance, create=True, old_course_run=discovery_course_run) except SlumberBaseException as ex: logger.exception('Failed to create course run [%s] on Studio: %s', course.key, ex.content) raise except Exception: # pylint: disable=broad-except logger.exception('Failed to create course run [%s] on Studio', course.key) raise instance.lms_course_id = response['id'] instance.save() logger.info('Completed creation of course run [%s] on Studio.', instance.lms_course_id)
def assert_data_generated_correctly(course_run, expected_team_data): course = course_run.course expected = { 'title': course_run.title_override or course.title, 'org': course.organizations.first().key, 'number': course.number, 'run': StudioAPI.calculate_course_run_key_run_value(course_run), 'schedule': { 'start': serialize_datetime(course_run.start_date_temporary), 'end': serialize_datetime(course_run.end_date_temporary), }, 'team': expected_team_data, 'pacing_type': course_run.pacing_type_temporary, } assert StudioAPI.generate_data_for_studio_api(course_run) == expected
def publish_to_studio(self, partner, course_run): api = StudioAPI(partner.studio_api_client) try: api.push_to_studio(course_run) return self.PUBLICATION_SUCCESS_STATUS except SlumberBaseException as ex: content = ex.content.decode('utf8') logger.exception( 'Failed to publish course run [%d] to Studio! Error was: [%s]', course_run.pk, content) return 'FAILED: ' + content except Exception as ex: # pylint: disable=broad-except logger.exception('Failed to publish course run [%d] to Studio!', course_run.pk) return 'FAILED: ' + str(ex)
def test_calculate_course_run_key_run_value_with_multiple_runs_per_trimester(): number = 'TestX' organization = OrganizationFactory() partner = organization.partner course_key = '{org}+{number}'.format(org=organization.key, number=number) discovery_course = DiscoveryCourseFactory(partner=partner, key=course_key) start = datetime.datetime(2017, 2, 1) DiscoveryCourseRunFactory(key='course-v1:TestX+Testing101x+1T2017', course=discovery_course) assert StudioAPI.calculate_course_run_key_run_value(number, start) == '1T2017a' DiscoveryCourseRunFactory(key='course-v1:TestX+Testing101x+1T2017a', course=discovery_course) assert StudioAPI.calculate_course_run_key_run_value(number, start) == '1T2017b'
def assert_data_generated_correctly(course_run, expected_team_data): course = course_run.course expected = { 'title': course_run.title_override or course.title, 'org': course.organizations.first().key, 'number': course.number, 'run': StudioAPI.calculate_course_run_key_run_value( course.number, course_run.start_date_temporary), 'schedule': { 'start': serialize_datetime(course_run.start_date_temporary), 'end': serialize_datetime(course_run.end_date_temporary), }, 'team': expected_team_data, 'pacing_type': course_run.pacing_type_temporary, } # the publisher djangoapp doesn't care about the 'creating' flag passed below, so we just always set it False assert StudioAPI.generate_data_for_studio_api(course_run, creating=False) == expected
def create_course_run_in_studio_receiver(sender, instance, created, **kwargs): # pylint: disable=unused-argument if created and waffle.switch_is_active( 'enable_publisher_create_course_run_in_studio'): course = instance.course partner = course.partner if not partner: logger.error( 'Failed to publish course run [%d] to Studio. Related course [%d] has no associated Partner.', instance.id, course.id) return logger.info('Publishing course run [%d] to Studio...', instance.id) api = StudioAPI(instance.course.partner.studio_api_client) discovery_course_run = instance.discovery_counterpart_latest_by_start_date try: if discovery_course_run: response = api.push_to_studio( instance, create=True, old_course_run_key=discovery_course_run.key) else: response = api.push_to_studio(instance, create=True) except SlumberBaseException as ex: logger.exception('Failed to create course run [%s] on Studio: %s', course.key, ex.content) raise except Exception: logger.exception('Failed to create course run [%s] on Studio', course.key) raise instance.lms_course_id = response['id'] instance.save() api.update_course_run_image_in_studio(instance, response) logger.info('Completed creation of course run [%s] on Studio.', instance.lms_course_id)
def test_calculate_course_run_key_run_value(month, expected): course = CourseFactory() start = datetime.datetime(2017, month, 1) assert StudioAPI.calculate_course_run_key_run_value( course.number, start=start) == expected
def test_calculate_course_run_key_run_value(month, expected): course_run = CourseRunFactory(start=datetime.datetime(2017, month, 1)) assert StudioAPI.calculate_course_run_key_run_value(course_run) == expected