refactoring trading and trading for web

This commit is contained in:
Cayo Puigdefabregas 2021-11-12 10:00:43 +01:00
parent 1279180700
commit 8e47af8e27
5 changed files with 15 additions and 155 deletions

View File

@ -184,12 +184,12 @@ class ConfirmView(ConfirmMixin):
trade = data['action'] trade = data['action']
lot = trade.lot lot = trade.lot
for dev in data['devices']: 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.') raise ValidationError('Some devices not possible confirm.')
# Change the owner for every devices # Change the owner for every devices
for dev in data['devices']: for dev in data['devices']:
if dev.trading_for_web(lot) == 'NeedConfirmation': if dev.trading(lot) == 'NeedConfirmation':
user_to = data['action'].user_to user_to = data['action'].user_to
dev.change_owner(user_to) dev.change_owner(user_to)

View File

@ -147,7 +147,7 @@ class Metrics(MetricsMix):
if the action is one trade action, is possible than have a list of confirmations. 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. 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): def get_trade(self):
""" """

View File

@ -313,12 +313,13 @@ class Device(Thing):
@property @property
def tradings(self): 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 """The trading state, or None if no Trade action has
ever been performed to this device. This extract the posibilities for to do. 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'): if not hasattr(lot, 'trade'):
return return
@ -348,6 +349,9 @@ class Device(Thing):
if ac.t == 'Confirm' and ac.action == trade: if ac.t == 'Confirm' and ac.action == trade:
if status in [0, 6]: if status in [0, 6]:
if simple:
status = 2
continue
status = 1 status = 1
last_user = ac.user last_user = ac.user
if ac.user == user_from and user_to == g.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 ac.t == 'Revoke' and ac.action == trade:
if status == 3: if status == 3:
if simple:
status = 5
continue
status = 4 status = 4
last_user = ac.user last_user = ac.user
if ac.user == user_from and user_to == g.user: if ac.user == user_from and user_to == g.user:
@ -390,151 +397,6 @@ class Device(Thing):
return Status[status] 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 @property
def revoke(self): def revoke(self):
"""If the actual trading state is an revoke action, this property show """If the actual trading state is an revoke action, this property show

View File

@ -231,7 +231,7 @@ class LotDeviceView(LotBaseChildrenView):
return return
devices = set(Device.query.filter(Device.id.in_(ids)).filter( devices = set(Device.query.filter(Device.id.in_(ids)).filter(
Device.owner==g.user)) Device.owner == g.user))
lot.devices.update(devices) lot.devices.update(devices)
@ -276,7 +276,7 @@ def delete_from_trade(lot: Lot, devices: List):
drop_of_lot = [] drop_of_lot = []
without_confirms = [] without_confirms = []
for dev in devices: 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) drop_of_lot.append(dev)
dev.reset_owner() dev.reset_owner()
@ -285,7 +285,6 @@ def delete_from_trade(lot: Lot, devices: List):
without_confirms.append(dev) without_confirms.append(dev)
dev.reset_owner() dev.reset_owner()
revoke = Revoke(action=lot.trade, user=g.user, devices=set(devices)) revoke = Revoke(action=lot.trade, user=g.user, devices=set(devices))
db.session.add(revoke) db.session.add(revoke)

View File

@ -1828,7 +1828,6 @@ def test_trade_case2(user: UserClient, user2: UserClient):
device1, device2 = trade.devices device1, device2 = trade.devices
# import pdb; pdb.set_trace()
assert device1.actions[-2].t == 'Trade' assert device1.actions[-2].t == 'Trade'
assert device1.actions[-1].t == 'Confirm' assert device1.actions[-1].t == 'Confirm'
assert device1.actions[-1].user == trade.user_to assert device1.actions[-1].user == trade.user_to