playlists/views.py
def query_group_by_track_key(from_date, to_date):
    ''' app engine Query and GqlQuery do not support SQL group by
    manually count each record and group them by some unique key
    '''

    query = filter_tracks_by_date_range(from_date, to_date)
    fields = ['album_title', 'artist_name', 'label']

    #
    key_item = 'group_by_key'
    key_counter = 'play_count'

    # group by key/fields
    def item_key(item):
        key_parts = []
        for key in fields:
            stub = as_encoded_str(_get_entity_attr(item, key, ''))

            if stub is None:
                # for existing None-type attributes
                stub = ''
            stub = stub.lower()
            key_parts.append(stub)
        return ','.join(key_parts)

    # dict version of db rec
    def item2hash(item):
        d = {}
        for key in fields:
            d[key] = _get_entity_attr(item, key, None)

        # init additional props
        d[key_counter] = 0
        d['from_date'] = from_date
        d['to_date'] = to_date
        d['heavy_rotation'] = int(bool(HEAVY_ROTATION_TAG in item.categories))
        d['light_rotation'] = int(bool(LIGHT_ROTATION_TAG in item.categories))

        return d

    # unique list of tracks with order
    items = []

    # hash of seen keys
    seen = {}

    for item in AutoRetry(query):
        key = item_key(item)

        if not seen.has_key(key):
            x = item2hash(item)
            seen[key] = x
            items.append(x)

        # inc counter
        seen[key][key_counter] += 1

    return items
 app engine Query and GqlQuery do not support SQL group by
    manually count each record and group them by some unique key
    
app engine Query and GqlQuery do not support SQL group by
manually count each record and group them by some unique key
app engine Query and GqlQuery do not support SQL group by manually count each record and group them by some unique key
def query_group_by_track_key(from_date, to_date):
    query = filter_tracks_by_date_range(from_date, to_date)
    fields = ['album_title', 'artist_name', 'label']
    key_item = 'group_by_key'
    key_counter = 'play_count'
     group by key/fields
    def item_key(item):
        key_parts = []
        for key in fields:
            stub = as_encoded_str(_get_entity_attr(item, key, ''))
            if stub is None:
                 for existing None-type attributes
                stub = ''
            stub = stub.lower()
            key_parts.append(stub)
        return ','.join(key_parts)
     dict version of db rec
    def item2hash(item):
        d = {}
        for key in fields:
            d[key] = _get_entity_attr(item, key, None)
         init additional props
        d[key_counter] = 0
        d['from_date'] = from_date
        d['to_date'] = to_date
        d['heavy_rotation'] = int(bool(HEAVY_ROTATION_TAG in item.categories))
        d['light_rotation'] = int(bool(LIGHT_ROTATION_TAG in item.categories))
        return d
     unique list of tracks with order
    items = []
     hash of seen keys
    seen = {}
    for item in AutoRetry(query):
        key = item_key(item)
        if not seen.has_key(key):
            x = item2hash(item)
            seen[key] = x
            items.append(x)
         inc counter
        seen[key][key_counter] += 1
    return items
def query_group_by_track_key ( from_date , to_date ) : query = filter_tracks_by_date_range ( from_date , to_date ) fields = [ 'album_title' , 'artist_name' , 'label' ] key_item = 'group_by_key' key_counter = 'play_count' def item_key ( item ) : key_parts = [ ] for key in fields : stub = as_encoded_str ( _get_entity_attr ( item , key , '' ) ) if stub is None : stub = '' stub = stub . lower ( ) key_parts . append ( stub ) return ',' . join ( key_parts ) def item2hash ( item ) : d = { } for key in fields : d [ key ] = _get_entity_attr ( item , key , None ) d [ key_counter ] = 0 d [ 'from_date' ] = from_date d [ 'to_date' ] = to_date d [ 'heavy_rotation' ] = int ( bool ( HEAVY_ROTATION_TAG in item . categories ) ) d [ 'light_rotation' ] = int ( bool ( LIGHT_ROTATION_TAG in item . categories ) ) return d items = [ ] seen = { } for item in AutoRetry ( query ) : key = item_key ( item ) if not seen . has_key ( key ) : x = item2hash ( item ) seen [ key ] = x items . append ( x ) seen [ key ] [ key_counter ] += 1 return items
app engine Query and GqlQuery do not support SQL group by
manually count each record and group them by some unique key
app engine Query and GqlQuery do not support SQL group by manually count each record and group them by some unique key
''' app engine Query and GqlQuery do not support SQL group by
    manually count each record and group them by some unique key
    ''' # # group by key/fields # for existing None-type attributes # dict version of db rec # init additional props # unique list of tracks with order # hash of seen keys # inc counter
from_date
to_date