diff --git a/src/webui/requirements.in b/src/webui/requirements.in index 6e43eabd89c32166e6af9381f0bb6da51cf649dd..7c883450947b5b9d3c38b8f9d38a8d7de8f55627 100644 --- a/src/webui/requirements.in +++ b/src/webui/requirements.in @@ -2,6 +2,7 @@ flask flask-wtf flask-healthz grpcio +grpcio-health-checking prometheus-client pytest pytest-benchmark diff --git a/src/webui/service/device/routes.py b/src/webui/service/device/routes.py index 30ca43a3af3df695d976b670f1feb3229313d563..7c545138eac3bc532296045c98823b491825fda6 100644 --- a/src/webui/service/device/routes.py +++ b/src/webui/service/device/routes.py @@ -78,9 +78,8 @@ def add(): client.close() flash(f'New device was created with ID "{response.device_uuid.uuid}".', 'success') - redirect('/device/') + return redirect('/device/') except Exception as e: - print(e) flash(f'Problem adding the device. {e.details()}', 'danger') return render_template('device/add.html', form=form, @@ -95,3 +94,26 @@ def detail(device_uuid: str): response: Device = client.GetDevice(request) client.close() return render_template('device/detail.html', device=response) + +@device.get('<device_uuid>/delete') +def delete(device_uuid): + try: + client: DeviceClient = DeviceClient(DEVICE_SERVICE_ADDRESS, DEVICE_SERVICE_PORT) + + # first, check if device exists! + # request: DeviceId = DeviceId() + # request.device_uuid.uuid = device_uuid + # response: Device = client.GetDevice(request) + # TODO: finalize implementation + + request: DeviceId = DeviceId() + request.device_uuid.uuid = device_uuid + response = client.DeleteDevice(request) + + client.close() + + flash('Device deleted successfully!', 'success') + except Exception as e: + flash(f'Problem deleting the device. {e.details()}', 'danger') + + return redirect('/device/') diff --git a/src/webui/service/templates/device/detail.html b/src/webui/service/templates/device/detail.html index a5578dd2d06cfed9e621b9841e8516e375126540..c090e13b516954778a1c27842520f1e8b2bc493b 100644 --- a/src/webui/service/templates/device/detail.html +++ b/src/webui/service/templates/device/detail.html @@ -3,6 +3,21 @@ {% block content %} <h1>Device {{ device.device_id.device_uuid.uuid }}</h1> + <div class="row mb-3"> + <div class="col-sm-3"> + <button type="button" class="btn btn-success" onclick="window.location.href = '/device/'"><i class="bi bi-box-arrow-in-left"></i>Back to device list</button> + </div> + <div class="col-sm-3"> + <a id="update" class="btn btn-secondary" href="#"><i class="bi bi-pencil-square"></i>Update</a> + </div> + <div class="col-sm-3"> + <!-- <button type="button" class="btn btn-danger"><i class="bi bi-x-square"></i>Delete device</button> --> + <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal"> + <i class="bi bi-x-square"></i>Delete device + </button> + </div> + </div> + <div class="row mb-3"> <b>UUID:</b> <div class="col-sm-10"> @@ -36,6 +51,23 @@ </div> </div> - <div class="row mb-3"><button type="button" class="btn btn-success" onclick="window.location.href = '/device/'"><i class="bi bi-box-arrow-in-left"></i>Back to device list</button></div> + <!-- Modal --> +<div class="modal fade" id="deleteModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true"> + <div class="modal-dialog"> + <div class="modal-content"> + <div class="modal-header"> + <h5 class="modal-title" id="staticBackdropLabel">Delete device?</h5> + <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> + </div> + <div class="modal-body"> + Are you sure you want to delete the device "{{ device.device_id.device_uuid.uuid }}"? + </div> + <div class="modal-footer"> + <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">No</button> + <a type="button" class="btn btn-danger" href="{{ url_for('device.delete', device_uuid=device.device_id.device_uuid.uuid) }}"><i class="bi bi-exclamation-diamond"></i>Yes</a> + </div> + </div> + </div> + </div> {% endblock %} \ No newline at end of file diff --git a/src/webui/tests/test_unitary.py b/src/webui/tests/test_unitary.py index 0c0b82a6bbb096dc825791944331314093236970..f71fef0ab0bf1bf8a61050b0c0ae2d359e70eb96 100644 --- a/src/webui/tests/test_unitary.py +++ b/src/webui/tests/test_unitary.py @@ -96,3 +96,9 @@ def test_device_add_action(client): } rw = client.post('/device/add', data=DEVICE_EMU, follow_redirects=True) assert b'success' in rw.data + +def test_device_delete_action(client): + with client.session_transaction() as sess: + sess['context_uuid'] = 'admin' + rw = client.get('/device/EMULATED/delete', follow_redirects=True) + assert b'success' in rw.data