From 56dcdf28970386f35be7e4374023ddf87eaebc7c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 23 Nov 2021 11:08:48 +0100 Subject: [PATCH 1/2] get_status docuemnts in metrics --- ereuse_devicehub/resources/device/metrics.py | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/device/metrics.py b/ereuse_devicehub/resources/device/metrics.py index df713c42..bf118da6 100644 --- a/ereuse_devicehub/resources/device/metrics.py +++ b/ereuse_devicehub/resources/device/metrics.py @@ -208,9 +208,11 @@ class TradeMetrics(MetricsMix): def __init__(self, *args, **kwargs): self.document = kwargs.pop('document') self.actions = copy.copy(self.document.actions) + self.reversed_actions = copy.copy(self.document.actions) self.hid = self.document.file_hash self.devicehub_id = '' super().__init__(*args, **kwargs) + self.reversed_actions.reverse() def get_metrics(self): self.last_trade = next(x for x in self.actions if x.t == 'Trade') @@ -219,7 +221,7 @@ class TradeMetrics(MetricsMix): row['type'] = 'Trade-Document' row['action_type'] = 'Trade-Document' - if self.document.total_weight: + if self.document.total_weight or self.document.weight: row['type'] = 'Trade-Container' row['action_type'] = 'Trade-Container' @@ -235,10 +237,31 @@ class TradeMetrics(MetricsMix): elif self.document.owner == self.last_trade.user_to: row['action_create_by'] = 'Receiver' + self.get_status(row) + self.rows.append(row) return self.rows + def get_status(self, row): + """ + We want to know if receiver or supplier do some action that change the status + of the container. + """ + if not (self.document.total_weight and self.document.weight): + return '' + for ac in self.reversed_actions: + if ac.type not in ['Use', 'Refurbish', 'Recycling', 'Management']: + continue + if ac.author == self.last_trade.user_from: + row['status_supplier'] = ac.type + row['status_supplier_created'] = ac.created + if ac.author == self.last_trade.user_to: + row['status_receiver'] = ac.type + row['status_receiver_created'] = ac.created + + return '' + def get_confirms(self): """ if the action is one trade action, is possible than have a list of confirmations. From 1cdd7d39b42f909e51a2bdf8e32a958518ea0bf3 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 23 Nov 2021 11:26:21 +0100 Subject: [PATCH 2/2] fixing test metrics container --- tests/test_metrics.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_metrics.py b/tests/test_metrics.py index cab985d1..20242ad3 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -257,11 +257,13 @@ def test_metrics_action_status_for_containers(user: UserClient, user2: UserClien accept='text/csv', query=[('filter', {'type': ['Computer']})]) - body1 = ';bbbbbbbb;test.pdf;Trade-Container;foo@foo.com;foo2@foo.com;Supplier;False;;;;;150.0;' - body2 = ';;0;0;Trade-Container;0;0' + body1 = ';bbbbbbbb;test.pdf;Trade-Container;foo@foo.com;foo2@foo.com;Supplier;False;Recycling;;' + body2 = ';;150.0;' + body3 = ';;0;0;Trade-Container;0;0' assert len(csv_str.split('\n')) == 4 assert body1 in csv_str.split('\n')[-2] assert body2 in csv_str.split('\n')[-2] + assert body3 in csv_str.split('\n')[-2] @pytest.mark.mvp