Commit 01e9f689 by David Ormsbee

Merge pull request #26 from edx/dcs/youtube-lookup

Lookup youtube videos by id
parents 22bef6f9 65349b38
......@@ -32,7 +32,7 @@ from django.core.validators import MinValueValidator, RegexValidator
from django.core.urlresolvers import reverse
url_regex = r'^[a-zA-Z0-9\-]*$'
url_regex = r'^[a-zA-Z0-9\-_]*$'
class Profile(models.Model):
......@@ -60,6 +60,7 @@ class Profile(models.Model):
def __unicode__(self):
return self.profile_name
class Video(models.Model):
"""
Model for a Video group with the same content.
......@@ -87,6 +88,18 @@ class Video(models.Model):
def __str__(self):
return self.edx_video_id
@classmethod
def by_youtube_id(cls, youtube_id):
"""
Look up video by youtube id
"""
url = '://youtu.be/%s' % youtube_id
qset = cls.objects.filter(
encoded_videos__profile__profile_name='youtube',
encoded_videos__url__endswith=url
).prefetch_related('encoded_videos', 'courses', 'subtitles')
return qset
class CourseVideo(models.Model):
"""
......
......@@ -325,6 +325,10 @@ class VideoListTest(APIAuthTestCase):
Profile.objects.create(**constants.PROFILE_DICT_DESKTOP)
super(VideoListTest, self).setUp()
def tearDown(self):
super(VideoListTest, self).tearDown()
Video.objects.all().delete()
# Tests for successful POST 201 requests.
def test_complete_set_two_encoded_video_post(self):
"""
......@@ -494,15 +498,45 @@ class VideoListTest(APIAuthTestCase):
self.assertEqual(len(videos), 1)
self.assertEqual(videos[0]['courses'], [course1, course2])
url = reverse('course-video-list', kwargs={'course_id': course1})
url = reverse('video-list') + '?course=%s' % course1
videos = self.client.get(url).data
self.assertEqual(len(videos), 1)
self.assertEqual(videos[0]['edx_video_id'], constants.VIDEO_DICT_ANIMAL['edx_video_id'])
url = reverse('course-video-list', kwargs={'course_id': 'animals/fish/salmon'})
url = reverse('video-list') + '?course=animals/fish/salmon'
response = self.client.get(url).data
self.assertEqual(len(response), 0)
def test_lookup_youtube(self):
"""
Test looking up by youtube id
"""
video = {
'edx_video_id': 'testing-youtube',
'encoded_videos': [
{
'profile': 'youtube',
'url': 'https://youtu.be/AbcDef',
'file_size': 4545,
'bitrate': 6767,
}
],
'subtitles': [],
'courses': ['youtube'],
'client_video_id': "Funny Cats",
'duration': 122
}
response = self.client.post(reverse('video-list'), video, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
# now look up the vid by youtube id
url = reverse('video-list') + '?youtube=AbcDef'
response = self.client.get(url).data
self.assertEqual(len(response), 1)
self.assertEqual(response[0]['edx_video_id'], video['edx_video_id'])
# Tests for POST queries to database
def test_queries_for_only_video(self):
......
......@@ -29,10 +29,4 @@ urlpatterns = patterns(
views.get_subtitle,
name="subtitle-content"
),
url(
r'^courses/{}$'.format(settings.COURSE_ID_PATTERN),
views.CourseVideoList.as_view(),
name="course-video-list"
),
)
......@@ -58,16 +58,19 @@ class VideoList(generics.ListCreateAPIView):
lookup_field = "edx_video_id"
serializer_class = VideoSerializer
class CourseVideoList(generics.ListAPIView):
authentication_classes = (OAuth2Authentication, SessionAuthentication)
permission_classes = (ReadRestrictedDjangoModelPermissions,)
queryset = Video.objects.all().prefetch_related("encoded_videos")
lookup_field = "course_id"
serializer_class = VideoSerializer
def get_queryset(self):
return self.queryset.filter(courses__course_id=self.kwargs['course_id'])
qset = Video.objects.all().prefetch_related("encoded_videos", "courses")
args = self.request.GET
course_id = args.get('course')
if course_id:
# view videos by course id
qset = qset.filter(courses__course_id=course_id)
youtube_id = args.get('youtube')
if youtube_id:
# view videos by youtube id
qset = qset & Video.by_youtube_id(youtube_id)
return qset
class ProfileList(generics.ListCreateAPIView):
......
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