ConfirmRevoke as a new action

This commit is contained in:
Cayo Puigdefabregas 2021-04-30 12:44:32 +02:00
parent b2bd820d50
commit 34fbf0dca6
4 changed files with 18 additions and 26 deletions

View File

@ -254,6 +254,11 @@ class ConfirmDef(ActionDef):
SCHEMA = schemas.Confirm SCHEMA = schemas.Confirm
class ConfirmRevokeDef(ActionDef):
VIEW = None
SCHEMA = schemas.ConfirmRevoke
class TradeDef(ActionDef): class TradeDef(ActionDef):
VIEW = None VIEW = None
SCHEMA = schemas.Trade SCHEMA = schemas.Trade

View File

@ -1435,8 +1435,6 @@ class CancelReservation(Organize):
class Confirm(JoinedTableMixin, ActionWithMultipleDevices): class Confirm(JoinedTableMixin, ActionWithMultipleDevices):
"""Users confirm the offer and change it to trade""" """Users confirm the offer and change it to trade"""
revoke = Column(Boolean, default=False, nullable=False)
revoke.comment = """Used for revoke and other confirm"""
user_id = db.Column(UUID(as_uuid=True), user_id = db.Column(UUID(as_uuid=True),
db.ForeignKey(User.id), db.ForeignKey(User.id),
nullable=False, nullable=False,
@ -1455,15 +1453,15 @@ class Confirm(JoinedTableMixin, ActionWithMultipleDevices):
primaryjoin='Confirm.action_id == Action.id') primaryjoin='Confirm.action_id == Action.id')
def __repr__(self) -> str: def __repr__(self) -> str:
if self.action.t in ['Offer', 'Trade']: if self.action.t in ['Trade']:
origin = 'To' origin = 'To'
if self.user == self.action.user_from: if self.user == self.action.user_from:
origin = 'From' origin = 'From'
if self.revoke:
self.t = 'Revoke'
self.type = 'Revoke'
return '<{0.t} {0.id} accepted by {1}>'.format(self, origin) return '<{0.t} {0.id} accepted by {1}>'.format(self, origin)
class ConfirmRevoke(Confirm):
pass
class Trade(JoinedTableMixin, ActionWithMultipleDevices): class Trade(JoinedTableMixin, ActionWithMultipleDevices):
"""Trade actions log the political exchange of devices between users. """Trade actions log the political exchange of devices between users.
@ -1511,13 +1509,6 @@ class Trade(JoinedTableMixin, ActionWithMultipleDevices):
cascade=CASCADE_OWN), cascade=CASCADE_OWN),
primaryjoin='Trade.lot_id == Lot.id') primaryjoin='Trade.lot_id == Lot.id')
def __repr__(self) -> str:
users_accepted = [x.user for x in self.acceptances]
if not self.user_from in users_accepted or not self.user_to in users_accepted:
self.t = 'Offer'
self.type = 'Offer'
return '<{0.t} {0.id} {0.severity} devices={0.devices!r}>'.format(self)
class InitTransfer(Trade): class InitTransfer(Trade):
"""The act of transfer ownership of devices between two agents""" """The act of transfer ownership of devices between two agents"""

View File

@ -459,37 +459,34 @@ class CancelReservation(Organize):
class Confirm(ActionWithMultipleDevices): class Confirm(ActionWithMultipleDevices):
__doc__ = m.Confirm.__doc__ __doc__ = m.Confirm.__doc__
revoke = Boolean(required=False, description="""If you want revoke an other confirmation""")
action = NestedOn('Action', only_query='id') action = NestedOn('Action', only_query='id')
@validates_schema @validates_schema
def validate_confirm(self, data: dict): def validate_confirm(self, data: dict):
if data.get('revoke'):
return data
acceptances = copy.copy(data['action'].acceptances) acceptances = copy.copy(data['action'].acceptances)
acceptances.reverse() acceptances.reverse()
for ac in acceptances: for ac in acceptances:
if ac.user == g.user and ac.revoke: if ac.user == g.user and ac.t == 'ConfirmRevoke':
return data return data
if ac.user == g.user: if ac.user == g.user:
txt = "you are confirmed this action before" txt = "you are confirmed this action before"
raise ValidationError(txt) raise ValidationError(txt)
class ConfirmRevoke(ActionWithMultipleDevices):
__doc__ = m.ConfirmRevoke.__doc__
action = NestedOn('Action', only_query='id')
@validates_schema @validates_schema
def validate_revoke(self, data: dict): def validate_revoke(self, data: dict):
if not data.get('revoke'):
return data
acceptances = copy.copy(data['action'].acceptances) acceptances = copy.copy(data['action'].acceptances)
acceptances.reverse() acceptances.reverse()
for ac in acceptances: for ac in acceptances:
if ac.user == g.user and not ac.revoke: if ac.user == g.user and not ac.t == 'ConfirmRevoke':
return data return data
if ac.user == g.user and ac.revoke: if ac.user == g.user and ac.t == 'ConfirmRevoke':
txt = "you are revoke this action before" txt = "you are revoke this action before"
raise ValidationError(txt) raise ValidationError(txt)

View File

@ -1097,10 +1097,9 @@ def test_confirm_revoke(user: UserClient, user2: UserClient):
user2.post(res=models.Action, data=request_confirm) user2.post(res=models.Action, data=request_confirm)
request_revoke = { request_revoke = {
'type': 'Confirm', 'type': 'ConfirmRevoke',
'action': trade.id, 'action': trade.id,
'devices': [], 'devices': [],
'revoke': True,
} }
user2.post(res=models.Action, data=request_revoke) user2.post(res=models.Action, data=request_revoke)