Ejemplo n.º 1
0
    def get_old(self):
        """ Deprecated old version of search, so we can Gandalf in the new one.

        If new search is working, this should be taken out by May 31, 2012.
        """

        show_update = False
        if App.is_dev_server and user_util.is_current_user_admin():
            update = self.request_bool("update", False)
            if update:
                self.update()

            version_number = layer_cache.KeyValueCache.get(
                "last_dev_topic_vesion_indexed")
            default_version = topic_models.TopicVersion.get_default_version()
            if version_number != default_version.number:
                show_update = True

        query = self.request.get('page_search_query')
        template_values = {'page_search_query': query}
        query = query.strip()
        if len(query) < search.SEARCH_PHRASE_MIN_LENGTH:
            if len(query) > 0:
                template_values.update(
                    {'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH})
            self.render_jinja2_template("searchresults.html", template_values)
            return
        searched_phrases = []

        # Do an async query for all ExerciseVideos, since this may be slow
        exvids_query = ExerciseVideo.all()
        exvids_future = util.async_queries([exvids_query])

        # One full (non-partial) search, then sort by kind
        all_text_keys = Topic.full_text_search(
            query,
            limit=50,
            kind=None,
            stemming=Topic.INDEX_STEMMING,
            multi_word_literal=Topic.INDEX_MULTI_WORD,
            searched_phrases_out=searched_phrases)

        # Quick title-only partial search
        topic_partial_results = filter(
            lambda topic_dict: query in topic_dict["title"].lower(),
            autocomplete.topic_title_dicts())
        video_partial_results = filter(
            lambda video_dict: query in video_dict["title"].lower(),
            autocomplete.video_title_dicts())
        url_partial_results = filter(
            lambda url_dict: query in url_dict["title"].lower(),
            autocomplete.url_title_dicts())

        # Combine results & do one big get!
        all_keys = [str(key_and_title[0]) for key_and_title in all_text_keys]
        all_keys.extend([result["key"] for result in topic_partial_results])
        all_keys.extend([result["key"] for result in video_partial_results])
        all_keys.extend([result["key"] for result in url_partial_results])
        all_keys = list(set(all_keys))

        # Filter out anything that isn't a Topic, Url or Video
        all_keys = [
            key for key in all_keys
            if db.Key(key).kind() in ["Topic", "Url", "Video"]
        ]

        # Get all the entities
        all_entities = db.get(all_keys)

        # Group results by type
        topics = []
        videos = []
        for entity in all_entities:
            if isinstance(entity, Topic):
                topics.append(entity)
            elif isinstance(entity, Video):
                videos.append(entity)
            elif isinstance(entity, Url):
                videos.append(entity)
            elif entity:
                logging.info("Found unknown object " + repr(entity))

        # Get topics for videos not in matching topics
        filtered_videos = []
        filtered_videos_by_key = {}
        for video in videos:
            if [(str(topic.key()) in video.topic_string_keys)
                    for topic in topics].count(True) == 0:
                video_topic = video.first_topic()
                if video_topic != None:
                    topics.append(video_topic)
                    filtered_videos.append(video)
                    filtered_videos_by_key[str(video.key())] = []
            else:
                filtered_videos.append(video)
                filtered_videos_by_key[str(video.key())] = []
        video_count = len(filtered_videos)

        # Get the related exercises
        all_exercise_videos = exvids_future[0].get_result()
        exercise_keys = []
        for exvid in all_exercise_videos:
            video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid))
            if video_key in filtered_videos_by_key:
                exercise_key = ExerciseVideo.exercise.get_value_for_datastore(
                    exvid)
                video_exercise_keys = filtered_videos_by_key[video_key]
                video_exercise_keys.append(exercise_key)
                exercise_keys.append(exercise_key)
        exercises = db.get(exercise_keys)

        # Sort exercises with videos
        video_exercises = {}
        for video_key, exercise_keys in filtered_videos_by_key.iteritems():
            video_exercises[video_key] = map(
                lambda exkey: [
                    exercise for exercise in exercises
                    if exercise.key() == exkey
                ][0], exercise_keys)

        # Count number of videos in each topic and sort descending
        topic_count = 0
        matching_topic_count = 0
        if topics:
            if len(filtered_videos) > 0:
                for topic in topics:
                    topic.match_count = [
                        (str(topic.key()) in video.topic_string_keys)
                        for video in filtered_videos
                    ].count(True)
                    if topic.match_count > 0:
                        topic_count += 1

                topics = sorted(topics,
                                key=lambda topic: topic.match_count,
                                reverse=True)
            else:
                for topic in topics:
                    topic.match_count = 0

            for topic in topics:
                if topic.title.lower() == query:
                    topic.matches = True
                    matching_topic_count += 1

                    child_topics = topic.get_child_topics(
                        include_descendants=True)
                    topic.child_topics = [
                        t for t in child_topics if t.has_content()
                    ]

        template_values.update({
            'show_update': show_update,
            'topics': topics,
            'videos': filtered_videos,
            'video_exercises': video_exercises,
            'search_string': query,
            'video_count': video_count,
            'topic_count': topic_count,
            'matching_topic_count': matching_topic_count
        })

        self.render_jinja2_template("searchresults.html", template_values)
Ejemplo n.º 2
0
Archivo: main.py Proyecto: di445/server
    def get(self):
        query = self.request.get('page_search_query')
        template_values = {'page_search_query': query}
        query = query.strip()
        if len(query) < search.SEARCH_PHRASE_MIN_LENGTH:
            if len(query) > 0:
                template_values.update({
                    'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH
                })
            self.render_jinja2_template("searchresults.html", template_values)
            return

        # Do an async query for all ExerciseVideos, since this may be slow
        exvids_query = ExerciseVideo.all()
        exvids_future = util.async_queries([exvids_query])

        # One full (non-partial) search, then sort by kind
        all_text_keys = Topic.full_text_search(
                query, limit=50, kind=None,
                stemming=Topic.INDEX_STEMMING,
                multi_word_literal=Topic.INDEX_MULTI_WORD)

        # Quick title-only partial search
        topic_partial_results = filter(
                lambda topic_dict: query in topic_dict["title"].lower(),
                autocomplete.topic_title_dicts())
        video_partial_results = filter(
                lambda video_dict: query in video_dict["title"].lower(),
                autocomplete.video_title_dicts())
        url_partial_results = filter(
                lambda url_dict: query in url_dict["title"].lower(),
                autocomplete.url_title_dicts())

        # Combine results & do one big get!
        all_key_list = [str(key_and_title[0]) for key_and_title in all_text_keys]
        # all_key_list.extend([result["key"] for result in topic_partial_results])
        all_key_list.extend([result["key"] for result in video_partial_results])
        all_key_list.extend([result["key"] for result in url_partial_results])
        all_key_list = list(set(all_key_list))

        # Filter out anything that isn't a Topic, Url or Video
        all_key_list = [key for key in all_key_list if db.Key(key).kind() in ["Topic", "Url", "Video"]]

        # Get all the entities
        all_entities = db.get(all_key_list)

        # Group results by type
        topics = []
        videos = []
        for entity in all_entities:
            if isinstance(entity, Topic):
                topics.append(entity)
            elif isinstance(entity, Video):
                videos.append(entity)
            elif isinstance(entity, Url):
                videos.append(entity)
            elif entity:
                logging.info("Found unknown object " + repr(entity))

        topic_count = len(topics)

        # Get topics for videos not in matching topics
        filtered_videos = []
        filtered_videos_by_key = {}
        for video in videos:
            if [(str(topic.key()) in video.topic_string_keys) for topic in topics].count(True) == 0:
                video_topic = video.first_topic()
                if video_topic != None:
                    topics.append(video_topic)
                    filtered_videos.append(video)
                    filtered_videos_by_key[str(video.key())] = []
            else:
                filtered_videos.append(video)
                filtered_videos_by_key[str(video.key())] = []
        video_count = len(filtered_videos)

        # Get the related exercises
        all_exercise_videos = exvids_future[0].get_result()
        exercise_keys = []
        for exvid in all_exercise_videos:
            video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid))
            if video_key in filtered_videos_by_key:
                exercise_key = ExerciseVideo.exercise.get_value_for_datastore(exvid)
                video_exercise_keys = filtered_videos_by_key[video_key]
                video_exercise_keys.append(exercise_key)
                exercise_keys.append(exercise_key)
        exercises = db.get(exercise_keys)

        # Sort exercises with videos
        video_exercises = {}
        for video_key, exercise_keys in filtered_videos_by_key.iteritems():
            video_exercises[video_key] = map(lambda exkey: [exercise for exercise in exercises if exercise.key() == exkey][0], exercise_keys)

        # Count number of videos in each topic and sort descending
        if topics:
            if len(filtered_videos) > 0:
                for topic in topics:
                    topic.match_count = [(str(topic.key()) in video.topic_string_keys) for video in filtered_videos].count(True)
                topics = sorted(topics, key=lambda topic: -topic.match_count)
            else:
                for topic in topics:
                    topic.match_count = 0

        template_values.update({
                           'topics': topics,
                           'videos': filtered_videos,
                           'video_exercises': video_exercises,
                           'search_string': query,
                           'video_count': video_count,
                           'topic_count': topic_count,
                           })
        
        self.render_jinja2_template("searchresults.html", template_values)
Ejemplo n.º 3
0
    def get_old(self):
        """ Deprecated old version of search, so we can Gandalf in the new one.

        If new search is working, this should be taken out by May 31, 2012.
        """

        show_update = False
        if App.is_dev_server and user_util.is_current_user_admin():
            update = self.request_bool("update", False)
            if update:
                self.update()

            version_number = layer_cache.KeyValueCache.get(
                "last_dev_topic_vesion_indexed")
            default_version = topic_models.TopicVersion.get_default_version()
            if version_number != default_version.number:
                show_update = True

        query = self.request.get('page_search_query')
        template_values = {'page_search_query': query}
        query = query.strip()
        if len(query) < search.SEARCH_PHRASE_MIN_LENGTH:
            if len(query) > 0:
                template_values.update({
                    'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH
                })
            self.render_jinja2_template("searchresults.html", template_values)
            return
        searched_phrases = []

        # Do an async query for all ExerciseVideos, since this may be slow
        exvids_query = ExerciseVideo.all()
        exvids_future = util.async_queries([exvids_query])

        # One full (non-partial) search, then sort by kind
        all_text_keys = Topic.full_text_search(
                query, limit=50, kind=None,
                stemming=Topic.INDEX_STEMMING,
                multi_word_literal=Topic.INDEX_MULTI_WORD,
                searched_phrases_out=searched_phrases)

        # Quick title-only partial search
        topic_partial_results = filter(
                lambda topic_dict: query in topic_dict["title"].lower(),
                autocomplete.topic_title_dicts())
        video_partial_results = filter(
                lambda video_dict: query in video_dict["title"].lower(),
                autocomplete.video_title_dicts())
        url_partial_results = filter(
                lambda url_dict: query in url_dict["title"].lower(),
                autocomplete.url_title_dicts())

        # Combine results & do one big get!
        all_keys = [str(key_and_title[0]) for key_and_title in all_text_keys]
        all_keys.extend([result["key"] for result in topic_partial_results])
        all_keys.extend([result["key"] for result in video_partial_results])
        all_keys.extend([result["key"] for result in url_partial_results])
        all_keys = list(set(all_keys))

        # Filter out anything that isn't a Topic, Url or Video
        all_keys = [key for key in all_keys
                    if db.Key(key).kind() in ["Topic", "Url", "Video"]]

        # Get all the entities
        all_entities = db.get(all_keys)

        # Group results by type
        topics = []
        videos = []
        for entity in all_entities:
            if isinstance(entity, Topic):
                topics.append(entity)
            elif isinstance(entity, Video):
                videos.append(entity)
            elif isinstance(entity, Url):
                videos.append(entity)
            elif entity:
                logging.info("Found unknown object " + repr(entity))

        # Get topics for videos not in matching topics
        filtered_videos = []
        filtered_videos_by_key = {}
        for video in videos:
            if [(str(topic.key()) in video.topic_string_keys)
                for topic in topics].count(True) == 0:
                video_topic = video.first_topic()
                if video_topic != None:
                    topics.append(video_topic)
                    filtered_videos.append(video)
                    filtered_videos_by_key[str(video.key())] = []
            else:
                filtered_videos.append(video)
                filtered_videos_by_key[str(video.key())] = []
        video_count = len(filtered_videos)

        # Get the related exercises
        all_exercise_videos = exvids_future[0].get_result()
        exercise_keys = []
        for exvid in all_exercise_videos:
            video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid))
            if video_key in filtered_videos_by_key:
                exercise_key = ExerciseVideo.exercise.get_value_for_datastore(
                    exvid)
                video_exercise_keys = filtered_videos_by_key[video_key]
                video_exercise_keys.append(exercise_key)
                exercise_keys.append(exercise_key)
        exercises = db.get(exercise_keys)

        # Sort exercises with videos
        video_exercises = {}
        for video_key, exercise_keys in filtered_videos_by_key.iteritems():
            video_exercises[video_key] = map(
                lambda exkey: [exercise for exercise in exercises
                               if exercise.key() == exkey][0], exercise_keys)

        # Count number of videos in each topic and sort descending
        topic_count = 0
        matching_topic_count = 0
        if topics:
            if len(filtered_videos) > 0:
                for topic in topics:
                    topic.match_count = [
                        (str(topic.key()) in video.topic_string_keys)
                        for video in filtered_videos].count(True)
                    if topic.match_count > 0:
                        topic_count += 1

                topics = sorted(topics,
                                key=lambda topic: topic.match_count,
                                reverse=True)
            else:
                for topic in topics:
                    topic.match_count = 0

            for topic in topics:
                if topic.title.lower() == query:
                    topic.matches = True
                    matching_topic_count += 1

                    child_topics = topic.get_child_topics(
                        include_descendants=True)
                    topic.child_topics = [t for t in child_topics
                                          if t.has_content()]

        template_values.update({
                           'show_update': show_update,
                           'topics': topics,
                           'videos': filtered_videos,
                           'video_exercises': video_exercises,
                           'search_string': query,
                           'video_count': video_count,
                           'topic_count': topic_count,
                           'matching_topic_count': matching_topic_count
                           })

        self.render_jinja2_template("searchresults.html", template_values)
Ejemplo n.º 4
0
    def get(self):
        query = self.request.get('page_search_query')
        template_values = {'page_search_query': query}
        query = query.strip()
        if len(query) < search.SEARCH_PHRASE_MIN_LENGTH:
            if len(query) > 0:
                template_values.update(
                    {'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH})
            self.render_jinja2_template("searchresults.html", template_values)
            return

        # Do an async query for all ExerciseVideos, since this may be slow
        exvids_query = ExerciseVideo.all()
        exvids_future = util.async_queries([exvids_query])

        # One full (non-partial) search, then sort by kind
        all_text_keys = Topic.full_text_search(
            query,
            limit=50,
            kind=None,
            stemming=Topic.INDEX_STEMMING,
            multi_word_literal=Topic.INDEX_MULTI_WORD)

        # Quick title-only partial search
        topic_partial_results = filter(
            lambda topic_dict: query in topic_dict["title"].lower(),
            autocomplete.topic_title_dicts())
        video_partial_results = filter(
            lambda video_dict: query in video_dict["title"].lower(),
            autocomplete.video_title_dicts())
        url_partial_results = filter(
            lambda url_dict: query in url_dict["title"].lower(),
            autocomplete.url_title_dicts())

        # Combine results & do one big get!
        all_key_list = [
            str(key_and_title[0]) for key_and_title in all_text_keys
        ]
        # all_key_list.extend([result["key"] for result in topic_partial_results])
        all_key_list.extend(
            [result["key"] for result in video_partial_results])
        all_key_list.extend([result["key"] for result in url_partial_results])
        all_key_list = list(set(all_key_list))

        # Filter out anything that isn't a Topic, Url or Video
        all_key_list = [
            key for key in all_key_list
            if db.Key(key).kind() in ["Topic", "Url", "Video"]
        ]

        # Get all the entities
        all_entities = db.get(all_key_list)

        # Group results by type
        topics = []
        videos = []
        for entity in all_entities:
            if isinstance(entity, Topic):
                topics.append(entity)
            elif isinstance(entity, Video):
                videos.append(entity)
            elif isinstance(entity, Url):
                videos.append(entity)
            elif entity:
                logging.info("Found unknown object " + repr(entity))

        topic_count = len(topics)

        # Get topics for videos not in matching topics
        filtered_videos = []
        filtered_videos_by_key = {}
        for video in videos:
            if [(str(topic.key()) in video.topic_string_keys)
                    for topic in topics].count(True) == 0:
                video_topic = video.first_topic()
                if video_topic != None:
                    topics.append(video_topic)
                    filtered_videos.append(video)
                    filtered_videos_by_key[str(video.key())] = []
            else:
                filtered_videos.append(video)
                filtered_videos_by_key[str(video.key())] = []
        video_count = len(filtered_videos)

        # Get the related exercises
        all_exercise_videos = exvids_future[0].get_result()
        exercise_keys = []
        for exvid in all_exercise_videos:
            video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid))
            if video_key in filtered_videos_by_key:
                exercise_key = ExerciseVideo.exercise.get_value_for_datastore(
                    exvid)
                video_exercise_keys = filtered_videos_by_key[video_key]
                video_exercise_keys.append(exercise_key)
                exercise_keys.append(exercise_key)
        exercises = db.get(exercise_keys)

        # Sort exercises with videos
        video_exercises = {}
        for video_key, exercise_keys in filtered_videos_by_key.iteritems():
            video_exercises[video_key] = map(
                lambda exkey: [
                    exercise for exercise in exercises
                    if exercise.key() == exkey
                ][0], exercise_keys)

        # Count number of videos in each topic and sort descending
        if topics:
            if len(filtered_videos) > 0:
                for topic in topics:
                    topic.match_count = [
                        (str(topic.key()) in video.topic_string_keys)
                        for video in filtered_videos
                    ].count(True)
                topics = sorted(topics, key=lambda topic: -topic.match_count)
            else:
                for topic in topics:
                    topic.match_count = 0

        template_values.update({
            'topics': topics,
            'videos': filtered_videos,
            'video_exercises': video_exercises,
            'search_string': query,
            'video_count': video_count,
            'topic_count': topic_count,
        })

        self.render_jinja2_template("searchresults.html", template_values)