From db63b6bc60c0c3c75662136e8524e274746c355b Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Mon, 29 Jan 2024 10:38:53 +0100 Subject: views: Preselect most used oglive When setting up oglive, preselect oglive that is most used by selected clients. If only one client is selected, preselect that of the client. In the unusual scenario where a client is set with an oglive that is not in the ogserver's list of available oglives, preselect default. --- ogcp/templates/actions/oglive.html | 25 +++++++++++++++++++++++++ ogcp/views.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/ogcp/templates/actions/oglive.html b/ogcp/templates/actions/oglive.html index 6b8e8b3..81b70dd 100644 --- a/ogcp/templates/actions/oglive.html +++ b/ogcp/templates/actions/oglive.html @@ -17,6 +17,31 @@ {{ macros.cmd_selected_clients(selected_clients) }} +{% if oglives_set|length > 1 %} +

Selected clients have different ogLive

+ + + + + + + + + + {% for oglive, clients in oglives_set.items() %} + + + + + {% endfor %} + +
ogLiveClients
{{oglive}} + {% for client in clients %} + {{client}} + {% endfor %} +
+{% endif %} + {{ wtf.quick_form(form, action=url_for('action_oglive'), method='post', diff --git a/ogcp/views.py b/ogcp/views.py index 24df2e3..f6f1fb7 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -1577,6 +1577,22 @@ def action_mode(): selected_clients=selected_clients, clients=clients, modes_set=modes_set) +def get_clients_oglive(ips, server): + oglives = {} + for ip in ips: + r = server.get('/client/info', payload={"client": [ip]}) + if not r: + raise ServerError + if r.status_code != requests.codes.ok: + raise ServerErrorCode + resp = r.json() + oglive = resp['livedir'] + client_name = resp['name'] + if oglive not in oglives: + oglives[oglive] = [client_name] + else: + oglives[oglive].append(client_name) + return oglives @app.route('/action/oglive', methods=['GET', 'POST']) @login_required @@ -1602,21 +1618,35 @@ def action_oglive(): return redirect(url_for('commands')) server = get_server_from_clients(list(ips)) + + try: + oglives_set = get_clients_oglive(ips, server) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') + r = server.get('/oglive/list') if not r: return ogserver_down('commands') if r.status_code != requests.codes.ok: return ogserver_error('commands') + most_used_oglive = max(oglives_set, key=lambda l: len(oglives_set[l])) available_oglives = [(oglive.get('directory'), oglive.get('directory')) - for oglive in r.json()['oglive']] - available_oglives.insert(0, ('default', 'default')) + for oglive in r.json()['oglive'] + if oglive.get('directory') == most_used_oglive] + if not available_oglives: + available_oglives.append(('default', 'default')) + available_oglives.extend([(oglive.get('directory'), oglive.get('directory')) + for oglive in r.json()['oglive'] + if oglive.get('directory') != most_used_oglive]) form.oglive.choices = list(available_oglives) form.ok.render_kw = {'formaction': url_for('action_oglive')} scopes, clients = get_scopes(set(ips)) selected_clients = list(get_selected_clients(scopes['scope']).items()) - return render_template('actions/oglive.html', form=form, scopes=scopes, + return render_template('actions/oglive.html', oglives_set=oglives_set, form=form, scopes=scopes, selected_clients=selected_clients) -- cgit v1.2.3-18-g5258