Merge pull request #189 from eReuse/feature/buttom-report-in-lot

Feature/buttom report in lot
This commit is contained in:
cayop 2021-12-10 12:32:35 +01:00 committed by GitHub
commit 4af5e44d21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 25 deletions

View File

@ -1,4 +1,5 @@
import csv import csv
import json
import enum import enum
import uuid import uuid
import time import time
@ -150,7 +151,9 @@ class DevicesDocumentView(DeviceView):
class ActionsDocumentView(DeviceView): class ActionsDocumentView(DeviceView):
@cache(datetime.timedelta(minutes=1)) @cache(datetime.timedelta(minutes=1))
def find(self, args: dict): 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) return self.generate_post_csv(query)
def generate_post_csv(self, query): def generate_post_csv(self, query):
@ -169,6 +172,13 @@ class ActionsDocumentView(DeviceView):
cw.writerow(d.values()) cw.writerow(d.values())
query_trade = Trade.query.filter(Trade.devices.any(Device.id.in_(devs_id))).all() 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: for trade in query_trade:
data_rows = trade.get_metrics() data_rows = trade.get_metrics()
for row in data_rows: for row in data_rows:

View File

@ -142,7 +142,7 @@ def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client)
csv_user, _ = user.get(res=documents.DocumentDef.t, csv_user, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', accept='text/csv',
query=[('filter', {'type': ['Computer']})]) query=[('filter', {'type': ['Computer'], 'ids': [device_id]})])
csv_user2, _ = user2.get(res=documents.DocumentDef.t, csv_user2, _ = user2.get(res=documents.DocumentDef.t,
item='actions/', 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, csv_user, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', accept='text/csv',
query=[('filter', {'type': ['Computer']})]) query=[('filter', {'type': ['Computer'], 'ids': [device_id]})])
assert "4692" in csv_user assert "4692" in csv_user
assert "8692" in csv_user assert "8692" in csv_user

View File

@ -128,12 +128,13 @@ def test_metrics_action_status(user: UserClient, user2: UserClient):
# Insert computer # Insert computer
lenovo = yaml2json('desktop-9644w8n-lenovo-0169622.snapshot') lenovo = yaml2json('desktop-9644w8n-lenovo-0169622.snapshot')
snap, _ = user.post(json_encode(lenovo), res=ma.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) action_use, _ = user.post(action, res=ma.Action)
csv_str, _ = user.get(res=documents.DocumentDef.t, csv_str, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', 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' 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;;' body = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Status;;foo@foo.com;Receiver;;;Use;;'
assert head in csv_str 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) snap1, _ = user.post(json_encode(lenovo), res=ma.Snapshot)
snap2, _ = user.post(json_encode(acer), res=ma.Snapshot) snap2, _ = user.post(json_encode(acer), res=ma.Snapshot)
lot, _ = user.post({'name': 'MyLot'}, res=Lot) 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'])] ('id', snap2['device']['id'])]
lot, _ = user.post({}, lot, _ = user.post({},
res=Lot, res=Lot,
item='{}/devices'.format(lot['id']), item='{}/devices'.format(lot['id']),
query=devices) 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) user.post(action, res=ma.Action)
request_post = { request_post = {
'type': 'Trade', 'type': 'Trade',
'devices': [snap1['device']['id'], snap2['device']['id']], 'devices': devices_id,
'userFromEmail': user.email, 'userFromEmail': user.email,
'userToEmail': user2.email, 'userToEmail': user2.email,
'price': 10, 'price': 10,
@ -173,12 +177,12 @@ def test_complet_metrics_with_trade(user: UserClient, user2: UserClient):
user.post(res=ma.Action, data=request_post) 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) action_use, _ = user.post(action, res=ma.Action)
csv_str, _ = user.get(res=documents.DocumentDef.t, csv_str, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', 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 = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Trade;foo@foo.com;'
body1_lenovo += 'foo2@foo.com;Supplier;NeedConfirmation;Use;;' 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 assert body2_acer in csv_str
# User2 mark this device as Refurbish # 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) action_use2, _ = user2.post(action, res=ma.Action)
csv_str, _ = user.get(res=documents.DocumentDef.t, csv_str, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', 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 = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Trade;foo@foo.com;'
body1_lenovo += 'foo2@foo.com;Supplier;NeedConfirmation;Use;Use;' 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'] assert str(trade.actions[-1].id) == action['id']
# get metrics from botom in lot menu
csv_str, _ = user.get(res=documents.DocumentDef.t, csv_str, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', 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;;' body1 = ';bbbbbbbb;test.pdf;Trade-Container;foo@foo.com;foo2@foo.com;Supplier;False;Recycling;;'
body2 = ';;150.0;' body2 = ';;150.0;'
body3 = ';;0;0;Trade-Container;0;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 body1 in csv_str.split('\n')[-2]
assert body2 in csv_str.split('\n')[-2] assert body2 in csv_str.split('\n')[-2]
assert body3 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.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @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') lenovo = yaml2json('desktop-9644w8n-lenovo-0169622.snapshot')
snap1, _ = user.post(json_encode(lenovo), res=ma.Snapshot) snap1, _ = user.post(json_encode(lenovo), res=ma.Snapshot)
lot, _ = user.post({'name': 'MyLot'}, res=Lot) lot, _ = user.post({'name': 'MyLot'}, res=Lot)
devices = [('id', snap1['device']['id'])] device_id = snap1['device']['id']
devices = [('id', device_id)]
lot, _ = user.post({}, lot, _ = user.post({},
res=Lot, res=Lot,
item='{}/devices'.format(lot['id']), item='{}/devices'.format(lot['id']),
query=devices) query=devices)
request_post = { request_post = {
'type': 'Trade', 'type': 'Trade',
'devices': [snap1['device']['id']], 'devices': [device_id],
'userFromEmail': user.email, 'userFromEmail': user.email,
'userToEmail': user2.email, 'userToEmail': user2.email,
'price': 10, 'price': 10,
@ -294,20 +311,21 @@ def test_visual_metrics_for_old_owners(user: UserClient, user2: UserClient):
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade['id'], 'action': trade['id'],
'devices': [snap1['device']['id']] 'devices': [device_id]
} }
user2.post(res=ma.Action, data=request_confirm) 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) action_use, _ = user.post(action, res=ma.Action)
csv_supplier, _ = user.get(res=documents.DocumentDef.t, csv_supplier, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', accept='text/csv',
query=[('filter', {'type': ['Computer']})]) query=[('filter', {'type': ['Computer'], 'ids': [device_id]})])
csv_receiver, _ = user2.get(res=documents.DocumentDef.t, csv_receiver, _ = user2.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', accept='text/csv',
query=[('filter', {'type': ['Computer']})]) query=[('filter', {'type': ['Computer'], 'ids': [device_id]})])
body = ';;0;0;Trade;0;0\n' body = ';;0;0;Trade;0;0\n'
assert body in csv_receiver 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') lenovo = yaml2json('desktop-9644w8n-lenovo-0169622.snapshot')
snap1, _ = user.post(json_encode(lenovo), res=ma.Snapshot) snap1, _ = user.post(json_encode(lenovo), res=ma.Snapshot)
lot, _ = user.post({'name': 'MyLot'}, res=Lot) lot, _ = user.post({'name': 'MyLot'}, res=Lot)
devices = [('id', snap1['device']['id'])] device_id = snap1['device']['id']
devices = [('id', device_id)]
lot, _ = user.post({}, lot, _ = user.post({},
res=Lot, res=Lot,
item='{}/devices'.format(lot['id']), item='{}/devices'.format(lot['id']),
query=devices) query=devices)
request_post = { request_post = {
'type': 'Trade', 'type': 'Trade',
'devices': [snap1['device']['id']], 'devices': [device_id],
'userFromEmail': user2.email, 'userFromEmail': user2.email,
'userToEmail': user.email, 'userToEmail': user.email,
'price': 10, 'price': 10,
@ -342,17 +361,17 @@ def test_bug_trade_confirmed(user: UserClient, user2: UserClient):
csv_not_confirmed, _ = user.get(res=documents.DocumentDef.t, csv_not_confirmed, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', accept='text/csv',
query=[('filter', {'type': ['Computer']})]) query=[('filter', {'type': ['Computer'], 'ids': [device_id]})])
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade['id'], 'action': trade['id'],
'devices': [snap1['device']['id']] 'devices': [device_id]
} }
user2.post(res=ma.Action, data=request_confirm) user2.post(res=ma.Action, data=request_confirm)
csv_confirmed, _ = user2.get(res=documents.DocumentDef.t, csv_confirmed, _ = user2.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', 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_not_confirmed = "Trade;foo2@foo.com;foo@foo.com;Receiver;NeedConfirmation;"
body_confirmed = "Trade;foo2@foo.com;foo@foo.com;Receiver;TradeConfirmed;" body_confirmed = "Trade;foo2@foo.com;foo@foo.com;Receiver;TradeConfirmed;"