From 2b43d96b21bea94f1e02eec67855f048fb8194a0 Mon Sep 17 00:00:00 2001 From: Carlos Natalino <carlos.natalino@chalmers.se> Date: Mon, 15 Nov 2021 15:32:48 +0000 Subject: [PATCH] Advancing on the device workflow and improving tests. --- src/webui/requirements.in | 1 + src/webui/service/device/routes.py | 26 ++++++++++++-- .../service/templates/device/detail.html | 34 ++++++++++++++++++- src/webui/tests/test_unitary.py | 6 ++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/webui/requirements.in b/src/webui/requirements.in index 6e43eabd8..7c8834509 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 30ca43a3a..7c545138e 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 a5578dd2d..c090e13b5 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 0c0b82a6b..f71fef0ab 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 -- GitLab