diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 603462b8..18cc4902 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1664,7 +1664,10 @@ class MoveOnDocument(JoinedTableMixin, ActionWithMultipleTradeDocuments): ) container_from = db.relationship( 'TradeDocument', - primaryjoin='MoveOnDocument.container_from_id == TradeDocument.id', + backref=backref('containers_from', + lazy=True, + cascade=CASCADE_OWN), + primaryjoin='MoveOnDocument.container_from_id == TradeDocument.id' ) container_from_id.comment = """This is the trade document used as container in a incoming lot""" @@ -1675,6 +1678,9 @@ class MoveOnDocument(JoinedTableMixin, ActionWithMultipleTradeDocuments): ) container_to = db.relationship( 'TradeDocument', + backref=backref('containers_to', + lazy=True, + cascade=CASCADE_OWN), primaryjoin='MoveOnDocument.container_to_id == TradeDocument.id', ) container_to_id.comment = """This is the trade document used as container in a outgoing lot""" diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index a0917bef..f27a97d8 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -342,13 +342,6 @@ class Device(Thing): if action.type == 'Revoke': return action.id - @property - def confirm_status(self): - """The actual state of confirmation of one Trade, or None if no Trade action - has ever been performed to this device.""" - # TODO @cayop we need implement this functionality - return None - @property def physical(self): """The actual physical state, None otherwise.""" diff --git a/ereuse_devicehub/resources/tradedocument/models.py b/ereuse_devicehub/resources/tradedocument/models.py index 36ae370a..177b2bd3 100644 --- a/ereuse_devicehub/resources/tradedocument/models.py +++ b/ereuse_devicehub/resources/tradedocument/models.py @@ -1,3 +1,5 @@ +import copy +from contextlib import suppress from citext import CIText from flask import g @@ -101,10 +103,10 @@ class TradeDocument(Thing): revoke = 'Revoke' revoke_pending = 'Revoke Pending' confirm_revoke = 'Document Revoked' - if not self.actions: + ac = self.last_action_trading() + if not ac: return - ac = self.actions[-1] if ac.type == 'ConfirmRevokeDocument': # can to do revoke_confirmed @@ -143,6 +145,18 @@ class TradeDocument(Thing): return weight + def last_action_trading(self): + """which is the last action trading""" + with suppress(StopIteration, ValueError): + actions = copy.copy(self.actions) + actions.sort(key=lambda x: x.created) + t_trades = ['Trade', + 'Confirm', + 'ConfirmRevokeDocument', + 'RevokeDocument', + 'ConfirmDocument'] + return next(e for e in reversed(actions) if e.t in t_trades) + def _warning_actions(self, actions): """Show warning actions""" return sorted(ev for ev in actions if ev.severity >= Severity.Warning)