def test_url_cache(self): test_utils.invalidate_widget_video_cache.run_original_for_test() video = get_video(1) video_url = video.get_video_url() # After adding the video, we should be able to look up its ID in the # cache, given just the URL. cache_id_1 = video_cache.get_video_id(video_url) self.assertIsNotNone(cache_id_1) self.assertTrue(Video.objects.filter(video_id=cache_id_1).exists()) # Remove the video (and make sure it's gone). video.delete() self.assertFalse( Video.objects.filter(videourl__url=video_url).exists()) # Trying to get the video ID out of the cache now actually *creates* the # video! cache_id_2 = video_cache.get_video_id(video_url) self.assertTrue(Video.objects.filter(videourl__url=video_url).exists()) # The video_id will be different than before (since this is a new Video # record) and the cache should have been updated properly. self.assertNotEqual(cache_id_1, cache_id_2) self.assertTrue(Video.objects.filter(video_id=cache_id_2).exists())
def show_widget(self, request, video_url, is_remote, base_state=None, additional_video_urls=None): video_id = video_cache.get_video_id(video_url) if video_id is None: # for example, private youtube video. return None try: video_urls = video_cache.get_video_urls(video_id) except models.Video.DoesNotExist: video_cache.invalidate_video_id(video_url) video_id = video_cache.get_video_id(video_url) video_urls = video_cache.get_video_urls(video_id) return_value = { 'video_id' : video_id, 'subtitles': None, } return_value['video_urls']= video_urls if additional_video_urls is not None: for url in additional_video_urls: video_cache.associate_extra_url(url, video_id) st_widget_view_statistic_update.delay(video_id=video_id) add_general_settings(request, return_value) if request.user.is_authenticated(): return_value['username'] = request.user.username return_value['drop_down_contents'] = \ video_cache.get_video_languages(video_id) if base_state is not None and base_state.get("language_code", None) is not None: lang_pk = base_state.get('language_pk', None) if lang_pk is None: lang_code = base_state.get('language_code', None) lang_pk = video_cache.pk_for_default_language(video_id, lang_code) subtitles = self._autoplay_subtitles( request.user, video_id, lang_pk, base_state.get('revision', None)) return_value['subtitles'] = subtitles else: if is_remote: autoplay_language = self._find_remote_autoplay_language(request) language_pk = video_cache.pk_for_default_language(video_id, autoplay_language) if autoplay_language is not None: subtitles = self._autoplay_subtitles( request.user, video_id, language_pk, None) return_value['subtitles'] = subtitles return return_value
def show_widget(self, request, video_url, is_remote, base_state=None, additional_video_urls=None): video_id = video_cache.get_video_id(video_url) if video_id is None: # for example, private youtube video. return None if additional_video_urls is not None: for url in additional_video_urls: video_cache.associate_extra_url(url, video_id) models.Video.widget_views_counter(video_id).incr() widget_views_total_counter.incr() return_value = { 'video_id': video_id, 'subtitles': None, } add_general_settings(request, return_value) if request.user.is_authenticated(): return_value['username'] = request.user.username return_value['video_urls'] = video_cache.get_video_urls(video_id) return_value['drop_down_contents'] = \ self._drop_down_contents(video_id) if base_state is not None and base_state.get("language_code", None) is not None: lang_pk = base_state.get('language_pk', None) if lang_pk is None: lang_code = base_state.get('language_code', None) language = models.SubtitleLanguage.objects.get( video_id=video_id, language=lang_code) if language: lang_pk = language.pk subtitles = self._autoplay_subtitles( request.user, video_id, lang_pk, base_state.get('revision', None)) return_value['subtitles'] = subtitles else: if is_remote: autoplay_language = self._find_remote_autoplay_language( request) language = models.Video.objects.get( video_id=video_id).subtitle_language() if language is not None: language_pk = language.pk else: language_pk = None if autoplay_language is not None: subtitles = self._autoplay_subtitles( request.user, video_id, language_pk, None) return_value['subtitles'] = subtitles return return_value
def test_missing_lang_no_fail(self): # when sending a nonexisting lang, we should end up with the original lang, since # others might have been cleared and not gotten through the cache # we are asserting this won't raise an exception for https://www.pivotaltracker.com/story/show/15348901 url = "http://videos-cdn.mozilla.net/serv/mozhacks/demos/screencasts/londonproject/screencast.ogv" cache_key = video_cache._video_id_key(url) video_cache.cache.set(cache_key, "", video_cache.TIMEOUT) video_id = video_cache.get_video_id(url) video_cache.get_subtitles_dict(video_id, 0, 0, lambda x: x)
def fetch_video_id_and_settings(self, request, video_url): video_id = video_cache.get_video_id(video_url) is_original_language_subtitled = self._subtitle_count(video_id) > 0 general_settings = {} add_general_settings(request, general_settings) return { 'video_id': video_id, 'is_original_language_subtitled': is_original_language_subtitled, 'general_settings': general_settings }
def show_widget(self, request, video_url, is_remote, base_state=None, additional_video_urls=None): video_id = video_cache.get_video_id(video_url) if video_id is None: # for example, private youtube video. return None try: video_urls = video_cache.get_video_urls(video_id) except models.Video.DoesNotExist: video_cache.invalidate_video_id(video_url) video_id = video_cache.get_video_id(video_url) video_urls = video_cache.get_video_urls(video_id) return_value = {"video_id": video_id, "subtitles": None} return_value["video_urls"] = video_urls return_value["is_moderated"] = video_cache.get_is_moderated(video_id) if additional_video_urls is not None: for url in additional_video_urls: video_cache.associate_extra_url(url, video_id) add_general_settings(request, return_value) if request.user.is_authenticated(): return_value["username"] = request.user.username return_value["drop_down_contents"] = video_cache.get_video_languages(video_id) return_value["my_languages"] = get_user_languages_from_request(request) # keeping both forms valid as backwards compatibility layer lang_code = base_state and base_state.get("language_code", base_state.get("language", None)) if base_state is not None and lang_code is not None: lang_pk = base_state.get("language_pk", None) if lang_pk is None: lang_pk = video_cache.pk_for_default_language(video_id, lang_code) subtitles = self._autoplay_subtitles(request.user, video_id, lang_pk, base_state.get("revision", None)) return_value["subtitles"] = subtitles else: if is_remote: autoplay_language = self._find_remote_autoplay_language(request) language_pk = video_cache.pk_for_default_language(video_id, autoplay_language) if autoplay_language is not None: subtitles = self._autoplay_subtitles(request.user, video_id, language_pk, None) return_value["subtitles"] = subtitles return return_value
def test_empty_id_show_widget(self): url = "http://videos-cdn.mozilla.net/serv/mozhacks/demos/screencasts/londonproject/screencast.ogv" cache_key = video_cache._video_id_key(url) video, create = Video.get_or_create_for_url(url) video_cache.cache.set(cache_key, "", video_cache.TIMEOUT) # we have a bogus url video_id = video_cache.get_video_id(url) self.assertTrue(bool(video_id)) try: Video.objects.get(video_id=video_id) except Video.DoesNotExist: self.fail("Should not point to a non existing video")
def show_widget(self, request, video_url, is_remote, base_state=None, additional_video_urls=None): video_id = video_cache.get_video_id(video_url) if video_id is None: # for example, private youtube video. return None if additional_video_urls is not None: for url in additional_video_urls: video_cache.associate_extra_url(url, video_id) models.Video.widget_views_counter(video_id).incr() widget_views_total_counter.incr() return_value = { 'video_id' : video_id, 'subtitles': None, } add_general_settings(request, return_value) if request.user.is_authenticated(): return_value['username'] = request.user.username return_value['video_urls'] = video_cache.get_video_urls(video_id) return_value['drop_down_contents'] = \ self._drop_down_contents(video_id) if base_state is not None and base_state.get("language_code", None) is not None: lang_pk = base_state.get('language_pk', None) if lang_pk is None: lang_code = base_state.get('language_code', None) language = models.SubtitleLanguage.objects.get(video_id=video_id,language=lang_code) if language: lang_pk = language.pk subtitles = self._autoplay_subtitles( request.user, video_id, lang_pk, base_state.get('revision', None)) return_value['subtitles'] = subtitles else: if is_remote: autoplay_language = self._find_remote_autoplay_language(request) language = models.Video.objects.get(video_id=video_id).subtitle_language() if language is not None: language_pk = language.pk else: language_pk = None if autoplay_language is not None: subtitles = self._autoplay_subtitles( request.user, video_id, language_pk, None) return_value['subtitles'] = subtitles return return_value
def show_widget(self, request, video_url, is_remote, base_state=None, additional_video_urls=None): try: video_id = video_cache.get_video_id(video_url) except Exception as e: # for example, private youtube video or private widgets return {"error_msg": unicode(e)} if video_id is None: return None error = self._check_visibility_policy_for_widget(request, video_id) if error: return error video_urls, video_id, error = self._get_video_urls_for_widget( video_url, video_id) if error: return error resp = { 'video_id': video_id, 'subtitles': None, 'video_urls': video_urls, 'is_moderated': video_cache.get_is_moderated(video_id), 'filename': video_cache.get_download_filename(video_id), } if additional_video_urls is not None: for url in additional_video_urls: video_cache.associate_extra_url(url, video_id) add_general_settings(request, resp) if request.user.is_authenticated(): resp['username'] = request.user.username resp['drop_down_contents'] = video_cache.get_video_languages(video_id) resp['my_languages'] = get_user_languages_from_request(request) resp['subtitles'] = self._get_subtitles_for_widget( request, base_state, video_id, is_remote) return resp
def _get_video_urls_for_widget(self, video_url, video_id): """Return the video URLs, 'cleaned' video id, and error.""" try: video_urls = video_cache.get_video_urls(video_id) except models.Video.DoesNotExist: video_cache.invalidate_video_id(video_url) try: video_id = video_cache.get_video_id(video_url) except Exception as e: return None, None, {"error_msg": unicode(e)} video_urls = video_cache.get_video_urls(video_id) return video_urls, video_id, None
def test_cache_delete_valid_chars(self): # this tests depends on memcache being available try: from memcache.Client import MemcachedKeyCharacterError except ImportError: return request = RequestMockup(self.user_0) session = create_two_sub_session(request) video = session.video # make sure we have video on cache video_id = video_cache.get_video_id(video.get_absolute_url(video.get_video_url())) self.assertEquals(video_id, video.video_id) self.assertTrue(bool(video_id)) try: video_cache.invalidate_cache(video_id) except MemcachedKeyCharacterError: self.fail("Cache invalidation should not fail")
def test_video_id_not_empty_string(self): url = "http://videos-cdn.mozilla.net/serv/mozhacks/demos/screencasts/londonproject/screencast.ogv" cache_key = video_cache._video_id_key(url) video_cache.cache.set(cache_key, "", video_cache.TIMEOUT) video_id = video_cache.get_video_id(url) self.assertTrue(bool(video_id))