Commit 66c86483 by Michael Frey Committed by GitHub

Merge pull request #246 from edx/mjfrey/course_run_image

Create course run image from course api
parents 1f8b7db9 892159d2
...@@ -6,7 +6,7 @@ from opaque_keys.edx.keys import CourseKey ...@@ -6,7 +6,7 @@ from opaque_keys.edx.keys import CourseKey
from course_discovery.apps.core.models import Currency from course_discovery.apps.core.models import Currency
from course_discovery.apps.course_metadata.data_loaders import AbstractDataLoader from course_discovery.apps.course_metadata.data_loaders import AbstractDataLoader
from course_discovery.apps.course_metadata.models import ( 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__) logger = logging.getLogger(__name__)
...@@ -122,9 +122,22 @@ class CoursesApiDataLoader(AbstractDataLoader): ...@@ -122,9 +122,22 @@ class CoursesApiDataLoader(AbstractDataLoader):
'video': self.get_courserun_video(body), 'video': self.get_courserun_video(body),
'pacing_type': self.get_pacing_type(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) 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): def get_pacing_type(self, body):
pacing = body.get('pacing') pacing = body.get('pacing')
......
...@@ -126,7 +126,7 @@ class CoursesApiDataLoaderTests(ApiClientTestMixin, DataLoaderTestMixin, TestCas ...@@ -126,7 +126,7 @@ class CoursesApiDataLoaderTests(ApiClientTestMixin, DataLoaderTestMixin, TestCas
) )
return bodies 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. """ """ Assert a CourseRun corresponding to the specified data body was properly loaded into the database. """
# Validate the Course # Validate the Course
...@@ -148,11 +148,19 @@ class CoursesApiDataLoaderTests(ApiClientTestMixin, DataLoaderTestMixin, TestCas ...@@ -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.enrollment_end, AbstractDataLoader.parse_date(body['enrollment_end']))
self.assertEqual(course_run.pacing_type, self.loader.get_pacing_type(body)) self.assertEqual(course_run.pacing_type, self.loader.get_pacing_type(body))
self.assertEqual(course_run.video, self.loader.get_courserun_video(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 @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. """ """ Verify the method ingests data from the Courses API. """
api_data = self.mock_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(Course.objects.count(), 0)
self.assertEqual(CourseRun.objects.count(), 0) self.assertEqual(CourseRun.objects.count(), 0)
...@@ -166,7 +174,7 @@ class CoursesApiDataLoaderTests(ApiClientTestMixin, DataLoaderTestMixin, TestCas ...@@ -166,7 +174,7 @@ class CoursesApiDataLoaderTests(ApiClientTestMixin, DataLoaderTestMixin, TestCas
self.assertEqual(CourseRun.objects.count(), expected_num_course_runs) self.assertEqual(CourseRun.objects.count(), expected_num_course_runs)
for datum in api_data: 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. # Verify multiple calls to ingest data do NOT result in data integrity errors.
self.loader.ingest() self.loader.ingest()
......
...@@ -53,7 +53,7 @@ COURSES_API_BODIES = [ ...@@ -53,7 +53,7 @@ COURSES_API_BODIES = [
}, },
'course_video': { 'course_video': {
'uri': None 'uri': None
} },
}, },
'name': 'Evolution of the Human Sociality: A Quest for the Origin of Our Social Behavior', 'name': 'Evolution of the Human Sociality: A Quest for the Origin of Our Social Behavior',
'number': '000x', 'number': '000x',
...@@ -76,7 +76,11 @@ COURSES_API_BODIES = [ ...@@ -76,7 +76,11 @@ COURSES_API_BODIES = [
}, },
'course_video': { 'course_video': {
'uri': None 'uri': None
} },
'image': {
'raw': 'http://example.com/image.jpg',
},
}, },
'name': 'Evolution of the Human Sociality: A Quest for the Origin of Our Social Behavior', 'name': 'Evolution of the Human Sociality: A Quest for the Origin of Our Social Behavior',
'number': '000x', '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