get the status and history status of one devices

This commit is contained in:
Cayo Puigdefabregas 2021-09-28 13:05:58 +02:00
parent 4ce316a9ac
commit f10f2b848f
2 changed files with 50 additions and 0 deletions

View File

@ -261,6 +261,43 @@ class Device(Thing):
with suppress(LookupError, ValueError):
return self.last_action_of(*states.Trading.actions())
@property
def status(self):
"""Show the actual status of device for this owner.
The status depend of one of this 4 actions:
- Use
- Refurbish
- Recycling
- Management
"""
from ereuse_devicehub.resources.device import states
with suppress(LookupError, ValueError):
return self.last_action_of(*states.Status.actions())
@property
def history_status(self):
"""Show the history of the status actions of the device.
The status depend of one of this 4 actions:
- Use
- Refurbish
- Recycling
- Management
"""
from ereuse_devicehub.resources.device import states
status_actions = [ac.t for ac in states.Status.actions()]
history = []
for ac in self.actions:
if not ac.t in status_actions:
continue
if not history:
history.append(ac)
continue
if ac.rol_user == history[-1].rol_user:
# get only the last action consecutive for the same user
history = history[:-1] + [ac]
return history
@property
def trading(self):
"""The trading state, or None if no Trade action has

View File

@ -83,3 +83,16 @@ class Usage(State):
Allocate = e.Allocate
Deallocate = e.Deallocate
InUse = e.Live
class Status(State):
"""Define status of device for one user.
:cvar Use: The device is in use for one final user.
:cvar Refurbish: The device is owned by one refurbisher.
:cvar Recycling: The device is sended to recycling.
:cvar Management: The device is owned by one Manager.
"""
Use = e.Use
Refurbish = e.Refurbish
Recycling = e.Recycling
Management = e.Management