change offer with new requirements

This commit is contained in:
Cayo Puigdefabregas 2021-04-07 10:49:28 +02:00
parent 9a48ea09a4
commit 6fde0cabc6
2 changed files with 53 additions and 12 deletions

View File

@ -1465,14 +1465,12 @@ class Offer(JoinedTableMixin, ActionWithMultipleDevices):
""" """
user_from_id = db.Column(UUID(as_uuid=True), user_from_id = db.Column(UUID(as_uuid=True),
db.ForeignKey(User.id), db.ForeignKey(User.id),
nullable=False, nullable=False)
default=lambda: g.user.id)
user_from = db.relationship(User, primaryjoin=user_from_id == User.id) user_from = db.relationship(User, primaryjoin=user_from_id == User.id)
user_from_comment = """The user that offers the device due this deal.""" user_from_comment = """The user that offers the device due this deal."""
user_to_id = db.Column(UUID(as_uuid=True), user_to_id = db.Column(UUID(as_uuid=True),
db.ForeignKey(User.id), db.ForeignKey(User.id),
nullable=False, nullable=False)
default=lambda: g.user.id)
user_to = db.relationship(User, primaryjoin=user_to_id == User.id) user_to = db.relationship(User, primaryjoin=user_to_id == User.id)
user_to_comment = """The user that gets the device due this deal.""" user_to_comment = """The user that gets the device due this deal."""
price = Column(Float(decimal_return_scale=2), nullable=True) price = Column(Float(decimal_return_scale=2), nullable=True)
@ -1481,6 +1479,10 @@ class Offer(JoinedTableMixin, ActionWithMultipleDevices):
date = Column(db.TIMESTAMP(timezone=True)) date = Column(db.TIMESTAMP(timezone=True))
document_id = Column(CIText()) document_id = Column(CIText())
document_id.comment = """The id of one document like invoice so they can be linked.""" document_id.comment = """The id of one document like invoice so they can be linked."""
confirm = Column(Boolean, default=False, nullable=False)
confirm.comment = """If you need confirmation of the user, you need actevate this field"""
code = Column(CIText(), nullable=True)
code.comment = """If the user not exist, you need a code to be able to do the traceability"""
lot_id = db.Column(UUID(as_uuid=True), lot_id = db.Column(UUID(as_uuid=True),
db.ForeignKey('lot.id', db.ForeignKey('lot.id',
use_alter=True, use_alter=True,

View File

@ -470,29 +470,68 @@ class Offer(ActionWithMultipleDevices):
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', required=False)
user_from_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False) user_from_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userFrom', required=False)
# lot = NestedOn('Lot', dump_only=True) code = SanitizedStr(validate=Length(max=STR_SIZE), data_key='code', required=False)
confirm = Boolean(missing=False, description="""If you need confirmation of the user
you need actevate this field""")
lot = NestedOn('Lot', lot = NestedOn('Lot',
many=False, many=False,
required=True, # todo test ensuring len(devices) >= 1 required=True,
only_query='id') only_query='id')
@validates_schema @validates_schema
def validate_user_to_id(self, data: dict): def validate_user_to_id(self, data: dict):
if 'user_to_id' in data: """
user_to = User.query.filter_by(email=data['user_to_id']).one() - if user_exist
* confirmation
* without confirmation
- if user don't exist
* without confirmation
"""
# import pdb; pdb.set_trace()
if data.get('user_to_id'):
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']: for dev in data['devices']:
dev.owner_id = user_to.id dev.owner_id = user_to.id
if hasattr(dev, 'components'): if hasattr(dev, 'components'):
for c in dev.components: for c in dev.components:
c.owner_id = user_to.id c.owner_id = user_to.id
else:
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_from_id' in data: """
user_to = User.query.filter_by(email=data['user_from_id']).one() - if user_exist
data['user_from_id'] = user_to.id * confirmation
* without confirmation
- if user don't exist
* without confirmation
"""
# import pdb; pdb.set_trace()
if data.get('user_from_id'):
user_from = User.query.filter_by(email=data.get('user_from_id')).one()
data['user_from_id'] = user_from.id
for dev in data['devices']:
dev.owner_id = user_from.id
if hasattr(dev, 'components'):
for c in dev.components:
c.owner_id = user_from.id
else:
data['confirm'] = False
@validates_schema
def validate_code(self, data: dict):
"""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'):
return
if not data.get('code'):
txt = "you need a code to be able to do the traceability"
raise ValidationError(txt)
class InitTransfer(Trade): class InitTransfer(Trade):