adding final user and timings as property instead of insert this datas in the database

This commit is contained in:
Cayo Puigdefabregas 2020-12-01 22:03:15 +01:00
parent 783265ad65
commit 4d9420d1e9
4 changed files with 33 additions and 5 deletions

View File

@ -60,8 +60,6 @@ def upgrade():
op.drop_table('live', schema=f'{get_inv()}') op.drop_table('live', schema=f'{get_inv()}')
op.create_table('live', op.create_table('live',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('final_user_code', citext.CIText(), default='', nullable=True,
comment = "This is a internal code for mainteing the secrets of the personal datas of the new holder"),
sa.Column('serial_number', sa.Unicode(), nullable=True, sa.Column('serial_number', sa.Unicode(), nullable=True,
comment='The serial number of the Hard Disk in lower case.'), comment='The serial number of the Hard Disk in lower case.'),
sa.Column('time', sa.SmallInteger(), nullable=True), sa.Column('time', sa.SmallInteger(), nullable=True),

View File

@ -1299,13 +1299,39 @@ class Live(JoinedWithOneDeviceMixin, ActionWithOneDevice):
information about its state (in the form of a ``Snapshot`` action) information about its state (in the form of a ``Snapshot`` action)
and usage statistics. and usage statistics.
""" """
final_user_code = Column(CIText(), default='', nullable=True)
final_user_code.comment = """This is a internal code for mainteing the secrets of the
personal datas of the new holder"""
serial_number = Column(Unicode(), check_lower('serial_number')) serial_number = Column(Unicode(), check_lower('serial_number'))
serial_number.comment = """The serial number of the Hard Disk in lower case.""" serial_number.comment = """The serial number of the Hard Disk in lower case."""
time = Column(SmallInteger, nullable=False) time = Column(SmallInteger, nullable=False)
@property
def final_user_code(self):
""" show the final_user_code of the last action Allocate."""
actions = self.device.actions
actions.sort(key=lambda x: x.created)
for e in reversed(actions):
if isinstance(e, Allocate) and e.created < self.created:
return e.final_user_code
@property
def hours_of_use(self):
"""Show how many hours is used one device from the last check"""
actions = self.device.actions
actions.sort(key=lambda x: x.created)
# import pdb; pdb.set_trace()
for e in reversed(actions):
if isinstance(e, Snapshot) and e.created < self.created:
return self.time - self.get_last_power_cycle(e)
if isinstance(e, Live) and e.created < self.created:
return self.time - e.time
def get_last_power_cycle(self, snapshot):
test_hdd= [a for a in snapshot.actions if a.type == "TestDataStorage"]
if not (test_hdd and snapshot.device.allocated):
return 0
return test_hdd[0].power_cycle_count
class Organize(JoinedTableMixin, ActionWithMultipleDevices): class Organize(JoinedTableMixin, ActionWithMultipleDevices):
"""The act of manipulating/administering/supervising/controlling """The act of manipulating/administering/supervising/controlling

View File

@ -415,6 +415,7 @@ class Live(ActionWithOneDevice):
final_user_code = SanitizedStr(data_key="finalUserCode", dump_only=True) final_user_code = SanitizedStr(data_key="finalUserCode", dump_only=True)
serial_number = SanitizedStr(data_key="serialNumber", dump_only=True) serial_number = SanitizedStr(data_key="serialNumber", dump_only=True)
time = Integer(dump_only=False) time = Integer(dump_only=False)
hours_of_use = Integer(dump_only=False)
class Organize(ActionWithMultipleDevices): class Organize(ActionWithMultipleDevices):

View File

@ -255,6 +255,7 @@ def test_live(user: UserClient, app: Devicehub):
db_device = Device.query.filter_by(id=1).one() db_device = Device.query.filter_by(id=1).one()
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1, post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
"devices": [device_id], "description": "aaa", "devices": [device_id], "description": "aaa",
"finalUserCode": "abcdefjhi",
"startTime": "2020-11-01T02:00:00+00:00", "startTime": "2020-11-01T02:00:00+00:00",
"endTime": "2020-12-01T02:00:00+00:00" "endTime": "2020-12-01T02:00:00+00:00"
} }
@ -269,6 +270,8 @@ def test_live(user: UserClient, app: Devicehub):
action_live = [a for a in db_device.actions if a.type == 'Live'] action_live = [a for a in db_device.actions if a.type == 'Live']
assert len(action_live) == 1 assert len(action_live) == 1
assert action_live[0].time == 6293 assert action_live[0].time == 6293
assert action_live[0].hours_of_use == 0
assert action_live[0].final_user_code == post_request['finalUserCode']
assert action_live[0].serial_number == 'wd-wx11a80w7430' assert action_live[0].serial_number == 'wd-wx11a80w7430'