diff --git a/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py b/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py index 9be818e1..19f4571c 100644 --- a/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py +++ b/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py @@ -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()}') # DataWipeDocument table - op.create_table('recycle_document', - sa.Column('id', sa.BigInteger(), nullable=False), - sa.Column('trade_document_id', sa.BigInteger(), nullable=False), - sa.Column( - 'lot_id', - postgresql.UUID(as_uuid=True), - nullable=False - ), + op.create_table('move_on_document', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column("weight", sa.Float(decimal_return_scale=2), nullable=True), - sa.ForeignKeyConstraint(['lot_id'], [f'{get_inv()}.lot.id'],), - sa.ForeignKeyConstraint(['trade_document_id'], [f'{get_inv()}.trade_document.id'], ), - sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.document.id'], ), + sa.Column('container_from_id', sa.BigInteger(), nullable=False), + sa.Column('container_to_id', sa.BigInteger(), nullable=False), + 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'), schema=f'{get_inv()}' ) @@ -48,4 +44,4 @@ def upgrade(): def downgrade(): 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()}') diff --git a/ereuse_devicehub/resources/action/__init__.py b/ereuse_devicehub/resources/action/__init__.py index f33fa0df..0c3d3e9c 100644 --- a/ereuse_devicehub/resources/action/__init__.py +++ b/ereuse_devicehub/resources/action/__init__.py @@ -315,6 +315,6 @@ class MigrateFromDef(ActionDef): SCHEMA = schemas.MigrateFrom -class MoveOnContainerDef(ActionDef): +class MoveOnDocumentDef(ActionDef): VIEW = None - SCHEMA = schemas.MoveOnContainer + SCHEMA = schemas.MoveOnDocument diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 16098ead..603462b8 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1651,7 +1651,7 @@ class MakeAvailable(ActionWithMultipleDevices): pass -class MoveOnContainer(JoinedTableMixin, ActionWithMultipleTradeDocuments): +class MoveOnDocument(JoinedTableMixin, ActionWithMultipleTradeDocuments): """Action than certify one movement of some indescriptible material of one container to an other.""" @@ -1664,7 +1664,7 @@ class MoveOnContainer(JoinedTableMixin, ActionWithMultipleTradeDocuments): ) container_from = db.relationship( '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""" @@ -1675,7 +1675,7 @@ class MoveOnContainer(JoinedTableMixin, ActionWithMultipleTradeDocuments): ) container_to = db.relationship( '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""" diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index efe4df74..9021862f 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -2,7 +2,7 @@ import copy from datetime import datetime, timedelta from dateutil.tz import tzutc 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, \ TimeDelta, UUID 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.user import schemas as s_user from ereuse_devicehub.resources.user.models import User +from ereuse_devicehub.resources.tradedocument.models import TradeDocument class Action(Thing): @@ -842,8 +843,20 @@ class MigrateFrom(Migrate): __doc__ = m.MigrateFrom.__doc__ -class MoveOnContainer(Migrate): - __doc__ = m.MoveOnContainer.__doc__ +class MoveOnDocument(Action): + __doc__ = m.MoveOnDocument.__doc__ weight = Integer() container_from = 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