src/ckanext-Metadata/ckanext/Metadata/plugin.py
def after_search(self, search_results, search_params):
        '''
            Extensions will receive the search results, as well as the search
            parameters, and should return a modified (or not) object with the
            same structure:

                {'count': '', 'results': '', 'facets': ''}

            Note that count and facets may need to be adjusted if the extension
            changed the results for some reason.

            search_params will include an `extras` dictionary with all values
            from fields starting with `ext_`, so extensions can receive user
            input from specific fields.

        '''
        if 'owner_org' in search_params['q']:
            datasets = search_results['results']
            for dataset in datasets:
                if dataset['private'] and not (meta_helper.is_user_owns_package(dataset['id'], p.toolkit.c.user) or
                                               self.has_user_group_or_org_admin_role(dataset['owner_org'],
                                                                                     p.toolkit.c.user)):

                    # update dataset count for dataset groups
                    for group in dataset['groups']:
                        for item in search_results['search_facets']['groups']['items']:
                            if item.get('name') == group['name']:
                                item['count'] -= 1
                                break

                    # update dataset count for the organization
                    search_results['search_facets']['organization']['items'][0]['count'] -= 1
                    # update dataset counts by license
                    for item in search_results['search_facets']['license_id']['items']:
                        if item.get('name') == dataset['license_id']:
                            item['count'] -= 1
                            break

                    # update dataset count by each tag
                    for tag in dataset['tags']:
                        for item in search_results['search_facets']['tags']['items']:
                            if item.get('name') == tag['name']:
                                item['count'] -= 1
                                break

                    # update resource format counts
                    updated_formats = []
                    for resource in dataset['resources']:
                        res_format = resource['format']
                        if res_format in updated_formats:
                            continue
                        search_results['facets']['res_format'][res_format] -= 1
                        for item in search_results['search_facets']['res_format']['items']:
                            if item.get('name') == res_format:
                                item['count'] -= 1
                                updated_formats.append(res_format)
                                break

                    # remove the dataset since the user does not have access to it
                    search_results['results'].remove(dataset)

            # remove any facet items that has a count of zero or less
            facet_items_to_delete = {}
            facets = search_results['search_facets']
            for key, value in facets.iteritems():
                for facet_item in value['items']:
                    if facet_item['count'] <= 0:
                        if key not in facet_items_to_delete:
                            facet_items_to_delete[key] = {'items': []}
                            facet_items_to_delete[key]['items'].append(facet_item)
                        else:
                            facet_items_to_delete[key]['items'].append(facet_item)

            for facet_key in facet_items_to_delete:
                for item_to_delete in facet_items_to_delete[facet_key]['items']:
                    search_results['search_facets'][facet_key]['items'].remove(item_to_delete)

        return search_results

            Extensions will receive the search results, as well as the search
            parameters, and should return a modified (or not) object with the
            same structure:

                {'count': '', 'results': '', 'facets': ''}

            Note that count and facets may need to be adjusted if the extension
            changed the results for some reason.

            search_params will include an `extras` dictionary with all values
            from fields starting with `ext_`, so extensions can receive user
            input from specific fields.

        
Extensions will receive the search results, as well as the search
parameters, and should return a modified (or not) object with the
same structure.



Note that count and facets may need to be adjusted if the extension
changed the results for some reason.

search_params will include an `extras` dictionary with all values
from fields starting with `ext_`, so extensions can receive user
input from specific fields.
Extensions will receive the search results as well as the search parameters and should return a modified ( or not ) object with the same structure . Note that count and facets may need to be adjusted if the extension changed the results for some reason . search_params will include an ` extras ` dictionary with all values from fields starting with ` ext_ ` so extensions can receive user input from specific fields .
def after_search(self, search_results, search_params):
        if 'owner_org' in search_params['q']:
            datasets = search_results['results']
            for dataset in datasets:
                if dataset['private'] and not (meta_helper.is_user_owns_package(dataset['id'], p.toolkit.c.user) or
                                               self.has_user_group_or_org_admin_role(dataset['owner_org'],
                                                                                     p.toolkit.c.user)):
                    for group in dataset['groups']:
                        for item in search_results['search_facets']['groups']['items']:
                            if item.get('name') == group['name']:
                                item['count'] -= 1
                                break
                    search_results['search_facets']['organization']['items'][0]['count'] -= 1
                    for item in search_results['search_facets']['license_id']['items']:
                        if item.get('name') == dataset['license_id']:
                            item['count'] -= 1
                            break
                    for tag in dataset['tags']:
                        for item in search_results['search_facets']['tags']['items']:
                            if item.get('name') == tag['name']:
                                item['count'] -= 1
                                break
                    updated_formats = []
                    for resource in dataset['resources']:
                        res_format = resource['format']
                        if res_format in updated_formats:
                            continue
                        search_results['facets']['res_format'][res_format] -= 1
                        for item in search_results['search_facets']['res_format']['items']:
                            if item.get('name') == res_format:
                                item['count'] -= 1
                                updated_formats.append(res_format)
                                break
                    search_results['results'].remove(dataset)
            facet_items_to_delete = {}
            facets = search_results['search_facets']
            for key, value in facets.iteritems():
                for facet_item in value['items']:
                    if facet_item['count'] <= 0:
                        if key not in facet_items_to_delete:
                            facet_items_to_delete[key] = {'items': []}
                            facet_items_to_delete[key]['items'].append(facet_item)
                        else:
                            facet_items_to_delete[key]['items'].append(facet_item)
            for facet_key in facet_items_to_delete:
                for item_to_delete in facet_items_to_delete[facet_key]['items']:
                    search_results['search_facets'][facet_key]['items'].remove(item_to_delete)
        return search_results
def after_search ( self , search_results , search_params ) : if 'owner_org' in search_params [ 'q' ] : datasets = search_results [ 'results' ] for dataset in datasets : if dataset [ 'private' ] and not ( meta_helper . is_user_owns_package ( dataset [ 'id' ] , p . toolkit . c . user ) or self . has_user_group_or_org_admin_role ( dataset [ 'owner_org' ] , p . toolkit . c . user ) ) : for group in dataset [ 'groups' ] : for item in search_results [ 'search_facets' ] [ 'groups' ] [ 'items' ] : if item . get ( 'name' ) == group [ 'name' ] : item [ 'count' ] -= 1 break search_results [ 'search_facets' ] [ 'organization' ] [ 'items' ] [ 0 ] [ 'count' ] -= 1 for item in search_results [ 'search_facets' ] [ 'license_id' ] [ 'items' ] : if item . get ( 'name' ) == dataset [ 'license_id' ] : item [ 'count' ] -= 1 break for tag in dataset [ 'tags' ] : for item in search_results [ 'search_facets' ] [ 'tags' ] [ 'items' ] : if item . get ( 'name' ) == tag [ 'name' ] : item [ 'count' ] -= 1 break updated_formats = [ ] for resource in dataset [ 'resources' ] : res_format = resource [ 'format' ] if res_format in updated_formats : continue search_results [ 'facets' ] [ 'res_format' ] [ res_format ] -= 1 for item in search_results [ 'search_facets' ] [ 'res_format' ] [ 'items' ] : if item . get ( 'name' ) == res_format : item [ 'count' ] -= 1 updated_formats . append ( res_format ) break search_results [ 'results' ] . remove ( dataset ) facet_items_to_delete = { } facets = search_results [ 'search_facets' ] for key , value in facets . iteritems ( ) : for facet_item in value [ 'items' ] : if facet_item [ 'count' ] <= 0 : if key not in facet_items_to_delete : facet_items_to_delete [ key ] = { 'items' : [ ] } facet_items_to_delete [ key ] [ 'items' ] . append ( facet_item ) else : facet_items_to_delete [ key ] [ 'items' ] . append ( facet_item ) for facet_key in facet_items_to_delete : for item_to_delete in facet_items_to_delete [ facet_key ] [ 'items' ] : search_results [ 'search_facets' ] [ facet_key ] [ 'items' ] . remove ( item_to_delete ) return search_results
Extensions will receive the search results, as well as the search
parameters, and should return a modified (or not) object with the
same structure:
Extensions will receive the search results as well as the search parameters and should return a modified ( or not ) object with the same structure :
'''
            Extensions will receive the search results, as well as the search
            parameters, and should return a modified (or not) object with the
            same structure:

                {'count': '', 'results': '', 'facets': ''}

            Note that count and facets may need to be adjusted if the extension
            changed the results for some reason.

            search_params will include an `extras` dictionary with all values
            from fields starting with `ext_`, so extensions can receive user
            input from specific fields.

        ''' # update dataset count for dataset groups # update dataset count for the organization # update dataset counts by license # update dataset count by each tag # update resource format counts # remove the dataset since the user does not have access to it # remove any facet items that has a count of zero or less
self
search_results
search_params