From 47dd5b24d861bb28144b3e53becd5128dc7a0ed2 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Sat, 3 Apr 2021 13:33:56 +0200 Subject: [PATCH] change offer-trade for 2192 aproach --- .../51439cf24be8_change_trade_action.py | 33 ++++++---- ereuse_devicehub/resources/action/models.py | 60 ++++++++----------- ereuse_devicehub/resources/action/schemas.py | 22 ++++--- 3 files changed, 58 insertions(+), 57 deletions(-) diff --git a/ereuse_devicehub/migrations/versions/51439cf24be8_change_trade_action.py b/ereuse_devicehub/migrations/versions/51439cf24be8_change_trade_action.py index 912b938c..1dbc99b4 100644 --- a/ereuse_devicehub/migrations/versions/51439cf24be8_change_trade_action.py +++ b/ereuse_devicehub/migrations/versions/51439cf24be8_change_trade_action.py @@ -27,32 +27,45 @@ def get_inv(): def upgrade(): op.drop_table('trade', schema=f'{get_inv()}') - op.create_table('trade', + op.create_table('offer', sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('date', sa.TIMESTAMP(timezone=True), nullable=True), sa.Column('price', sa.Float(decimal_return_scale=4), nullable=True), + sa.Column('lot_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('date', sa.TIMESTAMP(timezone=True), nullable=True), sa.Column('user_from_id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('user_to_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('document_id', citext.CIText(), nullable=True), + sa.Column('currency', + sa.Enum('AFN', 'ARS', 'AWG', 'AUD', 'AZN', 'BSD', 'BBD', 'BDT', 'BYR', 'BZD', 'BMD', + 'BOB', 'BAM', 'BWP', 'BGN', 'BRL', 'BND', 'KHR', 'CAD', 'KYD', 'CLP', 'CNY', + 'COP', 'CRC', 'HRK', 'CUP', 'CZK', 'DKK', 'DOP', 'XCD', 'EGP', 'SVC', 'EEK', + 'EUR', 'FKP', 'FJD', 'GHC', 'GIP', 'GTQ', 'GGP', 'GYD', 'HNL', 'HKD', 'HUF', + 'ISK', 'INR', 'IDR', 'IRR', 'IMP', 'ILS', 'JMD', 'JPY', 'JEP', 'KZT', 'KPW', + 'KRW', 'KGS', 'LAK', 'LVL', 'LBP', 'LRD', 'LTL', 'MKD', 'MYR', 'MUR', 'MXN', + 'MNT', 'MZN', 'NAD', 'NPR', 'ANG', 'NZD', 'NIO', 'NGN', 'NOK', 'OMR', 'PKR', + 'PAB', 'PYG', 'PEN', 'PHP', 'PLN', 'QAR', 'RON', 'RUB', 'SHP', 'SAR', 'RSD', + 'SCR', 'SGD', 'SBD', 'SOS', 'ZAR', 'LKR', 'SEK', 'CHF', 'SRD', 'SYP', 'TWD', + 'THB', 'TTD', 'TRY', 'TRL', 'TVD', 'UAH', 'GBP', 'USD', 'UYU', 'UZS', 'VEF', + 'VND', 'YER', 'ZWD', name='currency'), nullable=False, + comment='The currency of this price as for ISO 4217.'), - sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), + sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.trade.id'], ), sa.ForeignKeyConstraint(['user_from_id'], ['common.user.id'], ), sa.ForeignKeyConstraint(['user_to_id'], ['common.user.id'], ), + sa.ForeignKeyConstraint(['lot_id'], [f'{get_inv()}.lot.id'], ), sa.PrimaryKeyConstraint('id'), schema=f'{get_inv()}' ) - op.create_table('offer', + op.create_table('trade', sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('document_id', citext.CIText(), nullable=True), sa.Column('accepted_by_from', sa.Boolean(), nullable=False), sa.Column('accepted_by_to', sa.Boolean(), nullable=False), sa.Column('confirm_transfer', sa.Boolean(), nullable=False), - sa.Column('trade_id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('lot_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('offer_id', postgresql.UUID(as_uuid=True), nullable=False), - sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.trade.id'], ), - sa.ForeignKeyConstraint(['trade_id'], [f'{get_inv()}.trade.id'], ), - sa.ForeignKeyConstraint(['lot_id'], [f'{get_inv()}.lot.id'], ), + sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), + sa.ForeignKeyConstraint(['offer_id'], [f'{get_inv()}.offer.id'], ), sa.PrimaryKeyConstraint('id'), schema=f'{get_inv()}' ) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index cf552ca7..f40d153d 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1446,28 +1446,25 @@ class Trade(JoinedTableMixin, ActionWithMultipleDevices): This class and its inheritors extend `Schema's Trade `_. """ - user_from_id = db.Column(UUID(as_uuid=True), - db.ForeignKey(User.id), - nullable=False, - default=lambda: g.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_to_id = db.Column(UUID(as_uuid=True), - db.ForeignKey(User.id), - nullable=False, - default=lambda: g.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.""" - price = Column(Float(decimal_return_scale=2), nullable=True) - date = Column(db.TIMESTAMP(timezone=True)) - # offer = relationship("Offer", uselist=False, back_populates="Trade") + accepted_by_from = Column(Boolean, default=False) + accepted_by_from_comment = """Who do the Offer""" + accepted_by_to = Column(Boolean, default=False) + accepted_by_to_comment = """Who recive the Offer""" + confirm_transfer = Column(Boolean, default=False) + confirm_transfer_comment = """Transfer of the phisical devices it is confirmed""" + offer_id = db.Column(UUID(as_uuid=True), + db.ForeignKey('offer.id', + user_alter=True, + name='trade_offer'), + nullable=True) + offer = db.relationship('offer', + backref=db.backref('trade', uselist=False, lazy=True), + primaryjoin='Trade.id == Offer.trade_id') -# class Offer(Trade): class Offer(JoinedTableMixin, ActionWithMultipleDevices): """ActionTrade Offer one lot for to do one Trade. """ - # from ereuse_devicehub.resources.lot.models import Lot user_from_id = db.Column(UUID(as_uuid=True), db.ForeignKey(User.id), nullable=False, @@ -1481,29 +1478,22 @@ class Offer(JoinedTableMixin, ActionWithMultipleDevices): user_to = db.relationship(User, primaryjoin=user_to_id == User.id) user_to_comment = """The user that gets the device due this deal.""" price = Column(Float(decimal_return_scale=2), nullable=True) + currency = Column(DBEnum(Currency), nullable=False, default='EUR') + currency.comment = """The currency of this price as for ISO 4217.""" date = Column(db.TIMESTAMP(timezone=True)) document_id = Column(CIText()) document_id.comment = """The id of one document like invoice so they can be linked.""" - accepted_by_from = Column(Boolean, default=False) - accepted_by_from_common = """Who do the Offer""" - accepted_by_to = Column(Boolean, default=False) - confirm_transfer = Column(Boolean, default=False) - accepted_by_to_common = """Who recive the Offer""" - trade_id = db.Column(UUID(as_uuid=True), - db.ForeignKey(Trade.id), - nullable=True) - trade = db.relationship(Trade, primaryjoin=trade_id == Trade.id) lot_id = db.Column(UUID(as_uuid=True), - db.ForeignKey('information_schema.lot.id'), + db.ForeignKey('lot.id', + use_alter=True, + name='lot_offer'), nullable=True) - # lot = relationship("Lot", back_populates="offer") - # lot = db.relationship('Lot', primaryjoin='lot_id' == 'Lot.id') - # lot = relationship('Lot', - # backref=backref('lot_one', - # lazy=True, - # cascade=CASCADE_OWN, - # **_sorted_actions), - # primaryjoin=lot_id == 'Lot.id') + lot = relationship('Lot', + backref=backref('offer', + lazy=True, + uselist=False, + cascade=CASCADE_OWN), + primaryjoin='Offer.lot_id == Lot.id') class InitTransfer(Trade): diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 00970291..41f26736 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -458,10 +458,20 @@ class CancelReservation(Organize): class Trade(ActionWithMultipleDevices): __doc__ = m.Trade.__doc__ + accepted_by_from = Boolean(missing=False, description=m.Offer.accepted_by_from.comment) + accepted_by_to = Boolean(missing=False, description=m.Offer.accepted_by_to.comment) + confirm_transfer = Boolean(missing=False, description=m.Offer.confirm_transfer.comment) + offer = NestedOn('Offer', dump_only=True) + + +class Offer(Trade): + __doc__ = m.Trade.__doc__ + document_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='documentID', required=False) date = DateTime(data_key='date', required=False) price = Float(required=False, data_key='price') 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) + lot = NestedOn('Lot', dump_only=True) @validates_schema def validate_user_to_id(self, data: dict): @@ -481,18 +491,6 @@ class Trade(ActionWithMultipleDevices): data['user_from_id'] = user_to.id -class Offer(Trade): - __doc__ = m.Trade.__doc__ - document_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='documentID', required=False) - accepted_by_from = Boolean(missing=False, description=m.Offer.accepted_by_from.comment) - accepted_by_to = Boolean(missing=False, description=m.Offer.accepted_by_to.comment) - lot = NestedOn('Lot', dump_only=True) - trade = NestedOn('Trade', dump_only=True) - # lot = NestedOn('Lot', - # many=False, - # required=True, - # only_query='id') - class InitTransfer(Trade): __doc__ = m.InitTransfer.__doc__