do a better check from schema

This commit is contained in:
Cayo Puigdefabregas 2021-04-08 19:11:27 +02:00
parent ac5b55d04a
commit a9ec5de237

View file

@ -469,8 +469,10 @@ class Offer(ActionWithMultipleDevices):
document_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='documentID', required=False) document_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='documentID', required=False)
date = DateTime(data_key='date', required=False) date = DateTime(data_key='date', required=False)
price = Float(required=False, data_key='price') price = Float(required=False, data_key='price')
user_to_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False) user_to_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', missing='',
user_from_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userFrom', required=False) required=False)
user_from_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userFrom', missing='',
required=False)
code = SanitizedStr(validate=Length(max=STR_SIZE), data_key='code', required=False) code = SanitizedStr(validate=Length(max=STR_SIZE), data_key='code', required=False)
confirm = Boolean(missing=False, description="""If you need confirmation of the user confirm = Boolean(missing=False, description="""If you need confirmation of the user
you need actevate this field""") you need actevate this field""")
@ -482,51 +484,45 @@ class Offer(ActionWithMultipleDevices):
@validates_schema @validates_schema
def validate_user_to_id(self, data: dict): def validate_user_to_id(self, data: dict):
""" """
- if user_exist - if user_to exist
* confirmation * confirmation
* without confirmation * without confirmation
- if user don't exist - if user_to don't exist
* without confirmation * without confirmation
""" """
# import pdb; pdb.set_trace() if data['user_to_id']:
if data.get('user_to_id'): user_to = User.query.filter_by(email=data['user_to_id']).one()
user_to = User.query.filter_by(email=data.get('user_to_id')).one()
data['user_to_id'] = user_to.id data['user_to_id'] = user_to.id
for dev in data['devices']: data['user_to'] = user_to
dev.owner_id = user_to.id
if hasattr(dev, 'components'):
for c in dev.components:
c.owner_id = user_to.id
else: else:
data['confirm'] = False data['confirm'] = False
@validates_schema @validates_schema
def validate_user_from_id(self, data: dict): def validate_user_from_id(self, data: dict):
""" """
- if user_exist - if user_from exist
* confirmation * confirmation
* without confirmation * without confirmation
- if user don't exist - if user_from don't exist
* without confirmation * without confirmation
""" """
# import pdb; pdb.set_trace() if not (data['user_from_id'] or data['user_to_id']):
if data.get('user_from_id'): txt = "you need one user from or user to for to do a offer"
user_from = User.query.filter_by(email=data.get('user_from_id')).one() raise ValidationError(txt)
if data['user_from_id']:
user_from = User.query.filter_by(email=data['user_from_id']).one()
data['user_from_id'] = user_from.id data['user_from_id'] = user_from.id
for dev in data['devices']: data['user_from'] = user_from
dev.owner_id = user_from.id
if hasattr(dev, 'components'):
for c in dev.components:
c.owner_id = user_from.id
else: else:
data['confirm'] = False data['confirm'] = False
@validates_schema @validates_schema
def validate_code(self, data: dict): def validate_code(self, data: dict):
"""If the user not exist, you need a code to be able to do the traceability""" """If the user not exist, you need a code to be able to do the traceability"""
if data.get('user_from_id') and data.get('user_to_id'): if data['user_from_id'] and data['user_to_id']:
return return
if not data.get('code'): if not data.get('code'):