Add current ogServer to scopesForm

With this commit when users select a scope, the ogServer to
which it belongs is also sent. Then when processing the form we obtain
this ogServer and we can send it the pertinent requests.

This commit also makes action "Add client" to use the ogServer sent in
the form.
async-tree
Javier Sánchez Parra 2022-08-09 19:11:00 +02:00
parent 2614c9304e
commit 229ad311be
4 changed files with 33 additions and 5 deletions

View File

@ -98,6 +98,7 @@ class ImageRestoreForm(FlaskForm):
restore = SubmitField(label=_l('Restore'))
class ClientDetailsForm(FlaskForm):
server = HiddenField()
name = StringField(label=_l('Name'))
ip = StringField(label=_l('IP'))
mac = StringField(label=_l('MAC'))

View File

@ -64,6 +64,12 @@ function checkParentsCheckboxes() {
if (checkboxChildren.length == 0) return;
if (this.name == "scope-server") {
const checkedChildren = checkboxChildren.filter(":checked");
checkbox.checked = checkedChildren.length > 0;
return;
}
const unCheckedChildren = checkboxChildren.filter(":not(:checked)");
checkbox.indeterminate =

View File

@ -23,7 +23,12 @@
{% macro scopes_tree_collapse_level(scopes, parent_id, state) -%}
{% for scope in scopes %}
<li id="{{ scope["name"]|replace(".", "_")|replace(" ", "_") }}_{{ scope["id"] }}" class="nav-item">
{% if scope["type"] == "room" %}
{% if scope["type"] == "server" %}
<input class="form-check-input" type="checkbox" form="scopesForm"
value="{{ scope["server_ip_port"] }}" onclick="return false;"
{% if scope.get("selected", False) %}checked{% endif %}
name="scope-server" hidden/>
{% elif scope["type"] == "room" %}
<input class="form-check-input" type="checkbox" form="scopesForm"
value="{{ scope["id"] }}"
{% if state %}style="filter: grayscale(100%);" onclick="return false;"{% endif %}

View File

@ -96,7 +96,7 @@ def validate_elements(elements, min_len=1, max_len=float('inf')):
return valid
def parse_elements(checkboxes_dict):
unwanted_elements = ['csrf_token', 'scope-room']
unwanted_elements = ['csrf_token', 'scope-server', 'scope-room']
elements = set()
for key, elements_list in checkboxes_dict.items():
if key not in unwanted_elements:
@ -223,6 +223,16 @@ def get_server_from_clients(selected_clients):
return server
def get_server_from_ip_port(str_ip_port):
ip, port = str_ip_port.split(':')
for s in servers:
if s.ip == ip and s.port == int(port):
return s
raise Exception('Server with address ' + str_ip_port + 'is not configured')
def get_scopes(ips=set()):
list_scopes = []
responses = multi_request('get', '/scopes')
@ -230,6 +240,9 @@ def get_scopes(ips=set()):
scopes = r['json']
server_scope = {}
server_scope['name'] = r['server'].name
server_scope['type'] = "server"
server_scope['server_ip_port'] = (r['server'].ip + ":" +
str(r['server'].port))
server_scope.update(scopes)
list_scopes.append(server_scope)
all_scopes = {'scope': list_scopes}
@ -838,7 +851,8 @@ def action_client_add():
"room": int(form.room.data),
"serial_number": form.serial_number.data}
r = g.server.post('/client/add', payload)
server = get_server_from_ip_port(form.server.data)
r = server.post('/client/add', payload)
if r.status_code != requests.codes.ok:
flash(_('ogServer: error adding client'),
category='error')
@ -846,15 +860,17 @@ def action_client_add():
flash(_('Client added successfully'), category='info')
return redirect(url_for("scopes"))
else:
r = g.server.get('/mode')
params = request.args.to_dict()
if not params.get('scope-room'):
flash(_('Please, select one room'), category='error')
return redirect(url_for('scopes'))
form.server.data = params['scope-server']
server = get_server_from_ip_port(params['scope-server'])
r = server.get('/mode')
available_modes = [(mode, mode) for mode in r.json()['modes']]
form.boot.choices = list(available_modes)
r = g.server.get('/scopes')
r = server.get('/scopes')
room_id = params['scope-room']
rooms = parse_scopes_from_tree(r.json(), 'room')
rooms = [(room['id'], room['name']) for room in rooms