From f85a0f5ef4ec221756d4905c18eba13092e6dbfc Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 22 Jun 2021 16:53:10 +0200 Subject: [PATCH] fixing documents trade --- .../versions/3a3601ac8224_tradedocuments.py | 10 +++++++ ereuse_devicehub/resources/device/schemas.py | 2 +- .../resources/tradedocument/models.py | 27 +++++++++++++++++++ .../resources/tradedocument/schemas.py | 12 ++++++--- .../resources/tradedocument/views.py | 1 - 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/migrations/versions/3a3601ac8224_tradedocuments.py b/ereuse_devicehub/migrations/versions/3a3601ac8224_tradedocuments.py index 18b944b9..a574047c 100644 --- a/ereuse_devicehub/migrations/versions/3a3601ac8224_tradedocuments.py +++ b/ereuse_devicehub/migrations/versions/3a3601ac8224_tradedocuments.py @@ -101,6 +101,16 @@ def upgrade(): sa.PrimaryKeyConstraint('id'), schema=f'{get_inv()}' ) + # Action document table + op.create_table('action_trade_document', + sa.Column('document_id', sa.BigInteger(), nullable=False), + sa.Column('action_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.ForeignKeyConstraint(['action_id'], [f'{get_inv()}.action.id'], ), + sa.ForeignKeyConstraint(['document_id'], [f'{get_inv()}.trade_document.id'], ), + sa.PrimaryKeyConstraint('document_id', 'action_id'), + schema=f'{get_inv()}' + ) + op.create_index('document_id', 'trade_document', ['id'], unique=False, postgresql_using='hash', schema=f'{get_inv()}') op.create_index(op.f('ix_trade_document_created'), 'trade_document', ['created'], unique=False, schema=f'{get_inv()}') op.create_index(op.f('ix_trade_document_updated'), 'trade_document', ['updated'], unique=False, schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index 828c9827..d9a658fe 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -50,7 +50,7 @@ class Device(Thing): description='The lots where this device is directly under.') rate = NestedOn('Rate', dump_only=True, description=m.Device.rate.__doc__) price = NestedOn('Price', dump_only=True, description=m.Device.price.__doc__) - trading = EnumField(states.Trading, dump_only=True, description=m.Device.trading.__doc__) + # trading = EnumField(states.Trading, dump_only=True, description=m.Device.trading.__doc__) trading = SanitizedStr(dump_only=True, description='') physical = EnumField(states.Physical, dump_only=True, description=m.Device.physical.__doc__) traking= EnumField(states.Traking, dump_only=True, description=m.Device.physical.__doc__) diff --git a/ereuse_devicehub/resources/tradedocument/models.py b/ereuse_devicehub/resources/tradedocument/models.py index 6bce51e7..e8aa4637 100644 --- a/ereuse_devicehub/resources/tradedocument/models.py +++ b/ereuse_devicehub/resources/tradedocument/models.py @@ -1,3 +1,4 @@ +import copy from citext import CIText from flask import g @@ -88,6 +89,32 @@ class TradeDocument(Thing): """ return sorted(self.actions_multiple_docs, key=lambda x: x.created) + @property + def trading(self): + """The trading state, or None if no Trade action has + ever been performed to this device. This extract the posibilities for to do""" + + confirm = 'Confirm' + to_confirm = 'ToConfirm' + revoke = 'Revoke' + + if not self.actions: + return + + actions = copy.copy(self.actions) + actions = list(reversed(actions)) + ac = actions[0] + + if ac.type == confirm: + return confirm + + if ac.type == revoke: + return revoke + + if ac.type == confirm: + if ac.user == self.owner: + return confirm + return to_confirm def last_action_of(self, *types): """Gets the last action of the given types. diff --git a/ereuse_devicehub/resources/tradedocument/schemas.py b/ereuse_devicehub/resources/tradedocument/schemas.py index 8e96e54a..fab95dc4 100644 --- a/ereuse_devicehub/resources/tradedocument/schemas.py +++ b/ereuse_devicehub/resources/tradedocument/schemas.py @@ -1,4 +1,4 @@ -from marshmallow.fields import DateTime, Integer +from marshmallow.fields import DateTime, Integer, validate from teal.marshmallow import SanitizedStr, URL # from marshmallow import ValidationError, validates_schema @@ -16,12 +16,16 @@ class TradeDocument(Thing): default='', description=m.TradeDocument.id_document.comment) description = SanitizedStr(default='', - description=m.TradeDocument.description.comment) + description=m.TradeDocument.description.comment, + validate=validate.Length(max=500)) file_name = SanitizedStr(data_key='filename', default='', - description=m.TradeDocument.file_name.comment) + description=m.TradeDocument.file_name.comment, + validate=validate.Length(max=100)) file_hash = SanitizedStr(data_key='hash', default='', - description=m.TradeDocument.file_hash.comment) + description=m.TradeDocument.file_hash.comment, + validate=validate.Length(max=64)) url = URL(description=m.TradeDocument.url.comment) lot = NestedOn('Lot', only_query='id', description=m.TradeDocument.lot.__doc__) + trading = SanitizedStr(dump_only=True, description='') diff --git a/ereuse_devicehub/resources/tradedocument/views.py b/ereuse_devicehub/resources/tradedocument/views.py index dcb1e6e7..fa66b481 100644 --- a/ereuse_devicehub/resources/tradedocument/views.py +++ b/ereuse_devicehub/resources/tradedocument/views.py @@ -19,7 +19,6 @@ class TradeDocumentView(View): def post(self): """Add one document.""" - # import pdb; pdb.set_trace() data = request.get_json(validate=True) hash3 = data['file_hash'] db_hash = ReportHash(hash3=hash3)