Commit 46671d8e by muhammad-ammar

address feedback

parent b95f6a07
...@@ -148,7 +148,9 @@ def get_course_video_image_url(course_id, edx_video_id): ...@@ -148,7 +148,9 @@ def get_course_video_image_url(course_id, edx_video_id):
Returns course video image url or None if no image found Returns course video image url or None if no image found
""" """
try: try:
video_image = CourseVideo.objects.get(course_id=course_id, video__edx_video_id=edx_video_id).video_image video_image = CourseVideo.objects.select_related('video_image').get(
course_id=course_id, video__edx_video_id=edx_video_id
).video_image
return video_image.image_url() return video_image.image_url()
except ObjectDoesNotExist: except ObjectDoesNotExist:
return None return None
...@@ -168,7 +170,9 @@ def update_video_image(edx_video_id, course_id, image_data, file_name): ...@@ -168,7 +170,9 @@ def update_video_image(edx_video_id, course_id, image_data, file_name):
Raises ValVideoNotFoundError if the CourseVideo cannot be retrieved. Raises ValVideoNotFoundError if the CourseVideo cannot be retrieved.
""" """
try: try:
course_video = CourseVideo.objects.get(course_id=course_id, video__edx_video_id=edx_video_id) course_video = CourseVideo.objects.select_related('video').get(
course_id=course_id, video__edx_video_id=edx_video_id
)
except ObjectDoesNotExist: except ObjectDoesNotExist:
error_message = u'CourseVideo not found for edx_video_id: {0}'.format(edx_video_id) error_message = u'CourseVideo not found for edx_video_id: {0}'.format(edx_video_id)
raise ValVideoNotFoundError(error_message) raise ValVideoNotFoundError(error_message)
...@@ -482,13 +486,13 @@ def copy_course_videos(source_course_id, destination_course_id): ...@@ -482,13 +486,13 @@ def copy_course_videos(source_course_id, destination_course_id):
) )
for course_video in course_videos: for course_video in course_videos:
dest_course_video, __ = CourseVideo.objects.get_or_create( destination_course_video, __ = CourseVideo.objects.get_or_create(
video=course_video.video, video=course_video.video,
course_id=destination_course_id course_id=destination_course_id
) )
try: try:
VideoImage.create_or_update( VideoImage.create_or_update(
course_video=dest_course_video, course_video=destination_course_video,
file_name=course_video.video_image.image.name file_name=course_video.video_image.image.name
) )
except VideoImage.DoesNotExist: except VideoImage.DoesNotExist:
...@@ -509,12 +513,12 @@ def export_to_xml(edx_video_id, course_id=None): ...@@ -509,12 +513,12 @@ def export_to_xml(edx_video_id, course_id=None):
Raises: Raises:
ValVideoNotFoundError: if the video does not exist ValVideoNotFoundError: if the video does not exist
""" """
image = '' video_image_name = ''
video = _get_video(edx_video_id) video = _get_video(edx_video_id)
try: try:
course_video = CourseVideo.objects.select_related('video_image').get(course_id=course_id, video=video) course_video = CourseVideo.objects.select_related('video_image').get(course_id=course_id, video=video)
image = course_video.video_image.image.name video_image_name = course_video.video_image.image.name
except ObjectDoesNotExist: except ObjectDoesNotExist:
pass pass
...@@ -523,7 +527,7 @@ def export_to_xml(edx_video_id, course_id=None): ...@@ -523,7 +527,7 @@ def export_to_xml(edx_video_id, course_id=None):
attrib={ attrib={
'client_video_id': video.client_video_id, 'client_video_id': video.client_video_id,
'duration': unicode(video.duration), 'duration': unicode(video.duration),
'image': image 'image': video_image_name
} }
) )
for encoded_video in video.encoded_videos.all(): for encoded_video in video.encoded_videos.all():
......
...@@ -808,7 +808,9 @@ class TestCopyCourse(TestCase): ...@@ -808,7 +808,9 @@ class TestCopyCourse(TestCase):
CourseVideo.objects.create(video=self.video3, course_id=self.course_id2) CourseVideo.objects.create(video=self.video3, course_id=self.course_id2)
def test_successful_copy(self): def test_successful_copy(self):
"""Tests a successful copy course""" """
Tests a successful copy course
"""
destination_course_id = 'course-copy1' destination_course_id = 'course-copy1'
api.copy_course_videos(self.course_id, destination_course_id) api.copy_course_videos(self.course_id, destination_course_id)
original_videos = Video.objects.filter(courses__course_id=self.course_id) original_videos = Video.objects.filter(courses__course_id=self.course_id)
...@@ -957,7 +959,7 @@ class ImportTest(TestCase): ...@@ -957,7 +959,7 @@ class ImportTest(TestCase):
CourseVideo.objects.create(video=video, course_id='existing_course_id') CourseVideo.objects.create(video=video, course_id='existing_course_id')
def make_import_xml(self, video_dict, encoded_video_dicts=None, image=None): def make_import_xml(self, video_dict, encoded_video_dicts=None, image=None):
ret = etree.Element( import_xml = etree.Element(
"video_asset", "video_asset",
attrib={ attrib={
key: unicode(video_dict[key]) key: unicode(video_dict[key])
...@@ -966,18 +968,18 @@ class ImportTest(TestCase): ...@@ -966,18 +968,18 @@ class ImportTest(TestCase):
) )
if image: if image:
ret.attrib['image'] = image import_xml.attrib['image'] = image
for encoding_dict in (encoded_video_dicts or []): for encoding_dict in (encoded_video_dicts or []):
etree.SubElement( etree.SubElement(
ret, import_xml,
"encoded_video", "encoded_video",
attrib={ attrib={
key: unicode(val) key: unicode(val)
for key, val in encoding_dict.items() for key, val in encoding_dict.items()
} }
) )
return ret return import_xml
def assert_obj_matches_dict_for_keys(self, obj, dict_, keys): def assert_obj_matches_dict_for_keys(self, obj, dict_, keys):
for key in keys: for key in keys:
...@@ -1271,6 +1273,22 @@ class CourseVideoImageTest(TestCase): ...@@ -1271,6 +1273,22 @@ class CourseVideoImageTest(TestCase):
image_url = api.get_course_video_image_url(self.course_id, self.edx_video_id) image_url = api.get_course_video_image_url(self.course_id, self.edx_video_id)
self.assertIsNone(image_url) self.assertIsNone(image_url)
def test_num_queries_update_video_image(self):
"""
Test number of queries executed to upload a course video image.
"""
with self.assertNumQueries(4):
api.update_video_image(
self.edx_video_id, self.course_id, ImageFile(open(self.image_path1)), 'image.jpg'
)
def test_num_queries_get_course_video_image_url(self):
"""
Test number of queries executed to get a course video image url.
"""
with self.assertNumQueries(1):
api.get_course_video_image_url(self.course_id, self.edx_video_id)
def test_get_videos_for_course(self): def test_get_videos_for_course(self):
""" """
Verify that `get_videos_for_course` api function has correct course_video_image_url. Verify that `get_videos_for_course` api function has correct course_video_image_url.
......
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