drop remove

This commit is contained in:
Cayo Puigdefabregas 2021-10-28 12:19:49 +02:00
parent 03238acb9f
commit 283f869b4d
4 changed files with 72 additions and 12 deletions

View File

@ -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):

View File

@ -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 ###

View File

@ -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'

View File

@ -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