diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-12-13 13:00:24 +0100 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-12-13 13:39:09 +0100 |
commit | fd8da5de26c9a18a069e66c554ffd51f32cf86b8 (patch) | |
tree | feb783e8dc20f7167ef450f12680efe4bbfd7f98 | |
parent | c7c28d6e92051d86f8855a4006e2ebefbc0ec5cc (diff) |
views: select the most used repo in Set repository viewv1.1.3-36
Preselect the most used repository among the selected clients
for the view Commands -> Setup -> Set repository
Change get_clients_oglive() into def get_client_list_by_key()
to obtain a dictionary with the list of ips of the clients with
the same value in the field of the client payload passed as key.
For example for the key 'repo_id' it would return a dictionary
{repo_id: [ips of the clients with that repo_id]}
-rw-r--r-- | ogcp/views.py | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/ogcp/views.py b/ogcp/views.py index 6e9752b..1049675 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -2470,8 +2470,8 @@ def action_mode(): selected_clients=selected_clients, clients=clients, modes_set=modes_set) -def get_clients_oglive(ips, server): - oglives = {} +def get_client_list_by_key(ips, server, key): + ret = {} for ip in ips: r = server.get('/client/info', payload={"client": [ip]}) if not r: @@ -2479,12 +2479,12 @@ def get_clients_oglive(ips, server): if r.status_code != requests.codes.ok: raise ServerErrorCode resp = r.json() - oglive = resp['livedir'] - if oglive not in oglives: - oglives[oglive] = [ip] + val = resp[key] + if val not in ret: + ret[val] = [ip] else: - oglives[oglive].append(ip) - return oglives + ret[val].append(ip) + return ret @app.route('/action/oglive', methods=['GET', 'POST']) @login_required @@ -2511,7 +2511,7 @@ def action_oglive(): server = get_server_from_clients(list(ips)) try: - oglives_set = get_clients_oglive(ips, server) + oglives_set = get_client_list_by_key(ips, server, key='livedir') except ServerError: return ogserver_down('commands') except ServerErrorCode: @@ -2601,9 +2601,25 @@ def action_repo_set(): except ServerErrorCode: return ogserver_error(r, 'commands') - form.repo.choices = [(repo["id"], repo["name"]) for repo in repositories] - scopes, clients = get_scopes(set(ips)) + + try: + repo_set = get_client_list_by_key(ips, server, key='repo_id') + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error(r, 'commands') + + most_used_repo_id = max(repo_set, key=lambda id: len(repo_set[id])) + + form.repo.choices = [] + for repo in repositories: + repo_entry = (repo["id"], repo["name"]) + if repo["id"] == most_used_repo_id: + form.repo.choices.insert(0, repo_entry) + else: + form.repo.choices.append(repo_entry) + selected_clients = list(get_selected_clients(scopes['scope']).items()) return render_template('actions/repo_set.html', repos_set=repos_set, form=form, scopes=scopes, selected_clients=selected_clients) |