From 7593c4e7dffcc4bd88bcdb4b336e0388024ba502 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 10 May 2021 11:50:13 +0200 Subject: [PATCH] cleaning and fixing validates of trade and confirms --- ereuse_devicehub/resources/action/schemas.py | 63 +++++++++----------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index c84e25dc..9808d3ce 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -462,15 +462,11 @@ class Confirm(ActionWithMultipleDevices): action = NestedOn('Action', only_query='id') @validates_schema - def validate_confirm(self, data: dict): - acceptances = copy.copy(data['action'].acceptances) - acceptances.reverse() - for ac in acceptances: - if ac.user == g.user and ac.t == 'ConfirmRevoke': - return data - - if ac.user == g.user: - txt = "you are confirmed this action before" + def validate_revoke(self, data: dict): + for dev in data['devices']: + # if device not exist in the Trade, then this query is wrong + if not dev in data['action'].devices: + txt = "Device {} not exist in the trade".format(dev.devicehub_id) raise ValidationError(txt) @@ -480,20 +476,12 @@ class Revoke(ActionWithMultipleDevices): @validates_schema def validate_revoke(self, data: dict): - acceptances = copy.copy(data['action'].acceptances) - acceptances.reverse() - # import pdb; pdb.set_trace() - for ac in acceptances: - if ac.user == g.user and not ac.t == 'ConfirmRevoke': - return data - - if ac.user == g.user and ac.t == 'ConfirmRevoke': - txt = "you are revoke this action before" + for dev in data['devices']: + # if device not exist in the Trade, then this query is wrong + if not dev in data['action'].devices: + txt = "Device {} not exist in the trade".format(dev.devicehub_id) raise ValidationError(txt) - txt = "you can't revoke this action because you did not confirm ir before" - raise ValidationError(txt) - class ConfirmRevoke(ActionWithMultipleDevices): __doc__ = m.ConfirmRevoke.__doc__ @@ -501,14 +489,11 @@ class ConfirmRevoke(ActionWithMultipleDevices): @validates_schema def validate_revoke(self, data: dict): - acceptances = copy.copy(data['action'].acceptances) - acceptances.reverse() - for ac in acceptances: - if ac.user == g.user and not ac.t == 'ConfirmRevoke': - return data - - if ac.user == g.user and ac.t == 'ConfirmRevoke': - txt = "you are revoke this action before" + # import pdb; pdb.set_trace() + for dev in data['devices']: + # if device not exist in the Trade, then this query is wrong + if not dev in data['action'].devices: + txt = "Device {} not exist in the revoke action".format(dev.devicehub_id) raise ValidationError(txt) @@ -531,6 +516,19 @@ class Trade(ActionWithMultipleDevices): @validates_schema def validate_lot(self, data: dict): + if not g.user.email in [data['user_from_id'], data['user_to_id']]: + txt = "you need to be one of the users of involved in the Trade" + raise ValidationError(txt) + + for dev in data['lot'].devices: + if not dev.owner == g.user: + txt = "you need to be the owner of the devices for to do a trade" + raise ValidationError(txt) + + if not data['lot'].owner == g.user: + txt = "you need to be the owner of the lot for to do a trade" + raise ValidationError(txt) + data['devices'] = data['lot'].devices @validates_schema @@ -566,13 +564,6 @@ class Trade(ActionWithMultipleDevices): if data['user_from_id']: user_from = User.query.filter_by(email=data['user_from_id']).one() - - # are you property of this devices? - txt = "Some of this devices don't are of this from user" - for x in data['devices']: - if not x.owner == user_from: - raise ValidationError(txt) - data['user_from_id'] = user_from.id data['user_from'] = user_from else: