def test_get_or_create(self, rhead): head_requests = [] def mocked_head(url): head_requests.append(url) if url == 'http://cdn.vidly/file.mp4': return Response('', 302, headers={ 'Content-Type': 'video/mp5', 'Content-Length': '2594479502', }) else: return Response('', 302, headers={ 'Location': 'http://cdn.vidly/file.mp4', }) rhead.side_effect = mocked_head information = VidlyMedia.get_or_create( 'xyz123', 'mp4', True ) eq_(information.tag, 'xyz123') eq_(information.video_format, 'mp4') eq_(information.hd, True) eq_(information.url, 'http://cdn.vidly/file.mp4') eq_(information.content_type, 'video/mp5') eq_(information.size, 2594479502L) assert len(head_requests) == 2 # do it again, same_information = VidlyMedia.get_or_create( 'xyz123', 'mp4', True ) eq_(same_information, information) assert len(head_requests) == 2
def items(self): channels = Channel.objects.filter( Q(id=self.channel.id) | Q(parent=self.channel.id) ) qs = ( Event.objects.archived() .approved() .filter(channels__in=channels) .filter(privacy=Event.PRIVACY_PUBLIC) .filter(template__name__icontains='vid.ly') .filter(template_environment__contains='tag') .exclude(duration__isnull=True) .order_by('-start_time') )[:self.channel.feed_size] all_tag_ids = set() self.all_tags = defaultdict(list) for x in Event.tags.through.objects.filter(event__in=qs): self.all_tags[x.event_id].append(x.tag_id) all_tag_ids.add(x.tag_id) # now `all_tags` is a dict like this: # 123: [45, 67] # where '123' is an event ID, and 45 and 67 are tag IDs # Convert it to something like this: # 123: ['tag1', 'tagX'] tags_qs = ( Tag.objects .filter(id__in=all_tag_ids) .values_list('id', 'name') ) all_tag_names = dict(x for x in tags_qs) for event_id, tag_ids in self.all_tags.items(): self.all_tags[event_id] = [ all_tag_names[x] for x in tag_ids ] items = [] for event in qs: try: vidly_media = VidlyMedia.get_or_create( event.template_environment['tag'], 'mp4', hd=True, ) except vidly.VidlyNotFoundError: print "vidlynotfounderror :(", repr(event) continue event._vidly_media = vidly_media items.append(event) return items
def test_get_or_create_multiples(self): first = VidlyMedia.objects.create( tag='xyz123', hd=True, video_format='mp4', url='http://first.com', size=10000, content_type='video/mp4' ) information = VidlyMedia.get_or_create( 'xyz123', 'mp4', True ) eq_(information, first) # Because the creation of these can happen in non-atomic views # (ie. rendering the itunes feed), you might get a race condition # in creating these so there might be multiples. # The classmethod get_or_create (not to be confused with # objects.get_or_create()) needs to be resilient to this. second = VidlyMedia.objects.create( tag='xyz123', hd=True, video_format='mp4', url='http://second.com', size=10001, content_type='video/mp4' ) information = VidlyMedia.get_or_create( 'xyz123', 'mp4', True ) eq_(information, second)
def test_get_or_create_multiples(self): first = VidlyMedia.objects.create(tag='xyz123', hd=True, video_format='mp4', url='http://first.com', size=10000, content_type='video/mp4') information = VidlyMedia.get_or_create('xyz123', 'mp4', True) eq_(information, first) # Because the creation of these can happen in non-atomic views # (ie. rendering the itunes feed), you might get a race condition # in creating these so there might be multiples. # The classmethod get_or_create (not to be confused with # objects.get_or_create()) needs to be resilient to this. second = VidlyMedia.objects.create(tag='xyz123', hd=True, video_format='mp4', url='http://second.com', size=10001, content_type='video/mp4') information = VidlyMedia.get_or_create('xyz123', 'mp4', True) eq_(information, second)
def test_get_or_create(self, rhead): head_requests = [] def mocked_head(url): head_requests.append(url) if url == 'http://cdn.vidly/file.mp4': return Response('', 302, headers={ 'Content-Type': 'video/mp5', 'Content-Length': '2594479502', }) else: return Response('', 302, headers={ 'Location': 'http://cdn.vidly/file.mp4', }) rhead.side_effect = mocked_head information = VidlyMedia.get_or_create('xyz123', 'mp4', True) eq_(information.tag, 'xyz123') eq_(information.video_format, 'mp4') eq_(information.hd, True) eq_(information.url, 'http://cdn.vidly/file.mp4') eq_(information.content_type, 'video/mp5') eq_(information.size, 2594479502L) assert len(head_requests) == 2 # do it again, same_information = VidlyMedia.get_or_create('xyz123', 'mp4', True) eq_(same_information, information) assert len(head_requests) == 2