Commit 892159d2 by Michael Frey

Create course run image from course api if no marketing site exists for partner

parent 1f8b7db9
......@@ -6,7 +6,7 @@ from opaque_keys.edx.keys import CourseKey
from course_discovery.apps.core.models import Currency
from course_discovery.apps.course_metadata.data_loaders import AbstractDataLoader
from course_discovery.apps.course_metadata.models import (
Video, Organization, Seat, CourseRun, Program, Course, CourseOrganization, ProgramType,
Image, Video, Organization, Seat, CourseRun, Program, Course, CourseOrganization, ProgramType,
)
logger = logging.getLogger(__name__)
......@@ -122,9 +122,22 @@ class CoursesApiDataLoader(AbstractDataLoader):
'video': self.get_courserun_video(body),
'pacing_type': self.get_pacing_type(body),
}
# If there is no marketing site setup for this partner, use the image from the course API.
# If there is a marketing site defined, it takes prededence.
if not self.partner.marketing_site_url_root:
defaults.update({'image': self.get_courserun_image(body)})
CourseRun.objects.update_or_create(key=body['id'], defaults=defaults)
def get_courserun_image(self, body):
image = None
image_url = body['media'].get('image', {}).get('raw')
if image_url:
image, __ = Image.objects.get_or_create(src=image_url)
return image
def get_pacing_type(self, body):
pacing = body.get('pacing')
......
......@@ -126,7 +126,7 @@ class CoursesApiDataLoaderTests(ApiClientTestMixin, DataLoaderTestMixin, TestCas
)
return bodies
def assert_course_run_loaded(self, body):
def assert_course_run_loaded(self, body, use_marketing_url=True):
""" Assert a CourseRun corresponding to the specified data body was properly loaded into the database. """
# Validate the Course
......@@ -148,11 +148,19 @@ class CoursesApiDataLoaderTests(ApiClientTestMixin, DataLoaderTestMixin, TestCas
self.assertEqual(course_run.enrollment_end, AbstractDataLoader.parse_date(body['enrollment_end']))
self.assertEqual(course_run.pacing_type, self.loader.get_pacing_type(body))
self.assertEqual(course_run.video, self.loader.get_courserun_video(body))
if use_marketing_url:
self.assertEqual(course_run.image, None)
else:
self.assertEqual(course_run.image, self.loader.get_courserun_image(body))
@responses.activate
def test_ingest(self):
@ddt.data(True, False)
def test_ingest(self, use_marketing_url):
""" Verify the method ingests data from the Courses API. """
api_data = self.mock_api()
if not use_marketing_url:
self.partner.marketing_site_url_root = None
self.assertEqual(Course.objects.count(), 0)
self.assertEqual(CourseRun.objects.count(), 0)
......@@ -166,7 +174,7 @@ class CoursesApiDataLoaderTests(ApiClientTestMixin, DataLoaderTestMixin, TestCas
self.assertEqual(CourseRun.objects.count(), expected_num_course_runs)
for datum in api_data:
self.assert_course_run_loaded(datum)
self.assert_course_run_loaded(datum, use_marketing_url)
# Verify multiple calls to ingest data do NOT result in data integrity errors.
self.loader.ingest()
......
......@@ -53,7 +53,7 @@ COURSES_API_BODIES = [
},
'course_video': {
'uri': None
}
},
},
'name': 'Evolution of the Human Sociality: A Quest for the Origin of Our Social Behavior',
'number': '000x',
......@@ -76,7 +76,11 @@ COURSES_API_BODIES = [
},
'course_video': {
'uri': None
}
},
'image': {
'raw': 'http://example.com/image.jpg',
},
},
'name': 'Evolution of the Human Sociality: A Quest for the Origin of Our Social Behavior',
'number': '000x',
......
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