Commit 04bd5191 by Nimisha Asthagiri

MA-410 Add course_id on import for existing VAL entry.

parent cb9cf1a3
......@@ -428,21 +428,31 @@ def import_from_xml(xml, edx_video_id, course_id=None):
if xml.tag != 'video_asset':
raise ValCannotCreateError('Invalid XML')
if Video.objects.filter(edx_video_id=edx_video_id).exists():
# If video with edx_video_id already exists, associate it with the given course_id.
try:
video = Video.objects.get(edx_video_id=edx_video_id)
logger.info(
"edx_video_id '%s' present in course '%s' not imported because it exists in VAL.",
edx_video_id,
course_id,
)
if course_id:
CourseVideo.create_with_validation(video=video, course_id=course_id)
return
except ValidationError as err:
logger.exception(err.message)
raise ValCannotCreateError(err.message_dict)
except Video.DoesNotExist:
pass
# Video with edx_video_id did not exist, so create one from xml data.
data = {
'edx_video_id': edx_video_id,
'client_video_id': xml.get('client_video_id'),
'duration': xml.get('duration'),
'status': 'imported',
'encoded_videos': [],
'courses': [],
'courses': [course_id] if course_id else [],
}
for encoded_video_el in xml.iterfind('encoded_video'):
profile_name = encoded_video_el.get('profile')
......@@ -461,7 +471,4 @@ def import_from_xml(xml, edx_video_id, course_id=None):
'file_size': encoded_video_el.get('file_size'),
'bitrate': encoded_video_el.get('bitrate'),
})
if course_id:
data['courses'].append(course_id)
create_video(data)
......@@ -19,6 +19,20 @@ from django.core.urlresolvers import reverse
URL_REGEX = r'^[a-zA-Z0-9\-_]*$'
class ModelFactoryWithValidation(object):
"""
A Model mixin that provides validation-based factory methods.
"""
@classmethod
def create_with_validation(cls, *args, **kwargs):
"""
Factory method that creates and validates the model object before it is saved.
"""
ret_val = cls(*args, **kwargs)
ret_val.full_clean()
ret_val.save()
class Profile(models.Model):
"""
Details for pre-defined encoding format
......@@ -90,7 +104,7 @@ class Video(models.Model):
return qset
class CourseVideo(models.Model):
class CourseVideo(models.Model, ModelFactoryWithValidation):
"""
Model for the course_id associated with the video content.
......
......@@ -83,6 +83,7 @@ class CourseSerializer(serializers.RelatedField):
course_video.full_clean(exclude=["video"])
return course_video
class VideoSerializer(serializers.ModelSerializer):
"""
Serializer for Video object
......
......@@ -860,7 +860,12 @@ class ImportTest(TestCase):
self.assertFalse(
video.encoded_videos.filter(profile__profile_name=constants.PROFILE_DESKTOP).exists()
)
self.assertFalse(video.courses.filter(course_id=new_course_id).exists())
self.assertTrue(video.courses.filter(course_id=new_course_id).exists())
def test_existing_video_with_invalid_course_id(self):
xml = self.make_import_xml(video_dict=constants.VIDEO_DICT_FISH)
with self.assertRaises(ValCannotCreateError):
api.import_from_xml(xml, edx_video_id=constants.VIDEO_DICT_FISH["edx_video_id"], course_id="x" * 300)
def test_unknown_profile(self):
profile = "unknown_profile"
......
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