From 34fbf0dca67b2afece8b733cad4530c1611a3ad4 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 30 Apr 2021 12:44:32 +0200 Subject: [PATCH] ConfirmRevoke as a new action --- ereuse_devicehub/resources/action/__init__.py | 5 +++++ ereuse_devicehub/resources/action/models.py | 17 ++++------------- ereuse_devicehub/resources/action/schemas.py | 19 ++++++++----------- tests/test_action.py | 3 +-- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/ereuse_devicehub/resources/action/__init__.py b/ereuse_devicehub/resources/action/__init__.py index fdfa15ea..40338d97 100644 --- a/ereuse_devicehub/resources/action/__init__.py +++ b/ereuse_devicehub/resources/action/__init__.py @@ -254,6 +254,11 @@ class ConfirmDef(ActionDef): SCHEMA = schemas.Confirm +class ConfirmRevokeDef(ActionDef): + VIEW = None + SCHEMA = schemas.ConfirmRevoke + + class TradeDef(ActionDef): VIEW = None SCHEMA = schemas.Trade diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 7160abfc..1db7087e 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1435,8 +1435,6 @@ class CancelReservation(Organize): class Confirm(JoinedTableMixin, ActionWithMultipleDevices): """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), db.ForeignKey(User.id), nullable=False, @@ -1455,15 +1453,15 @@ class Confirm(JoinedTableMixin, ActionWithMultipleDevices): primaryjoin='Confirm.action_id == Action.id') def __repr__(self) -> str: - if self.action.t in ['Offer', 'Trade']: + if self.action.t in ['Trade']: origin = 'To' if self.user == self.action.user_from: origin = 'From' - if self.revoke: - self.t = 'Revoke' - self.type = 'Revoke' return '<{0.t} {0.id} accepted by {1}>'.format(self, origin) +class ConfirmRevoke(Confirm): + pass + class Trade(JoinedTableMixin, ActionWithMultipleDevices): """Trade actions log the political exchange of devices between users. @@ -1511,13 +1509,6 @@ class Trade(JoinedTableMixin, ActionWithMultipleDevices): cascade=CASCADE_OWN), 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): """The act of transfer ownership of devices between two agents""" diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index f0099151..8ff82bbf 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -459,37 +459,34 @@ class CancelReservation(Organize): class Confirm(ActionWithMultipleDevices): __doc__ = m.Confirm.__doc__ - revoke = Boolean(required=False, description="""If you want revoke an other confirmation""") action = NestedOn('Action', only_query='id') - @validates_schema def validate_confirm(self, data: dict): - if data.get('revoke'): - return data - acceptances = copy.copy(data['action'].acceptances) acceptances.reverse() for ac in acceptances: - if ac.user == g.user and ac.revoke: + if ac.user == g.user and ac.t == 'ConfirmRevoke': return data if ac.user == g.user: txt = "you are confirmed this action before" raise ValidationError(txt) + +class ConfirmRevoke(ActionWithMultipleDevices): + __doc__ = m.ConfirmRevoke.__doc__ + action = NestedOn('Action', only_query='id') + @validates_schema def validate_revoke(self, data: dict): - if not data.get('revoke'): - return data - acceptances = copy.copy(data['action'].acceptances) acceptances.reverse() 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 - if ac.user == g.user and ac.revoke: + if ac.user == g.user and ac.t == 'ConfirmRevoke': txt = "you are revoke this action before" raise ValidationError(txt) diff --git a/tests/test_action.py b/tests/test_action.py index 4ac7fc0b..d6ec8041 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -1097,10 +1097,9 @@ def test_confirm_revoke(user: UserClient, user2: UserClient): user2.post(res=models.Action, data=request_confirm) request_revoke = { - 'type': 'Confirm', + 'type': 'ConfirmRevoke', 'action': trade.id, 'devices': [], - 'revoke': True, } user2.post(res=models.Action, data=request_revoke)