diff --git a/juice.py b/juice.py index 6157585..b84d52d 100644 --- a/juice.py +++ b/juice.py @@ -27,6 +27,8 @@ class RelayDevice: self.description = "" self.location = "" self.ip_address = "" + self.locked = False + self.sub_devices = [] self.sub_devices.append(RelayOutlet()) self.sub_devices.append(RelayOutlet()) @@ -194,6 +196,9 @@ def edit(): else: return make_error(404, "device_id not found") + if device.locked: + return make_error(400, "device is locked for editing") + if sub_dev_id: for sub_dev in device.sub_devices: if sub_dev.id == sub_dev_id: @@ -249,6 +254,29 @@ def new_device(): return json.dumps(data) +@app_views.route('/lock_device') +def lock_device(): + """ + Locks or unlocks a device to prevent or allow editing it's fields. + """ + device_id = request.args.get('device_id') + locked = request.args.get('locked') == 'true' + + for device in network: + if device.id == device_id: + break + else: + return make_error(404, "device_id not found") + + if locked: + device.locked = True + else: + device.locked = False + save_network() + + return jsonify(device_id=device.id, locked=device.locked) + + def make_error(code, message): """ Returns a JSON error. diff --git a/static/juice.js b/static/juice.js index 1a778c4..d9241de 100644 --- a/static/juice.js +++ b/static/juice.js @@ -123,31 +123,63 @@ function new_device() { function lock_device(device) { if (device.querySelector('.save')) { return; } - let fields = device.querySelectorAll('.editable'); - fields.forEach(function(field) { - field.querySelector('.edit').remove(); + let params = { + device_id: device.id, + locked: true, + }; + let query = Object.keys(params) + .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) + .join('&'); + + fetch(window.location.href + 'lock_device?' + query) + .then(function(response) { + return response.json(); + }) + .then(function(json) { + if (!json.locked) { return; } + + let fields = device.querySelectorAll('.editable'); + fields.forEach(function(field) { + field.querySelector('.edit').remove(); + }); + device.querySelector('.id').querySelector('.lock').remove(); + let unlock = document.createElement('span'); + unlock.innerHTML = ''; + unlock.className = 'unlock font-awesome'; + unlock.setAttribute('onclick', 'unlock_device(this.parentElement.parentElement)'); + device.querySelector('.id').appendChild(unlock); }); - device.querySelector('.id').querySelector('.lock').remove(); - let unlock = document.createElement('span'); - unlock.innerHTML = ''; - unlock.className = 'unlock font-awesome'; - unlock.setAttribute('onclick', 'unlock_device(this.parentElement.parentElement)'); - device.querySelector('.id').appendChild(unlock); } function unlock_device(device) { - let fields = device.querySelectorAll('.editable'); - fields.forEach(function(field) { - let edit = document.createElement('span'); - edit.innerHTML = ''; - edit.className = 'edit font-awesome'; - edit.setAttribute('onclick', 'edit_field(this.parentElement)'); - field.appendChild(edit); + let params = { + device_id: device.id, + locked: false, + }; + let query = Object.keys(params) + .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) + .join('&'); + + fetch(window.location.href + 'lock_device?' + query) + .then(function(response) { + return response.json(); + }) + .then(function(json) { + if (json.locked) { return; } + + let fields = device.querySelectorAll('.editable'); + fields.forEach(function(field) { + let edit = document.createElement('span'); + edit.innerHTML = ''; + edit.className = 'edit font-awesome'; + edit.setAttribute('onclick', 'edit_field(this.parentElement)'); + field.appendChild(edit); + }); + device.querySelector('.id').querySelector('.unlock').remove(); + let lock = document.createElement('span'); + lock.innerHTML = ''; + lock.className = 'lock font-awesome'; + lock.setAttribute('onclick', 'lock_device(this.parentElement.parentElement)'); + device.querySelector('.id').appendChild(lock); }); - device.querySelector('.id').querySelector('.unlock').remove(); - let lock = document.createElement('span'); - lock.innerHTML = ''; - lock.className = 'lock font-awesome'; - lock.setAttribute('onclick', 'lock_device(this.parentElement.parentElement)'); - device.querySelector('.id').appendChild(lock); } diff --git a/templates/index.html b/templates/index.html index 23600c7..d2ce55f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -18,16 +18,16 @@
{% for device in network %}
-
{{ device.id }}
-
{{ device.description }}
-
{{ device.location }}
-
{{ device.ip_address }}
+
{{ device.id }}{% if not device.locked %}{% else %}{% endif %}
+
{{ device.description }}{% if not device.locked %}{% endif %}
+
{{ device.location }}{% if not device.locked %}{% endif %}
+
{{ device.ip_address }}{% if not device.locked %}{% endif %}
{% for sub_dev in device.sub_devices %}
{{ sub_dev.id }}
-
{{ sub_dev.description }}
+
{{ sub_dev.description }}{% if not device.locked %}{% endif %}
{% endfor %}