'Select or change the tool panel section to contain the tools included in the tool shed repository
being reinstalled.  If there are updates available for the repository in the tool shed, the
tool_dependencies and repository_dependencies associated with the updated changeset revision will
have been retrieved from the tool shed and passed in the received kwd.  In this case, the stored
tool shed repository metadata from the Galaxy database will not be used since it is outdated.'

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

    message = ''
    status = 'done'
    repository_id = kwd.get('id', None)
    latest_changeset_revision = kwd.get('latest_changeset_revision', None)
    latest_ctx_rev = kwd.get('latest_ctx_rev', None)
    tool_shed_repository = repository_util.get_installed_tool_shed_repository(trans.app, repository_id)
    repository_clone_url = common_util.generate_clone_url_for_installed_repository(trans.app, tool_shed_repository)
    metadata = tool_shed_repository.metadata
    tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(trans.app, str(tool_shed_repository.tool_shed))
    tool_path = tool_shed_repository.get_tool_relative_path(trans.app)[0]
    if (latest_changeset_revision and latest_ctx_rev):
        encoded_updated_repo_info_dict = kwd.get('updated_repo_info_dict', None)
        updated_repo_info_dict = encoding_util.tool_shed_decode(encoded_updated_repo_info_dict)
        readme_files_dict = updated_repo_info_dict.get('readme_files_dict', None)
        includes_data_managers = updated_repo_info_dict.get('includes_data_managers', False)
        includes_datatypes = updated_repo_info_dict.get('includes_datatypes', False)
        includes_workflows = updated_repo_info_dict.get('includes_workflows', False)
        includes_tool_dependencies = updated_repo_info_dict.get('includes_tool_dependencies', False)
        repo_info_dict = updated_repo_info_dict['repo_info_dict']
    else:
        includes_data_managers = False
        includes_datatypes = False
        includes_tool_dependencies = False
        includes_workflows = False
        readme_files_dict = None
        tool_dependencies = None
        if metadata:
            if ('data_manager' in metadata):
                includes_data_managers = True
            if ('datatypes' in metadata):
                includes_datatypes = True
            if ('tool_dependencies' in metadata):
                includes_tool_dependencies = True
            if ('workflows' in metadata):
                includes_workflows = True
            params = dict(name=tool_shed_repository.name, owner=tool_shed_repository.owner, changeset_revision=tool_shed_repository.installed_changeset_revision)
            pathspec = ['repository', 'get_readme_files']
            raw_text = util.url_get(tool_shed_url, password_mgr=self.app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params)
            readme_files_dict = json.loads(raw_text)
            tool_dependencies = metadata.get('tool_dependencies', None)
        rdim = repository_dependency_manager.RepositoryDependencyInstallManager(trans.app)
        repository_dependencies = rdim.get_repository_dependencies_for_installed_tool_shed_repository(trans.app, tool_shed_repository)
        repo_info_dict = repository_util.create_repo_info_dict(trans.app, repository_clone_url=repository_clone_url, changeset_revision=tool_shed_repository.installed_changeset_revision, ctx_rev=tool_shed_repository.ctx_rev, repository_owner=tool_shed_repository.owner, repository_name=tool_shed_repository.name, tool_dependencies=tool_dependencies, repository_dependencies=repository_dependencies)
    irm = trans.app.installed_repository_manager
    dependencies_for_repository_dict = irm.get_dependencies_for_repository(tool_shed_url, repo_info_dict, includes_tool_dependencies, updating=True)
    includes_tool_dependencies = dependencies_for_repository_dict.get('includes_tool_dependencies', False)
    includes_tools = dependencies_for_repository_dict.get('includes_tools', False)
    includes_tools_for_display_in_tool_panel = dependencies_for_repository_dict.get('includes_tools_for_display_in_tool_panel', False)
    installed_repository_dependencies = dependencies_for_repository_dict.get('installed_repository_dependencies', None)
    installed_tool_dependencies = dependencies_for_repository_dict.get('installed_tool_dependencies', None)
    missing_repository_dependencies = dependencies_for_repository_dict.get('missing_repository_dependencies', None)
    missing_tool_dependencies = dependencies_for_repository_dict.get('missing_tool_dependencies', None)
    if (installed_repository_dependencies or missing_repository_dependencies):
        has_repository_dependencies = True
    else:
        has_repository_dependencies = False
    if includes_tools_for_display_in_tool_panel:
        if ('tool_panel_section' in metadata):
            tool_panel_dict = metadata['tool_panel_section']
            if tool_panel_dict:
                if tool_util.panel_entry_per_tool(tool_panel_dict):
                    tool_section_dicts = tool_panel_dict[tool_panel_dict.keys()[0]]
                    tool_section_dict = tool_section_dicts[0]
                    original_section_name = tool_section_dict['name']
                else:
                    original_section_name = tool_panel_dict['name']
            else:
                original_section_name = ''
        else:
            original_section_name = ''
        tool_panel_section_select_field = tool_util.build_tool_panel_section_select_field(trans.app)
        no_changes_check_box = CheckboxField('no_changes', checked=True)
        if original_section_name:
            message += ('The    tools    contained    in    your    <b>%s</b>    repository    were    last    loaded    into    the    tool    panel    section    <b>%s</b>.        ' % (escape(tool_shed_repository.name), original_section_name))
            message += 'Uncheck    the    <b>No    changes</b>    check    box    and    select    a    different    tool    panel    section    to    load    the    tools    in    a    '
            message += 'different    section    in    the    tool    panel.        '
            status = 'warning'
        else:
            message += ('The    tools    contained    in    your    <b>%s</b>    repository    were    last    loaded    into    the    tool    panel    outside    of    any    sections.        ' % escape(tool_shed_repository.name))
            message += 'Uncheck    the    <b>No    changes</b>    check    box    and    select    a    tool    panel    section    to    load    the    tools    into    that    section.        '
            status = 'warning'
    else:
        no_changes_check_box = None
        original_section_name = ''
        tool_panel_section_select_field = None
    shed_tool_conf_select_field = tool_util.build_shed_tool_conf_select_field(trans.app)
    dd = dependency_display.DependencyDisplayer(trans.app)
    containers_dict = dd.populate_containers_dict_for_new_install(tool_shed_url=tool_shed_url, tool_path=tool_path, readme_files_dict=readme_files_dict, installed_repository_dependencies=installed_repository_dependencies, missing_repository_dependencies=missing_repository_dependencies, installed_tool_dependencies=installed_tool_dependencies, missing_tool_dependencies=missing_tool_dependencies, updating=False)
    containers_dict = dd.merge_missing_repository_dependencies_to_installed_container(containers_dict)
    install_repository_dependencies_check_box = CheckboxField('install_repository_dependencies', checked=True)
    if (trans.app.config.tool_dependency_dir is None):
        if includes_tool_dependencies:
            message += 'Tool    dependencies    defined    in    this    repository    can    be    automatically    installed    if    you    set    the    value    of    your    <b>tool_dependency_dir</b>    '
            message += 'setting    in    your    Galaxy    config    file    (galaxy.ini)    and    restart    your    Galaxy    server    before    installing    the    repository.        '
            status = 'warning'
        install_tool_dependencies_check_box_checked = False
    else:
        install_tool_dependencies_check_box_checked = True
    install_tool_dependencies_check_box = CheckboxField('install_tool_dependencies', checked=install_tool_dependencies_check_box_checked)
    view = views.DependencyResolversView(self.app)
    if view.installable_resolvers:
        install_resolver_dependencies_check_box = CheckboxField('install_resolver_dependencies', checked=True)
    else:
        install_resolver_dependencies_check_box = None
    return trans.fill_template('/admin/tool_shed_repository/reselect_tool_panel_section.mako', repository=tool_shed_repository, no_changes_check_box=no_changes_check_box, original_section_name=original_section_name, includes_data_managers=includes_data_managers, includes_datatypes=includes_datatypes, includes_tools=includes_tools, includes_tools_for_display_in_tool_panel=includes_tools_for_display_in_tool_panel, includes_tool_dependencies=includes_tool_dependencies, includes_workflows=includes_workflows, has_repository_dependencies=has_repository_dependencies, install_repository_dependencies_check_box=install_repository_dependencies_check_box, install_tool_dependencies_check_box=install_tool_dependencies_check_box, install_resolver_dependencies_check_box=install_resolver_dependencies_check_box, containers_dict=containers_dict, tool_panel_section_select_field=tool_panel_section_select_field, shed_tool_conf_select_field=shed_tool_conf_select_field, encoded_repo_info_dict=encoding_util.tool_shed_encode(repo_info_dict), repo_info_dict=repo_info_dict, message=message, status=status)
