models of new trade

This commit is contained in:
Cayo Puigdefabregas 2021-03-16 14:31:38 +01:00
parent 2115f53f54
commit d7e50be052
2 changed files with 98 additions and 24 deletions

View File

@ -0,0 +1,65 @@
"""change trade action
Revision ID: 51439cf24be8
Revises: eca457d8b2a4
Create Date: 2021-03-15 17:40:34.410408
"""
from alembic import op
from alembic import context
from sqlalchemy.dialects import postgresql
import sqlalchemy as sa
import citext
# revision identifiers, used by Alembic.
revision = '51439cf24be8'
down_revision = 'eca457d8b2a4'
branch_labels = None
depends_on = None
def get_inv():
INV = context.get_x_argument(as_dictionary=True).get('inventory')
if not INV:
raise ValueError("Inventory value is not specified")
return INV
def upgrade():
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))
user_to_string = Column(CIText())
user_from_string = Column(CIText())
++++
op.create_table('trade',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('date', sa.TIMESTAMP(timezone=True), nullable=True),
sa.Column('user_from_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.Column('user_to_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.Column('user_from_string', citext.CIText(), nullable=True),
sa.Column('user_to_string', citext.CIText(), nullable=True),
sa.Column('price', sa.Float(decimal_return_scale=4), nullable=True),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.ForeignKeyConstraint(['from_id'], [f'common.user.id'], ),
sa.ForeignKeyConstraint(['to_id'], [f'common.user.id'], ),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'
)
def downgrade():
pass

View File

@ -1446,34 +1446,43 @@ class Trade(JoinedTableMixin, ActionWithMultipleDevices):
This class and its inheritors This class and its inheritors
extend `Schema's Trade <http://schema.org/TradeAction>`_. extend `Schema's Trade <http://schema.org/TradeAction>`_.
""" """
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_from_string = Column(CIText())
user_from_string_comment = """The user outsite of devicehub that offers the device."""
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."""
user_to_string = Column(CIText())
user_to_string_comment = """The user outsite of devicehub that offers the device."""
price = Column(Float(decimal_return_scale=2), nullable=True)
date = Column(db.TIMESTAMP(timezone=True))
class ActionTrade(Trade):
"""ActionTrade Offer one lot for to do one Trade.
"""
shipping_date = Column(db.TIMESTAMP(timezone=True)) shipping_date = Column(db.TIMESTAMP(timezone=True))
shipping_date.comment = """When are the devices going to be ready shipping_date.comment = """When are the devices going to be ready
for shipping? for shipping?
""" """
invoice_number = Column(CIText()) document_id = Column(CIText())
invoice_number.comment = """The id of the invoice so they can be linked.""" document_id.comment = """The id of one document like invoice so they can be linked."""
price_id = Column(UUID(as_uuid=True), ForeignKey(Price.id)) accepted_by_from = Column(Boolean)
price = relationship(Price, accepted_by_to = Column(Boolean)
backref=backref('trade', lazy=True, uselist=False), trade = db.Column(UUID(as_uuid=True),
primaryjoin=price_id == Price.id) db.ForeignKey(Trade.id),
price_id.comment = """The price set for this trade. nullable=True)
If no price is set it is supposed that the trade was lot = db.Column(UUID(as_uuid=True),
not payed, usual in donations. db.ForeignKey(Trade.id),
""" nullable=True)
to_id = Column(UUID(as_uuid=True), ForeignKey(Agent.id), nullable=False)
# todo compute the org
to = relationship(Agent,
backref=backref('actions_to', lazy=True, **_sorted_actions),
primaryjoin=to_id == Agent.id)
to_comment = """The agent that gets the device due this deal."""
confirms_id = Column(UUID(as_uuid=True), ForeignKey(Organize.id))
confirms = relationship(Organize,
backref=backref('confirmation', lazy=True, uselist=False),
primaryjoin=confirms_id == Organize.id)
confirms_id.comment = """An organize action that this association confirms.
For example, a ``Sell`` or ``Rent``
can confirm a ``Reserve`` action.
"""
class InitTransfer(Trade): class InitTransfer(Trade):