Adding MoveOnDocument in migration and fixing schema

This commit is contained in:
Cayo Puigdefabregas 2021-09-08 13:54:10 +02:00
parent 74af5add3c
commit 6cd28a3e3a
4 changed files with 29 additions and 20 deletions

View File

@ -29,18 +29,14 @@ def upgrade():
op.add_column("trade_document", sa.Column("weight", sa.Float(decimal_return_scale=2), nullable=True), schema=f'{get_inv()}') op.add_column("trade_document", sa.Column("weight", sa.Float(decimal_return_scale=2), nullable=True), schema=f'{get_inv()}')
# DataWipeDocument table # DataWipeDocument table
op.create_table('recycle_document', op.create_table('move_on_document',
sa.Column('id', sa.BigInteger(), nullable=False), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('trade_document_id', sa.BigInteger(), nullable=False),
sa.Column(
'lot_id',
postgresql.UUID(as_uuid=True),
nullable=False
),
sa.Column("weight", sa.Float(decimal_return_scale=2), nullable=True), sa.Column("weight", sa.Float(decimal_return_scale=2), nullable=True),
sa.ForeignKeyConstraint(['lot_id'], [f'{get_inv()}.lot.id'],), sa.Column('container_from_id', sa.BigInteger(), nullable=False),
sa.ForeignKeyConstraint(['trade_document_id'], [f'{get_inv()}.trade_document.id'], ), sa.Column('container_to_id', sa.BigInteger(), nullable=False),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.document.id'], ), sa.ForeignKeyConstraint(['container_from_id'], [f'{get_inv()}.trade_document.id'], ),
sa.ForeignKeyConstraint(['container_to_id'], [f'{get_inv()}.trade_document.id'], ),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}' schema=f'{get_inv()}'
) )
@ -48,4 +44,4 @@ def upgrade():
def downgrade(): def downgrade():
op.drop_column('trade_document', 'weight', schema=f'{get_inv()}') op.drop_column('trade_document', 'weight', schema=f'{get_inv()}')
op.drop_table('recycle_document', schema=f'{get_inv()}') op.drop_table('move_on_document', schema=f'{get_inv()}')

View File

@ -315,6 +315,6 @@ class MigrateFromDef(ActionDef):
SCHEMA = schemas.MigrateFrom SCHEMA = schemas.MigrateFrom
class MoveOnContainerDef(ActionDef): class MoveOnDocumentDef(ActionDef):
VIEW = None VIEW = None
SCHEMA = schemas.MoveOnContainer SCHEMA = schemas.MoveOnDocument

View File

@ -1651,7 +1651,7 @@ class MakeAvailable(ActionWithMultipleDevices):
pass pass
class MoveOnContainer(JoinedTableMixin, ActionWithMultipleTradeDocuments): class MoveOnDocument(JoinedTableMixin, ActionWithMultipleTradeDocuments):
"""Action than certify one movement of some indescriptible material of """Action than certify one movement of some indescriptible material of
one container to an other.""" one container to an other."""
@ -1664,7 +1664,7 @@ class MoveOnContainer(JoinedTableMixin, ActionWithMultipleTradeDocuments):
) )
container_from = db.relationship( container_from = db.relationship(
'TradeDocument', 'TradeDocument',
primaryjoin='MoveOnContainer.container_from_id == TradeDocument.id', primaryjoin='MoveOnDocument.container_from_id == TradeDocument.id',
) )
container_from_id.comment = """This is the trade document used as container in a incoming lot""" container_from_id.comment = """This is the trade document used as container in a incoming lot"""
@ -1675,7 +1675,7 @@ class MoveOnContainer(JoinedTableMixin, ActionWithMultipleTradeDocuments):
) )
container_to = db.relationship( container_to = db.relationship(
'TradeDocument', 'TradeDocument',
primaryjoin='MoveOnContainer.container_to_id == TradeDocument.id', primaryjoin='MoveOnDocument.container_to_id == TradeDocument.id',
) )
container_to_id.comment = """This is the trade document used as container in a outgoing lot""" container_to_id.comment = """This is the trade document used as container in a outgoing lot"""

View File

@ -2,7 +2,7 @@ import copy
from datetime import datetime, timedelta from datetime import datetime, timedelta
from dateutil.tz import tzutc from dateutil.tz import tzutc
from flask import current_app as app, g from flask import current_app as app, g
from marshmallow import Schema as MarshmallowSchema, ValidationError, fields as f, validates_schema from marshmallow import Schema as MarshmallowSchema, ValidationError, fields as f, validates_schema, pre_load
from marshmallow.fields import Boolean, DateTime, Decimal, Float, Integer, Nested, String, \ from marshmallow.fields import Boolean, DateTime, Decimal, Float, Integer, Nested, String, \
TimeDelta, UUID TimeDelta, UUID
from marshmallow.validate import Length, OneOf, Range from marshmallow.validate import Length, OneOf, Range
@ -25,6 +25,7 @@ from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE
from ereuse_devicehub.resources.schemas import Thing from ereuse_devicehub.resources.schemas import Thing
from ereuse_devicehub.resources.user import schemas as s_user from ereuse_devicehub.resources.user import schemas as s_user
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
class Action(Thing): class Action(Thing):
@ -842,8 +843,20 @@ class MigrateFrom(Migrate):
__doc__ = m.MigrateFrom.__doc__ __doc__ = m.MigrateFrom.__doc__
class MoveOnContainer(Migrate): class MoveOnDocument(Action):
__doc__ = m.MoveOnContainer.__doc__ __doc__ = m.MoveOnDocument.__doc__
weight = Integer() weight = Integer()
container_from = NestedOn('TradeDocument', only_query='id') container_from = NestedOn('TradeDocument', only_query='id')
container_to = NestedOn('TradeDocument', only_query='id') container_to = NestedOn('TradeDocument', only_query='id')
@pre_load
def extract_container(self, data):
id_hash = data['container_to']
docs = TradeDocument.query.filter_by(owner=g.user, file_hash=id_hash).all()
if len(docs) > 1:
txt = 'This document it is associated in more than one lot'
raise ValidationError(txt)
if len(docs) < 1:
txt = 'This document not exist'
raise ValidationError(txt)
data['container_to'] = docs[0].id