fixing documents trade

This commit is contained in:
Cayo Puigdefabregas 2021-06-22 16:53:10 +02:00
parent c08ccd8d34
commit f85a0f5ef4
5 changed files with 46 additions and 6 deletions

View File

@ -101,6 +101,16 @@ def upgrade():
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}' 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('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_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()}') op.create_index(op.f('ix_trade_document_updated'), 'trade_document', ['updated'], unique=False, schema=f'{get_inv()}')

View File

@ -50,7 +50,7 @@ class Device(Thing):
description='The lots where this device is directly under.') description='The lots where this device is directly under.')
rate = NestedOn('Rate', dump_only=True, description=m.Device.rate.__doc__) rate = NestedOn('Rate', dump_only=True, description=m.Device.rate.__doc__)
price = NestedOn('Price', dump_only=True, description=m.Device.price.__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='') trading = SanitizedStr(dump_only=True, description='')
physical = EnumField(states.Physical, dump_only=True, description=m.Device.physical.__doc__) physical = EnumField(states.Physical, dump_only=True, description=m.Device.physical.__doc__)
traking= EnumField(states.Traking, dump_only=True, description=m.Device.physical.__doc__) traking= EnumField(states.Traking, dump_only=True, description=m.Device.physical.__doc__)

View File

@ -1,3 +1,4 @@
import copy
from citext import CIText from citext import CIText
from flask import g from flask import g
@ -88,6 +89,32 @@ class TradeDocument(Thing):
""" """
return sorted(self.actions_multiple_docs, key=lambda x: x.created) 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): def last_action_of(self, *types):
"""Gets the last action of the given types. """Gets the last action of the given types.

View File

@ -1,4 +1,4 @@
from marshmallow.fields import DateTime, Integer from marshmallow.fields import DateTime, Integer, validate
from teal.marshmallow import SanitizedStr, URL from teal.marshmallow import SanitizedStr, URL
# from marshmallow import ValidationError, validates_schema # from marshmallow import ValidationError, validates_schema
@ -16,12 +16,16 @@ class TradeDocument(Thing):
default='', default='',
description=m.TradeDocument.id_document.comment) description=m.TradeDocument.id_document.comment)
description = SanitizedStr(default='', description = SanitizedStr(default='',
description=m.TradeDocument.description.comment) description=m.TradeDocument.description.comment,
validate=validate.Length(max=500))
file_name = SanitizedStr(data_key='filename', file_name = SanitizedStr(data_key='filename',
default='', default='',
description=m.TradeDocument.file_name.comment) description=m.TradeDocument.file_name.comment,
validate=validate.Length(max=100))
file_hash = SanitizedStr(data_key='hash', file_hash = SanitizedStr(data_key='hash',
default='', 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) url = URL(description=m.TradeDocument.url.comment)
lot = NestedOn('Lot', only_query='id', description=m.TradeDocument.lot.__doc__) lot = NestedOn('Lot', only_query='id', description=m.TradeDocument.lot.__doc__)
trading = SanitizedStr(dump_only=True, description='')

View File

@ -19,7 +19,6 @@ class TradeDocumentView(View):
def post(self): def post(self):
"""Add one document.""" """Add one document."""
# import pdb; pdb.set_trace()
data = request.get_json(validate=True) data = request.get_json(validate=True)
hash3 = data['file_hash'] hash3 = data['file_hash']
db_hash = ReportHash(hash3=hash3) db_hash = ReportHash(hash3=hash3)