ereuse_devicehub/resources/action/models.py

This commit is contained in:
Cayo Puigdefabregas 2021-01-14 17:32:03 +01:00
parent 12f28ba76f
commit d974d3d78f
2 changed files with 30 additions and 10 deletions

View file

@ -325,16 +325,15 @@ class Device(Thing):
actions = copy.copy(self.actions)
actions.sort(key=lambda x: x.created)
allocates = []
from ereuse_devicehub.resources.action.models import Snapshot, Allocate, Deallocate, Live
for act in actions:
if isinstance(act, Snapshot):
if act.type == 'Snapshot':
snapshot = act
lifestimes = snapshot.get_last_lifetimes()
lifetime = 0
if lifestimes:
lifetime = lifestimes[0]['lifetime']
if isinstance(act, Allocate):
if act.type == 'Allocate':
allo = {'type': 'Allocate',
'userCode': act.final_user_code,
'numUsers': act.end_users,
@ -345,15 +344,19 @@ class Device(Thing):
'allocateLifetime': lifetime}
allocates.append(allo)
if isinstance(act, Live):
if act.type == 'Live':
allocate = copy.copy(allo)
lifetime = act.usage_time_hdd
lifetime = act.usage_time_hdd.total_seconds()
allocate['type'] = 'Live'
allocate['liveCreate'] = act.created
for hd in lifestimes:
if hd['serial_number'] == act.serial_number:
lifetime = hd['lifetime']
break
allocate['liveLifetime'] = lifetime
allocates.append(allocate)
if isinstance(act, Deallocate):
if act.type == 'Deallocate':
deallo = {'type': 'Deallocate',
'userCode': '',
'numUsers': '',
@ -364,7 +367,7 @@ class Device(Thing):
'allocateLifetime': 0}
allocates.append(deallo)
return allocates
return allocates
def __lt__(self, other):
return self.id < other.id

View file

@ -11,7 +11,7 @@ from ereuse_utils.test import ANY
from ereuse_devicehub.client import Client, UserClient
from ereuse_devicehub.devicehub import Devicehub
from ereuse_devicehub.resources.action.models import Snapshot
from ereuse_devicehub.resources.action.models import Snapshot, Allocate, Live
from ereuse_devicehub.resources.documents import documents
from ereuse_devicehub.resources.device import models as d
from ereuse_devicehub.resources.lot.models import Lot
@ -110,7 +110,24 @@ def test_export_csv_permitions(user: UserClient, user2: UserClient, client: Clie
@pytest.mark.mvp
def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client):
"""Test export device information in a csv file with others users."""
snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot)
acer = file('acer.happy.battery.snapshot')
snapshot, _ = user.post(acer, res=Snapshot)
device_id = snapshot['device']['id']
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
"devices": [device_id], "description": "aaa",
"finalUserCode": "abcdefjhi",
"startTime": "2020-11-01T02:00:00+00:00",
"endTime": "2020-12-01T02:00:00+00:00"
}
user.post(res=Allocate, data=post_request)
hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0]
hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0]
hdd_action['lifetime'] += 1000
acer.pop('elapsed')
acer['licence_version'] = '1.0.0'
snapshot, _ = client.post(acer, res=Live)
csv_user, _ = user.get(res=documents.DocumentDef.t,
item='actions/',
accept='text/csv',
@ -131,7 +148,7 @@ def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client)
assert len(csv_user2) == 0
@pytest.mark.mvp
@pytest.mark.mvp
def test_export_basic_snapshot(user: UserClient):
"""Test export device information in a csv file."""
snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot)