From 283f869b4d24e04bbcc625e90173edea83df8cd8 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 28 Oct 2021 12:19:49 +0200 Subject: [PATCH] drop remove --- ereuse_devicehub/resources/action/models.py | 8 +-- .../resources/action/views/trade.py | 3 +- ereuse_devicehub/resources/device/models.py | 59 ++++++++++++++++++- ereuse_devicehub/resources/lot/views.py | 14 +++-- 4 files changed, 72 insertions(+), 12 deletions(-) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index fbc05234..5679decd 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1583,11 +1583,11 @@ class Revoke(Confirm): """Users can revoke one confirmation of one action trade""" -class ConfirmRevoke(Confirm): - """Users can confirm and accept one action revoke""" +# class ConfirmRevoke(Confirm): +# """Users can confirm and accept one action revoke""" - def __repr__(self) -> str: - return '<{0.t} {0.id} accepted by {0.user}>'.format(self) +# def __repr__(self) -> str: +# return '<{0.t} {0.id} accepted by {0.user}>'.format(self) class Trade(JoinedTableMixin, ActionWithMultipleTradeDocuments): diff --git a/ereuse_devicehub/resources/action/views/trade.py b/ereuse_devicehub/resources/action/views/trade.py index 16f2572b..9d330ace 100644 --- a/ereuse_devicehub/resources/action/views/trade.py +++ b/ereuse_devicehub/resources/action/views/trade.py @@ -219,8 +219,9 @@ class RevokeView(ConfirmMixin): if not data['devices']: raise ValidationError('Devices not exist.') + lot = data['action'].lot for dev in data['devices']: - if not dev.trading == 'TradeConfirmed': + if not dev.trading(lot) == 'TradeConfirmed': txt = 'Some of devices do not have enough to confirm for to do a revoke' ValidationError(txt) ### End check ### diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 473ca640..ce021b55 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -301,11 +301,66 @@ class Device(Thing): return history - @property - def trading(self): + def trading(self, lot): """The trading state, or None if no Trade action has ever been performed to this device. This extract the posibilities for to do""" + if not hasattr(lot, 'trade'): + return + Status = {0: 'Trade', + 1: 'Confirm', + 2: 'TradeConfirmed', + 3: 'Revoke', + 4: 'RevokeConfirmed'} + + trade = lot.trade + user_from = trade.user_from + user_to = trade.user_to + user_from_confirm = False + user_to_confirm = False + user_from_revoke = False + user_to_revoke = False + status = 0 + confirms = {} + revokes = {} + # acceptances = copy.copy(trade.acceptances) + # acceptances = sorted(acceptances, key=lambda x: x.created) + + if not hasattr(ac, 'acceptances'): + return Status[status] + + for ac in trade.acceptances: + if ac.user not in [user_from, user_to]: + continue + + if ac.t == 'Confirm': + if ac.user == user_from: + user_from_confirm = True + elif ac.user == user_to: + user_to_confirm = True + + if ac.t == 'Revoke': + if ac.user == user_from: + user_from_revoke = True + elif ac.user == user_to: + user_to_revoke= True + + confirms = [user_from_confirm, user_to_confirm] + revokes = [user_from_revoke, user_to_revoke] + + if any(confirms): + status = 1 + if all(confirms): + status = 2 + + if any(revokes): + status = 3 + if all(revokes): + status = 4 + + def trading2(self): + """The trading state, or None if no Trade action has + ever been performed to this device. This extract the posibilities for to do""" # trade = 'Trade' confirm = 'Confirm' need_confirm = 'NeedConfirmation' diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index fcbca827..83b3b7c5 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -280,7 +280,7 @@ def delete_from_trade(lot: Lot, ids: Set[int]): # then can be revoked and deleted of the lot # Confirm of dev.trading mean that there are only one confirmation # and the first user than put this device in trade is the actual g.user - if dev.trading == 'Confirm': + if dev.trading(lot) == 'Confirm': without_confirms.add(dev) dev.reset_owner() @@ -293,12 +293,16 @@ def delete_from_trade(lot: Lot, ids: Set[int]): without_confirms = devices if without_confirms: - confirm_revoke = ConfirmRevoke( - action=revoke, - user=g.user, + phantom = lot.trade.user_to + if lot.trade.user_to == g.user: + phantom = lot.trade.user_from + + phantom_revoke = Revoke( + action=lot.trade, + user=phantom, devices=without_confirms ) - db.session.add(confirm_revoke) + db.session.add(phantom_revoke) lot.devices.difference_update(without_confirms) lot.trade.devices = lot.devices