From 1279180700051758f4e245064df714f54c7c3c2c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 11 Nov 2021 22:50:07 +0100 Subject: [PATCH] fixing confirms --- .../resources/action/views/trade.py | 14 ++--- ereuse_devicehub/resources/lot/views.py | 57 +------------------ 2 files changed, 9 insertions(+), 62 deletions(-) diff --git a/ereuse_devicehub/resources/action/views/trade.py b/ereuse_devicehub/resources/action/views/trade.py index 842808ba..242b32ba 100644 --- a/ereuse_devicehub/resources/action/views/trade.py +++ b/ereuse_devicehub/resources/action/views/trade.py @@ -181,17 +181,17 @@ class ConfirmView(ConfirmMixin): then remove the list this device of the list of devices of this action """ real_devices = [] + trade = data['action'] + lot = trade.lot for dev in data['devices']: - ac = dev.last_action_trading - if ac.type == Confirm.t and not ac.user == g.user: - real_devices.append(dev) - - data['devices'] = OrderedSet(real_devices) + if dev.trading(lot) not in ['NeedConfirmation', 'NeedConfirmRevoke']: + raise ValidationError('Some devices not possible confirm.') # Change the owner for every devices for dev in data['devices']: - user_to = data['action'].user_to - dev.change_owner(user_to) + if dev.trading_for_web(lot) == 'NeedConfirmation': + user_to = data['action'].user_to + dev.change_owner(user_to) class RevokeView(ConfirmMixin): diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index 961ab812..85a4f7a3 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -261,8 +261,8 @@ class LotDeviceView(LotBaseChildrenView): def delete_from_trade(lot: Lot, devices: List): - users = [lot.trade.user_from.id, lot.trade.user_to.id] - if g.user.id not in users: + users = [lot.trade.user_from, lot.trade.user_to] + if g.user not in users: # theoretically this case is impossible txt = 'This is not your trade' raise ma.ValidationError(txt) @@ -303,56 +303,3 @@ def delete_from_trade(lot: Lot, devices: List): lot.devices.difference_update(OrderedSet(drop_of_lot)) return revoke - - -def delete_from_trade2(lot: Lot, ids: Set[int]): - users = [lot.trade.user_from.id, lot.trade.user_to.id] - if not g.user.id in users: - # theoretically this case is impossible - txt = 'This is not your trade' - raise ma.ValidationError(txt) - - devices = set(Device.query.filter(Device.id.in_(ids)).filter( - Device.owner_id.in_(users))) - - # Now we need to know which devices we need extract of the lot - without_confirms = set() # set of devs without confirms of user2 - - # if the trade need confirmation, then extract all devs than - # have only one confirmation and is from the same user than try to do - # now the revoke action - if lot.trade.confirm: - for dev in devices: - # if have only one confirmation - # 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(lot) == 'Confirm': - without_confirms.add(dev) - dev.reset_owner() - - # we need to mark one revoke for every devs - revoke = Revoke(action=lot.trade, user=g.user, devices=devices) - db.session.add(revoke) - - if not lot.trade.confirm: - # if the trade is with phantom account - without_confirms = devices - - if without_confirms: - 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(phantom_revoke) - - lot.devices.difference_update(without_confirms) - # TODO @cayop ?? we dont need this line - lot.trade.devices = lot.devices - - return revoke