djdb/search.py
def simple_music_search(query_str, max_num_results=None, entity_kind=None,
                        reviewed=False, user_key=None, include_revoked=False):
    """A simple free-form search well-suited for the music library.

    Args:
      query_str: A unicode query string.
      max_num_results: The maximum number of items to return.  If the
        number of matches exceeds this, some items will be discarded.
        If None, all matches will be returned.
      entity_kind: An optional string.  If given, the returned keys are
        restricted to entities of that kind.
      reviewed: If True, only return albums and tracks associated with
        albums that have been reviewed.
      user_key: If set, only return items containing reviews by the
        specified user.
      include_revoked: Whether to include revoked items in the search results.

    Returns:
      A dict mapping object types to lists of entities.
    """
    # First, find all matching keys.
    all_matches = fetch_keys_for_query_string(query_str, entity_kind)

    # If we returned None, this is an invalid query.
    if all_matches is None:
        return None

    # If a user key is set, we are only interested in items that have
    # been reviewed.
    if user_key:
        reviewed = True

    # Next, filter out the keys for tracks that do not have a title match.
    # Allow search on the tag field for tracks.
    keys_to_fetch = []
    for key, fields in all_matches.iteritems():
        if key.kind() != "Track" or "title" in fields or "tag" in fields or "track_artist" in fields:
            keys_to_fetch.append(key)

    # Fetch all of the specified keys from the datastore and construct a
    # segmented dict of matches.
    segmented_matches = load_and_segment_keys(keys_to_fetch, include_revoked)

    # If necessary, filter out unreviewed matches.
    if reviewed:
        if "Track" in segmented_matches:
            segmented_matches["Track"] = [
                trk for trk in segmented_matches["Track"]
                if _album_is_reviewed(trk.album, user_key)]
        if "Album" in segmented_matches:
            segmented_matches["Album"] = [
                alb for alb in segmented_matches["Album"]
                if _album_is_reviewed(alb, user_key)]
        if "Artist" in segmented_matches:
            segmented_matches["Artist"] = [
                art for art in segmented_matches["Artist"]
                if _artist_has_reviewed_album(art, user_key)]

    # Now enforce any limit on the number of results.
    _enforce_results_limit_on_matches(segmented_matches, max_num_results)
                
    return segmented_matches
A simple free-form search well-suited for the music library.

    Args:
      query_str: A unicode query string.
      max_num_results: The maximum number of items to return.  If the
        number of matches exceeds this, some items will be discarded.
        If None, all matches will be returned.
      entity_kind: An optional string.  If given, the returned keys are
        restricted to entities of that kind.
      reviewed: If True, only return albums and tracks associated with
        albums that have been reviewed.
      user_key: If set, only return items containing reviews by the
        specified user.
      include_revoked: Whether to include revoked items in the search results.

    Returns:
      A dict mapping object types to lists of entities.
    
A simple free-form search well-suited for the music library.
A simple free - form search well - suited for the music library .
def simple_music_search(query_str, max_num_results=None, entity_kind=None,
                        reviewed=False, user_key=None, include_revoked=False):
    all_matches = fetch_keys_for_query_string(query_str, entity_kind)
    if all_matches is None:
        return None
    if user_key:
        reviewed = True
    keys_to_fetch = []
    for key, fields in all_matches.iteritems():
        if key.kind() != "Track" or "title" in fields or "tag" in fields or "track_artist" in fields:
            keys_to_fetch.append(key)
    segmented_matches = load_and_segment_keys(keys_to_fetch, include_revoked)
    if reviewed:
        if "Track" in segmented_matches:
            segmented_matches["Track"] = [
                trk for trk in segmented_matches["Track"]
                if _album_is_reviewed(trk.album, user_key)]
        if "Album" in segmented_matches:
            segmented_matches["Album"] = [
                alb for alb in segmented_matches["Album"]
                if _album_is_reviewed(alb, user_key)]
        if "Artist" in segmented_matches:
            segmented_matches["Artist"] = [
                art for art in segmented_matches["Artist"]
                if _artist_has_reviewed_album(art, user_key)]
    _enforce_results_limit_on_matches(segmented_matches, max_num_results)
    return segmented_matches
def simple_music_search ( query_str , max_num_results = None , entity_kind = None , reviewed = False , user_key = None , include_revoked = False ) : all_matches = fetch_keys_for_query_string ( query_str , entity_kind ) if all_matches is None : return None if user_key : reviewed = True keys_to_fetch = [ ] for key , fields in all_matches . iteritems ( ) : if key . kind ( ) != "Track" or "title" in fields or "tag" in fields or "track_artist" in fields : keys_to_fetch . append ( key ) segmented_matches = load_and_segment_keys ( keys_to_fetch , include_revoked ) if reviewed : if "Track" in segmented_matches : segmented_matches [ "Track" ] = [ trk for trk in segmented_matches [ "Track" ] if _album_is_reviewed ( trk . album , user_key ) ] if "Album" in segmented_matches : segmented_matches [ "Album" ] = [ alb for alb in segmented_matches [ "Album" ] if _album_is_reviewed ( alb , user_key ) ] if "Artist" in segmented_matches : segmented_matches [ "Artist" ] = [ art for art in segmented_matches [ "Artist" ] if _artist_has_reviewed_album ( art , user_key ) ] _enforce_results_limit_on_matches ( segmented_matches , max_num_results ) return segmented_matches
A simple free-form search well-suited for the music library.
A simple free - form search well - suited for the music library .
"""A simple free-form search well-suited for the music library.

    Args:
      query_str: A unicode query string.
      max_num_results: The maximum number of items to return.  If the
        number of matches exceeds this, some items will be discarded.
        If None, all matches will be returned.
      entity_kind: An optional string.  If given, the returned keys are
        restricted to entities of that kind.
      reviewed: If True, only return albums and tracks associated with
        albums that have been reviewed.
      user_key: If set, only return items containing reviews by the
        specified user.
      include_revoked: Whether to include revoked items in the search results.

    Returns:
      A dict mapping object types to lists of entities.
    """ # First, find all matching keys. # If we returned None, this is an invalid query. # If a user key is set, we are only interested in items that have # been reviewed. # Next, filter out the keys for tracks that do not have a title match. # Allow search on the tag field for tracks. # Fetch all of the specified keys from the datastore and construct a # segmented dict of matches. # If necessary, filter out unreviewed matches. # Now enforce any limit on the number of results.
A dict mapping object types to lists of entities.
A dict mapping object types to lists of entities .
query_str
A unicode query string.
A unicode query string .
max_num_results
The maximum number of items to return.  If the
number of matches exceeds this, some items will be discarded.
If None, all matches will be returned.
The maximum number of items to return . If the number of matches exceeds this some items will be discarded . If None all matches will be returned .
entity_kind
An optional string.  If given, the returned keys are
restricted to entities of that kind.
An optional string . If given the returned keys are restricted to entities of that kind .
reviewed
If True, only return albums and tracks associated with
albums that have been reviewed.
If True only return albums and tracks associated with albums that have been reviewed .
user_key
If set, only return items containing reviews by the
specified user.
If set only return items containing reviews by the specified user .
include_revoked
Whether to include revoked items in the search results.
Whether to include revoked items in the search results .