Commit 86fbff5b by Clinton Blackburn

Course images pushed to Studio only if they exist

If a course has no associated image, a warning will be logged rather than attempting to access a non-existent file.

EDUCATOR-1462
parent 5e3d372a
......@@ -80,8 +80,18 @@ class StudioAPI:
return self._api.course_runs.post(data)
def update_course_run_image_in_studio(self, publisher_course_run):
files = {'card_image': publisher_course_run.course.image}
return self._api.course_runs(publisher_course_run.lms_course_id).images.post(files=files)
course = publisher_course_run.course
image = course.image
if image:
files = {'card_image': image}
return self._api.course_runs(publisher_course_run.lms_course_id).images.post(files=files)
else:
logger.warning(
'Card image for course run [%d] cannot be updated. The related course [%d] has no image defined.',
publisher_course_run.id,
course.id
)
def update_course_run_details_in_studio(self, publisher_course_run):
data = self.generate_data_for_studio_api(publisher_course_run)
......
......@@ -12,8 +12,11 @@ from course_discovery.apps.publisher.choices import PublisherUserRole
from course_discovery.apps.publisher.studio_api_utils import StudioAPI
from course_discovery.apps.publisher.tests.factories import CourseRunFactory, CourseUserRoleFactory
test_data = list(product(range(1, 5), ['1T2017'])) + list(product(range(5, 8), ['2T2017'])) + \
test_data = (
list(product(range(1, 5), ['1T2017'])) +
list(product(range(5, 8), ['2T2017'])) +
list(product(range(9, 13), ['3T2017']))
)
@pytest.mark.django_db
......@@ -86,3 +89,17 @@ def test_generate_data_for_studio_api_without_team():
'being created without a course team.',
course_run.course.number
)
@pytest.mark.django_db
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.publisher.studio_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
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment