From 8e47af8e27ba23ca8ea5ad429f3e0bd09ce561a1 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 12 Nov 2021 10:00:43 +0100 Subject: [PATCH] refactoring trading and trading for web --- .../resources/action/views/trade.py | 4 +- ereuse_devicehub/resources/device/metrics.py | 2 +- ereuse_devicehub/resources/device/models.py | 158 ++---------------- ereuse_devicehub/resources/lot/views.py | 5 +- tests/test_action.py | 1 - 5 files changed, 15 insertions(+), 155 deletions(-) diff --git a/ereuse_devicehub/resources/action/views/trade.py b/ereuse_devicehub/resources/action/views/trade.py index 242b32ba..87a5dc81 100644 --- a/ereuse_devicehub/resources/action/views/trade.py +++ b/ereuse_devicehub/resources/action/views/trade.py @@ -184,12 +184,12 @@ class ConfirmView(ConfirmMixin): trade = data['action'] lot = trade.lot for dev in data['devices']: - if dev.trading(lot) not in ['NeedConfirmation', 'NeedConfirmRevoke']: + if dev.trading(lot, simple=True) not in ['NeedConfirmation', 'NeedConfirmRevoke']: raise ValidationError('Some devices not possible confirm.') # Change the owner for every devices for dev in data['devices']: - if dev.trading_for_web(lot) == 'NeedConfirmation': + if dev.trading(lot) == 'NeedConfirmation': user_to = data['action'].user_to dev.change_owner(user_to) diff --git a/ereuse_devicehub/resources/device/metrics.py b/ereuse_devicehub/resources/device/metrics.py index c9576fdf..f9399f95 100644 --- a/ereuse_devicehub/resources/device/metrics.py +++ b/ereuse_devicehub/resources/device/metrics.py @@ -147,7 +147,7 @@ class Metrics(MetricsMix): if the action is one trade action, is possible than have a list of confirmations. Get the doble confirm for to know if this trade is confirmed or not. """ - return self.device.trading(self.act.lot) + return self.device.trading(self.act.lot, simple=True) def get_trade(self): """ diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 0c3d03c7..e7297fcd 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -313,12 +313,13 @@ class Device(Thing): @property def tradings(self): - return {str(x.id): self.trading_for_web(x.lot) for x in self.actions if x.t == 'Trade'} + return {str(x.id): self.trading(x.lot) for x in self.actions if x.t == 'Trade'} - def trading_for_web(self, lot): + def trading(self, lot, simple=None): """The trading state, or None if no Trade action has ever been performed to this device. This extract the posibilities for to do. - This method is performed for show in the web.""" + This method is performed for show in the web. + If you need to do one simple and generic response you can put simple=True for that.""" if not hasattr(lot, 'trade'): return @@ -348,6 +349,9 @@ class Device(Thing): if ac.t == 'Confirm' and ac.action == trade: if status in [0, 6]: + if simple: + status = 2 + continue status = 1 last_user = ac.user if ac.user == user_from and user_to == g.user: @@ -369,6 +373,9 @@ class Device(Thing): if ac.t == 'Revoke' and ac.action == trade: if status == 3: + if simple: + status = 5 + continue status = 4 last_user = ac.user if ac.user == user_from and user_to == g.user: @@ -390,151 +397,6 @@ class Device(Thing): return Status[status] - def trading_for_web2(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. - This method is performed for show in the web.""" - if not hasattr(lot, 'trade'): - return - - Status = {0: 'Trade', - 1: 'Confirm', - 2: 'NeedConfirmation', - 3: 'TradeConfirmed', - 4: 'Revoke', - 5: 'NeedConfirmRevoke', - 6: '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 - last_action = {'confirm': 0, 'revoke': 0} - - if not hasattr(trade, 'acceptances'): - return Status[status] - - for ac in self.actions: - if ac.t not in ['Confirm', 'Revoke']: - continue - - if ac.user not in [user_from, user_to]: - continue - - if ac.t == 'Confirm' and ac.action == trade: - if ac.user == user_from: - user_from_confirm = True - last_action['confirm'] = time.mktime(ac.created.timetuple()) - user_from_revoke, user_to_revoke = False, False - elif ac.user == user_to: - user_to_confirm = True - last_action['confirm'] = time.mktime(ac.created.timetuple()) - user_from_revoke, user_to_revoke = False, False - - if ac.t == 'Revoke' and ac.action == trade: - if ac.user == user_from: - user_from_revoke = True - last_action['revoke'] = time.mktime(ac.created.timetuple()) - user_from_confirm, user_to_confirm = False, False - elif ac.user == user_to: - user_to_revoke = True - last_action['revoke'] = time.mktime(ac.created.timetuple()) - user_from_confirm, user_to_confirm = False, False - - confirms = [user_from_confirm, user_to_confirm] - revokes = [user_from_revoke, user_to_revoke] - - confirm_vs_revoke = 'confirm' if last_action['confirm'] > last_action['revoke'] else 'revoke' - if any(confirms) and confirm_vs_revoke == 'confirm': - status = 1 - if user_to_confirm and user_from == g.user: - status = 2 - if user_from_confirm and user_to == g.user: - status = 2 - - if all(confirms): - status = 3 - - if any(revokes) and confirm_vs_revoke == 'revoke': - status = 4 - if user_to_revoke and user_from == g.user: - status = 5 - if user_from_revoke and user_to == g.user: - status = 5 - if all(revokes): - status = 6 - - return Status[status] - - 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. - This method is performed for show in the web.""" - if not hasattr(lot, 'trade'): - return - - Status = {0: 'Trade', - 2: 'NeedConfirmation', - 3: 'TradeConfirmed', - 5: 'NeedConfirmRevoke', - 6: 'RevokeConfirmed'} - - trade = lot.trade - user_from = trade.user_from - user_to = trade.user_to - status = 0 - last_user = None - - if not hasattr(trade, 'acceptances'): - return Status[status] - - for ac in self.actions: - if ac.t not in ['Confirm', 'Revoke']: - continue - - if ac.user not in [user_from, user_to]: - continue - - if ac.t == 'Confirm' and ac.action == trade: - if status in [0, 6]: - status = 2 - last_user = ac.user - continue - - if status == 2: - if last_user != ac.user: - status = 3 - last_user = ac.user - continue - - if status == 5: - status = 3 - last_user = ac.user - continue - - if ac.t == 'Revoke' and ac.action == trade: - if status == 3: - status = 5 - last_user = ac.user - continue - - if status == 5: - if last_user != ac.user: - status = 6 - last_user = ac.user - continue - - if status == 2: - status = 6 - last_user = ac.user - continue - - return Status[status] - @property def revoke(self): """If the actual trading state is an revoke action, this property show diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index 85a4f7a3..dce6af62 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -231,7 +231,7 @@ class LotDeviceView(LotBaseChildrenView): return devices = set(Device.query.filter(Device.id.in_(ids)).filter( - Device.owner==g.user)) + Device.owner == g.user)) lot.devices.update(devices) @@ -276,7 +276,7 @@ def delete_from_trade(lot: Lot, devices: List): drop_of_lot = [] without_confirms = [] for dev in devices: - if dev.trading_for_web(lot) in ['NeedConfirmation', 'Confirm', 'NeedConfirmRevoke']: + if dev.trading(lot) in ['NeedConfirmation', 'Confirm', 'NeedConfirmRevoke']: drop_of_lot.append(dev) dev.reset_owner() @@ -285,7 +285,6 @@ def delete_from_trade(lot: Lot, devices: List): without_confirms.append(dev) dev.reset_owner() - revoke = Revoke(action=lot.trade, user=g.user, devices=set(devices)) db.session.add(revoke) diff --git a/tests/test_action.py b/tests/test_action.py index 4d38131a..f37d70e0 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -1828,7 +1828,6 @@ def test_trade_case2(user: UserClient, user2: UserClient): device1, device2 = trade.devices - # import pdb; pdb.set_trace() assert device1.actions[-2].t == 'Trade' assert device1.actions[-1].t == 'Confirm' assert device1.actions[-1].user == trade.user_to