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

View File

@ -11,7 +11,7 @@ from ereuse_utils.test import ANY
from ereuse_devicehub.client import Client, UserClient from ereuse_devicehub.client import Client, UserClient
from ereuse_devicehub.devicehub import Devicehub 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.documents import documents
from ereuse_devicehub.resources.device import models as d from ereuse_devicehub.resources.device import models as d
from ereuse_devicehub.resources.lot.models import Lot 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 @pytest.mark.mvp
def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client): def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client):
"""Test export device information in a csv file with others users.""" """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, csv_user, _ = user.get(res=documents.DocumentDef.t,
item='actions/', item='actions/',
accept='text/csv', accept='text/csv',