'Filter repositories to those that are installable into Galaxy.'

@web.expose
def browse_valid_repositories(self, trans, **kwd):

    galaxy_url = common_util.handle_galaxy_url(trans, **kwd)
    if galaxy_url:
        kwd['galaxy_url'] = galaxy_url
    repository_id = kwd.get('id', None)
    if ('f-free-text-search' in kwd):
        if ('f-Category.name' in kwd):
            category_name = kwd['f-Category.name']
            category = suc.get_category_by_name(trans.app, category_name)
            kwd['id'] = trans.security.encode_id(category.id)
    if ('operation' in kwd):
        operation = kwd['operation'].lower()
        if (operation == 'preview_tools_in_changeset'):
            repository = repository_util.get_repository_in_tool_shed(trans.app, repository_id)
            repository_metadata = metadata_util.get_latest_repository_metadata(trans.app, repository.id, downloadable=True)
            latest_installable_changeset_revision = repository_metadata.changeset_revision
            return trans.response.send_redirect(web.url_for(controller='repository', action='preview_tools_in_changeset', repository_id=repository_id, changeset_revision=latest_installable_changeset_revision))
        elif (operation == 'valid_repositories_by_category'):
            for (k, v) in kwd.items():
                if k.startswith('f-'):
                    del kwd[k]
            category_id = kwd.get('id', None)
            category = suc.get_category(trans.app, category_id)
            kwd['f-Category.name'] = category.name
    (selected_changeset_revision, repository) = suc.get_repository_from_refresh_on_change(trans.app, **kwd)
    if repository:
        return trans.response.send_redirect(web.url_for(controller='repository', action='preview_tools_in_changeset', repository_id=trans.security.encode_id(repository.id), changeset_revision=selected_changeset_revision))
    url_args = dict(action='browse_valid_repositories', operation='preview_tools_in_changeset', repository_id=repository_id)
    self.valid_repository_grid.operations = [grids.GridOperation('Preview    and    install', url_args=url_args, allow_multiple=False, async_compatible=False)]
    title = trans.app.repository_grid_filter_manager.get_grid_title(trans, trailing_string='', default='Valid    Repositories')
    self.valid_repository_grid.title = title
    return self.valid_repository_grid(trans, **kwd)
