diff options
author | Javier Sánchez Parra <jsanchez@soleta.eu> | 2022-09-29 17:21:00 +0200 |
---|---|---|
committer | Javier Sánchez Parra <jsanchez@soleta.eu> | 2022-11-17 17:56:07 +0100 |
commit | 20935966efdae847023680217a4c109648698340 (patch) | |
tree | b36281271b133a141a1666d6039e4695128b9f3c /ogcp | |
parent | d029df8e842f8dce9d48f8ec9e11e336b0af46e9 (diff) |
WIP Make scopes tree async
Diffstat (limited to 'ogcp')
-rw-r--r-- | ogcp/static/js/ogcp.js | 12 | ||||
-rw-r--r-- | ogcp/templates/macros.html | 7 | ||||
-rw-r--r-- | ogcp/templates/scopes.html | 2 | ||||
-rw-r--r-- | ogcp/templates/tree.html | 2 | ||||
-rw-r--r-- | ogcp/views.py | 42 |
5 files changed, 58 insertions, 7 deletions
diff --git a/ogcp/static/js/ogcp.js b/ogcp/static/js/ogcp.js index 61d828d..d267e5b 100644 --- a/ogcp/static/js/ogcp.js +++ b/ogcp/static/js/ogcp.js @@ -3,6 +3,18 @@ const macs = new Map(); const Interval = 1000; let updateTimeoutId = null; +async function buildScopesTree() { + const resp = await fetch('/ogservers'); + const servers = await resp.json(); + const scopes = document.getElementById("scopes") + + for (const s of servers) { + const resp = await fetch('/scopes-tree?server=' + s); + const htmlText = await resp.text(); + scopes.innerHTML += htmlText; + } +} + async function show_client_mac(pill_id) { const pill = $('#' +pill_id); const ip = pill.html().split('<br>')[1] diff --git a/ogcp/templates/macros.html b/ogcp/templates/macros.html index c56e4e8..e114692 100644 --- a/ogcp/templates/macros.html +++ b/ogcp/templates/macros.html @@ -1,8 +1,6 @@ -{% macro scopes_tree_collapse(scopes, state='', selection_mode='scopes') -%} +{% macro scopes_tree_collapse(state='', selection_mode='scopes') -%} -<ul id="scopes" class="nav ogcp-nav flex-column nav-pills"> - {{ scopes_tree_collapse_level(scopes["scope"], "", "", state, selection_mode) }} -</ul> +<ul id="scopes" class="nav ogcp-nav flex-column nav-pills"></ul> <script> // Launch the javascript on document ready, so all the global functions exists // in the scope @@ -18,6 +16,7 @@ {% elif selection_mode == 'scopes' %} limitCheckboxes(); {% endif %} + buildScopesTree(); } }); </script> diff --git a/ogcp/templates/scopes.html b/ogcp/templates/scopes.html index d07e7d3..db2b742 100644 --- a/ogcp/templates/scopes.html +++ b/ogcp/templates/scopes.html @@ -12,7 +12,7 @@ {% endblock %} {% block sidebar %} - {{ macros.scopes_tree_collapse(scopes, sidebar_state, 'scopes') }} + {{ macros.scopes_tree_collapse(sidebar_state, 'scopes') }} {% endblock %} {% block commands %} diff --git a/ogcp/templates/tree.html b/ogcp/templates/tree.html new file mode 100644 index 0000000..fc06105 --- /dev/null +++ b/ogcp/templates/tree.html @@ -0,0 +1,2 @@ +{% import "macros.html" as macros %} +{{ macros.scopes_tree_collapse_level(scopes["scope"], '', '', '', 'scopes') }} diff --git a/ogcp/views.py b/ogcp/views.py index 0a65c17..5be8647 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -237,6 +237,27 @@ def get_server_from_ip_port(str_ip_port): raise Exception('Server with address ' + str_ip_port + 'is not configured') +def get_scopes_from_server(server): + r = server.get('/scopes') + server_scope = {} + server_scope['name'] = server.name + server_scope['type'] = "server" + server_scope['server_ip_port'] = (server.ip + ":" + str(server.port)) + server_scope.update(r.json()) + list_scopes = [] + list_scopes.append(server_scope) + scopes = {'scope': list_scopes} + if current_user.scopes: + allowed_scopes = [] + get_allowed_scopes(scopes, allowed_scopes) + scopes = {'scope': allowed_scopes} + r = server.get('/clients') + clients = r.json() + add_state_and_ips(scopes, clients['clients'], set()) + + return scopes + + def get_scopes(ips=set()): list_scopes = [] responses = multi_request('get', '/scopes') @@ -410,11 +431,28 @@ def get_client_mac(): return jsonify(pretty_mac) +@app.route('/ogservers', methods=['GET']) +@login_required +def get_ogservers(): + servers_list = [s.ip + ':' + str(s.port) for s in servers] + resp = jsonify(servers_list) + return resp + + +@app.route('/scopes-tree', methods=['GET']) +@login_required +def scopes_tree(): + params = request.args.to_dict() + server = get_server_from_ip_port(params['server']) + scopes = get_scopes_from_server(server) + return render_template('tree.html', scopes=scopes) + + @app.route('/scopes/') @login_required def scopes(): - scopes, clients = get_scopes() - return render_template('scopes.html', scopes=scopes, clients=clients) + return render_template('scopes.html') + @app.route('/action/poweroff', methods=['GET', 'POST']) @login_required |