diff --git a/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py b/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py index 85aecb55..48a74602 100644 --- a/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py +++ b/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py @@ -60,8 +60,6 @@ def upgrade(): op.drop_table('live', schema=f'{get_inv()}') op.create_table('live', 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, comment='The serial number of the Hard Disk in lower case.'), sa.Column('time', sa.SmallInteger(), nullable=True), diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 3436993d..c48cfd5c 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1299,13 +1299,39 @@ class Live(JoinedWithOneDeviceMixin, ActionWithOneDevice): information about its state (in the form of a ``Snapshot`` action) 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.comment = """The serial number of the Hard Disk in lower case.""" 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): """The act of manipulating/administering/supervising/controlling diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 864043c9..c4aeeca0 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -415,6 +415,7 @@ class Live(ActionWithOneDevice): final_user_code = SanitizedStr(data_key="finalUserCode", dump_only=True) serial_number = SanitizedStr(data_key="serialNumber", dump_only=True) time = Integer(dump_only=False) + hours_of_use = Integer(dump_only=False) class Organize(ActionWithMultipleDevices): diff --git a/tests/test_action.py b/tests/test_action.py index 79cfa89c..633bee92 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -255,6 +255,7 @@ def test_live(user: UserClient, app: Devicehub): db_device = Device.query.filter_by(id=1).one() 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" } @@ -269,6 +270,8 @@ def test_live(user: UserClient, app: Devicehub): action_live = [a for a in db_device.actions if a.type == 'Live'] assert len(action_live) == 1 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'