From 744b14f28699526e66447f20d19ca3629395880d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 19 May 2022 13:48:52 +0200 Subject: [PATCH] join all snapshots logs in one for every uuid --- ereuse_devicehub/inventory/views.py | 37 +++++++++++++++++-- .../templates/inventory/snapshots_list.html | 12 ++---- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index a6c4fa2f..6c281cea 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -519,13 +519,42 @@ class SnapshotListView(GenericMixin): def dispatch_request(self): self.get_context() self.context['page_title'] = "Snapshots Logs" - snapshots_log = SnapshotsLog.query.filter( - SnapshotsLog.owner == current_user - ).order_by(SnapshotsLog.created.desc()) - self.context['snapshots_log'] = snapshots_log + self.context['snapshots_log'] = self.get_snapshots_log() return flask.render_template(self.template_name, **self.context) + def get_snapshots_log(self): + snapshots_log = SnapshotsLog.query.filter( + SnapshotsLog.owner == g.user + ).order_by(SnapshotsLog.created.desc()) + logs = {} + for snap in snapshots_log: + if snap.snapshot_uuid not in logs: + logs[snap.snapshot_uuid] = { + 'sid': snap.sid, + 'snapshot_uuid': snap.snapshot_uuid, + 'version': snap.version, + 'device': snap.snapshot.device.devicehub_id + if snap.snapshot + else '', + 'status': snap.get_status(), + 'severity': snap.severity, + 'created': snap.created, + } + continue + + if snap.created > logs[snap.snapshot_uuid]['created']: + logs[snap.snapshot_uuid]['created'] = snap.created + + if snap.severity > logs[snap.snapshot_uuid]['severity']: + logs[snap.snapshot_uuid]['severity'] = snap.severity + logs[snap.snapshot_uuid]['status'] = snap.get_status() + + result = sorted(logs.values(), key=lambda d: d['created']) + result.reverse() + + return result + devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add')) devices.add_url_rule('/action/trade/add/', view_func=NewTradeView.as_view('trade_add')) diff --git a/ereuse_devicehub/templates/inventory/snapshots_list.html b/ereuse_devicehub/templates/inventory/snapshots_list.html index c9c76a08..b7035613 100644 --- a/ereuse_devicehub/templates/inventory/snapshots_list.html +++ b/ereuse_devicehub/templates/inventory/snapshots_list.html @@ -30,7 +30,6 @@ Version Device Status - Description Time @@ -47,17 +46,14 @@ {{ snap.version }} - {% if snap.snapshot %} - - {{ snap.snapshot.device.devicehub_id }} + {% if snap.device %} + + {{ snap.device }} {% endif %} - {{ snap.get_status() }} - - - {{ snap.description }} + {{ snap.status }} {{ snap.created.strftime('%H:%M %d-%m-%Y') }}