cleaning and fixing validates of trade and confirms

This commit is contained in:
Cayo Puigdefabregas 2021-05-10 11:50:13 +02:00
parent 2808da24ba
commit 7593c4e7df

View file

@ -462,15 +462,11 @@ class Confirm(ActionWithMultipleDevices):
action = NestedOn('Action', only_query='id') action = NestedOn('Action', only_query='id')
@validates_schema @validates_schema
def validate_confirm(self, data: dict): def validate_revoke(self, data: dict):
acceptances = copy.copy(data['action'].acceptances) for dev in data['devices']:
acceptances.reverse() # if device not exist in the Trade, then this query is wrong
for ac in acceptances: if not dev in data['action'].devices:
if ac.user == g.user and ac.t == 'ConfirmRevoke': txt = "Device {} not exist in the trade".format(dev.devicehub_id)
return data
if ac.user == g.user:
txt = "you are confirmed this action before"
raise ValidationError(txt) raise ValidationError(txt)
@ -480,18 +476,10 @@ class Revoke(ActionWithMultipleDevices):
@validates_schema @validates_schema
def validate_revoke(self, data: dict): def validate_revoke(self, data: dict):
acceptances = copy.copy(data['action'].acceptances) for dev in data['devices']:
acceptances.reverse() # if device not exist in the Trade, then this query is wrong
# import pdb; pdb.set_trace() if not dev in data['action'].devices:
for ac in acceptances: txt = "Device {} not exist in the trade".format(dev.devicehub_id)
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"
raise ValidationError(txt)
txt = "you can't revoke this action because you did not confirm ir before"
raise ValidationError(txt) raise ValidationError(txt)
@ -501,14 +489,11 @@ class ConfirmRevoke(ActionWithMultipleDevices):
@validates_schema @validates_schema
def validate_revoke(self, data: dict): def validate_revoke(self, data: dict):
acceptances = copy.copy(data['action'].acceptances) # import pdb; pdb.set_trace()
acceptances.reverse() for dev in data['devices']:
for ac in acceptances: # if device not exist in the Trade, then this query is wrong
if ac.user == g.user and not ac.t == 'ConfirmRevoke': if not dev in data['action'].devices:
return data txt = "Device {} not exist in the revoke action".format(dev.devicehub_id)
if ac.user == g.user and ac.t == 'ConfirmRevoke':
txt = "you are revoke this action before"
raise ValidationError(txt) raise ValidationError(txt)
@ -531,6 +516,19 @@ class Trade(ActionWithMultipleDevices):
@validates_schema @validates_schema
def validate_lot(self, data: dict): 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 data['devices'] = data['lot'].devices
@validates_schema @validates_schema
@ -566,13 +564,6 @@ class Trade(ActionWithMultipleDevices):
if data['user_from_id']: if data['user_from_id']:
user_from = User.query.filter_by(email=data['user_from_id']).one() 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_id'] = user_from.id
data['user_from'] = user_from data['user_from'] = user_from
else: else: