From daed1bdafbef866fbfd449e037f0ee302d9d53fd Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 3 Dec 2021 09:54:59 +0100 Subject: [PATCH 1/3] . --- ereuse_devicehub/resources/documents/documents.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index 774d3059..11a589df 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -155,6 +155,7 @@ class ActionsDocumentView(DeviceView): def generate_post_csv(self, query): """Get device query and put information in csv format.""" + # import pdb; pdb.set_trace() data = StringIO() cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') first = True From 10a1e23a5dbb4530a0bfb2e50b97cf5f527060ac Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 7 Dec 2021 13:20:16 +0100 Subject: [PATCH 2/3] Metrics only lot --- ereuse_devicehub/resources/documents/documents.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index 11a589df..6eb9f5f0 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -1,4 +1,5 @@ import csv +import json import enum import uuid import time @@ -150,12 +151,13 @@ class DevicesDocumentView(DeviceView): class ActionsDocumentView(DeviceView): @cache(datetime.timedelta(minutes=1)) def find(self, args: dict): - query = (x for x in self.query(args)) + filters = json.loads(request.args.get('filter', {})) + ids = filters.get('ids', []) + query = self.query(args).filter(Device.id.in_(ids)) return self.generate_post_csv(query) def generate_post_csv(self, query): """Get device query and put information in csv format.""" - # import pdb; pdb.set_trace() data = StringIO() cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') first = True @@ -170,6 +172,13 @@ class ActionsDocumentView(DeviceView): cw.writerow(d.values()) query_trade = Trade.query.filter(Trade.devices.any(Device.id.in_(devs_id))).all() + lot_id = request.args.get('lot') + if lot_id and not query_trade: + lot = Lot.query.filter_by(id=lot_id).one() + if hasattr(lot, "trade") and lot.trade: + if g.user in [lot.trade.user_from, lot.trade.user_to]: + query_trade = [lot.trade] + for trade in query_trade: data_rows = trade.get_metrics() for row in data_rows: From f00dad123f193122a3ac3b3b9f5a6fe533102081 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 9 Dec 2021 10:59:26 +0100 Subject: [PATCH 3/3] fixing test and adding one more --- tests/test_documents.py | 4 +-- tests/test_metrics.py | 63 +++++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/tests/test_documents.py b/tests/test_documents.py index fa70b636..da031c98 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -142,7 +142,7 @@ def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client) csv_user, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': [device_id]})]) csv_user2, _ = user2.get(res=documents.DocumentDef.t, item='actions/', @@ -180,7 +180,7 @@ def test_live_export_csv2(user: UserClient, client: Client, app: Devicehub): csv_user, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': [device_id]})]) assert "4692" in csv_user assert "8692" in csv_user diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 20242ad3..ae094963 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -128,12 +128,13 @@ def test_metrics_action_status(user: UserClient, user2: UserClient): # Insert computer lenovo = yaml2json('desktop-9644w8n-lenovo-0169622.snapshot') snap, _ = user.post(json_encode(lenovo), res=ma.Snapshot) - action = {'type': ma.Use.t, 'devices': [snap['device']['id']]} + device_id = snap['device']['id'] + action = {'type': ma.Use.t, 'devices': [device_id]} action_use, _ = user.post(action, res=ma.Action) csv_str, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': [device_id]})]) head = 'DHID;Hid;Document-Name;Action-Type;Action-User-LastOwner-Supplier;Action-User-LastOwner-Receiver;Action-Create-By;Trade-Confirmed;Status-Created-By-Supplier-About-Reciber;Status-Receiver;Status Supplier – Created Date;Status Receiver – Created Date;Trade-Weight;Action-Create;Allocate-Start;Allocate-User-Code;Allocate-NumUsers;UsageTimeAllocate;Type;LiveCreate;UsageTimeHdd\n' body = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Status;;foo@foo.com;Receiver;;;Use;;' assert head in csv_str @@ -150,19 +151,22 @@ def test_complet_metrics_with_trade(user: UserClient, user2: UserClient): snap1, _ = user.post(json_encode(lenovo), res=ma.Snapshot) snap2, _ = user.post(json_encode(acer), res=ma.Snapshot) lot, _ = user.post({'name': 'MyLot'}, res=Lot) - devices = [('id', snap1['device']['id']), + device1_id = snap1['device']['id'] + device2_id = snap2['device']['id'] + devices_id = [device1_id, device2_id] + devices = [('id', device1_id), ('id', snap2['device']['id'])] lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices) - action = {'type': ma.Refurbish.t, 'devices': [snap1['device']['id']]} + action = {'type': ma.Refurbish.t, 'devices': [device1_id]} user.post(action, res=ma.Action) request_post = { 'type': 'Trade', - 'devices': [snap1['device']['id'], snap2['device']['id']], + 'devices': devices_id, 'userFromEmail': user.email, 'userToEmail': user2.email, 'price': 10, @@ -173,12 +177,12 @@ def test_complet_metrics_with_trade(user: UserClient, user2: UserClient): user.post(res=ma.Action, data=request_post) - action = {'type': ma.Use.t, 'devices': [snap1['device']['id']]} + action = {'type': ma.Use.t, 'devices': [device1_id]} action_use, _ = user.post(action, res=ma.Action) csv_str, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': devices_id})]) body1_lenovo = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Trade;foo@foo.com;' body1_lenovo += 'foo2@foo.com;Supplier;NeedConfirmation;Use;;' @@ -194,12 +198,12 @@ def test_complet_metrics_with_trade(user: UserClient, user2: UserClient): assert body2_acer in csv_str # User2 mark this device as Refurbish - action = {'type': ma.Use.t, 'devices': [snap1['device']['id']]} + action = {'type': ma.Use.t, 'devices': [device1_id]} action_use2, _ = user2.post(action, res=ma.Action) csv_str, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': devices_id})]) body1_lenovo = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Trade;foo@foo.com;' body1_lenovo += 'foo2@foo.com;Supplier;NeedConfirmation;Use;Use;' @@ -252,19 +256,31 @@ def test_metrics_action_status_for_containers(user: UserClient, user2: UserClien assert str(trade.actions[-1].id) == action['id'] + # get metrics from botom in lot menu csv_str, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer']}), ('lot', lot['id'])]) 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 len(csv_str.split('\n')) == 3 assert body1 in csv_str.split('\n')[-2] assert body2 in csv_str.split('\n')[-2] assert body3 in csv_str.split('\n')[-2] + # get metrics from botom in devices menu + csv_str2, _ = user.get(res=documents.DocumentDef.t, + item='actions/', + accept='text/csv', + query=[('filter', {'type': ['Computer'], 'ids': [snap['device']['id']]})]) + + assert len(csv_str2.split('\n')) == 4 + assert body1 in csv_str2.split('\n')[-2] + assert body2 in csv_str2.split('\n')[-2] + assert body3 in csv_str2.split('\n')[-2] + @pytest.mark.mvp @pytest.mark.usefixtures(conftest.app_context.__name__) @@ -274,14 +290,15 @@ def test_visual_metrics_for_old_owners(user: UserClient, user2: UserClient): lenovo = yaml2json('desktop-9644w8n-lenovo-0169622.snapshot') snap1, _ = user.post(json_encode(lenovo), res=ma.Snapshot) lot, _ = user.post({'name': 'MyLot'}, res=Lot) - devices = [('id', snap1['device']['id'])] + device_id = snap1['device']['id'] + devices = [('id', device_id)] lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices) request_post = { 'type': 'Trade', - 'devices': [snap1['device']['id']], + 'devices': [device_id], 'userFromEmail': user.email, 'userToEmail': user2.email, 'price': 10, @@ -294,20 +311,21 @@ def test_visual_metrics_for_old_owners(user: UserClient, user2: UserClient): request_confirm = { 'type': 'Confirm', 'action': trade['id'], - 'devices': [snap1['device']['id']] + 'devices': [device_id] } user2.post(res=ma.Action, data=request_confirm) - action = {'type': ma.Refurbish.t, 'devices': [snap1['device']['id']]} + + action = {'type': ma.Refurbish.t, 'devices': [device_id]} action_use, _ = user.post(action, res=ma.Action) csv_supplier, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': [device_id]})]) csv_receiver, _ = user2.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': [device_id]})]) body = ';;0;0;Trade;0;0\n' assert body in csv_receiver @@ -322,14 +340,15 @@ def test_bug_trade_confirmed(user: UserClient, user2: UserClient): lenovo = yaml2json('desktop-9644w8n-lenovo-0169622.snapshot') snap1, _ = user.post(json_encode(lenovo), res=ma.Snapshot) lot, _ = user.post({'name': 'MyLot'}, res=Lot) - devices = [('id', snap1['device']['id'])] + device_id = snap1['device']['id'] + devices = [('id', device_id)] lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices) request_post = { 'type': 'Trade', - 'devices': [snap1['device']['id']], + 'devices': [device_id], 'userFromEmail': user2.email, 'userToEmail': user.email, 'price': 10, @@ -342,17 +361,17 @@ def test_bug_trade_confirmed(user: UserClient, user2: UserClient): csv_not_confirmed, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': [device_id]})]) request_confirm = { 'type': 'Confirm', 'action': trade['id'], - 'devices': [snap1['device']['id']] + 'devices': [device_id] } user2.post(res=ma.Action, data=request_confirm) csv_confirmed, _ = user2.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', - query=[('filter', {'type': ['Computer']})]) + query=[('filter', {'type': ['Computer'], 'ids': [device_id]})]) body_not_confirmed = "Trade;foo2@foo.com;foo@foo.com;Receiver;NeedConfirmation;" body_confirmed = "Trade;foo2@foo.com;foo@foo.com;Receiver;TradeConfirmed;"