From f10f2b848fc09f7852d7dabacc3a3ab74ded8c33 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 28 Sep 2021 13:05:58 +0200 Subject: [PATCH] get the status and history status of one devices --- ereuse_devicehub/resources/device/models.py | 37 +++++++++++++++++++++ ereuse_devicehub/resources/device/states.py | 13 ++++++++ 2 files changed, 50 insertions(+) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index a0917bef..fc6c3cc3 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -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 diff --git a/ereuse_devicehub/resources/device/states.py b/ereuse_devicehub/resources/device/states.py index f6ad1761..5177f701 100644 --- a/ereuse_devicehub/resources/device/states.py +++ b/ereuse_devicehub/resources/device/states.py @@ -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