Build action moveOn

This commit is contained in:
Cayo Puigdefabregas 2021-09-03 16:06:53 +02:00
parent 1aaf0a9ae7
commit 0d73bb1f63
5 changed files with 76 additions and 61 deletions

View File

@ -24,6 +24,7 @@ def get_inv():
raise ValueError("Inventory value is not specified")
return INV
def upgrade():
op.add_column("trade_document", sa.Column("weight", sa.Float(decimal_return_scale=2), nullable=True), schema=f'{get_inv()}')

View File

@ -62,6 +62,15 @@ _sorted_actions = {
'order_by': lambda: Action.end_time,
'collection_class': SortedSet
}
def sorted_actions_by(data):
return {
'order_by': lambda: data,
'collection_class': SortedSet
}
"""For db.backref, return the actions sorted by end_time."""
@ -1642,6 +1651,35 @@ class MakeAvailable(ActionWithMultipleDevices):
pass
class MoveOnContainer(JoinedTableMixin, ActionWithMultipleTradeDocuments):
"""Action than certify one movement of some indescriptible material of
one container to an other."""
weight = db.Column(db.Float(nullable=True))
weight.comment = """Weight than go to recycling"""
container_from_id = db.Column(
db.BigInteger,
db.ForeignKey('trade_document.id'),
nullable=False
)
container_from = db.relationship(
'TradeDocument',
primaryjoin='MoveOnContainer.container_from_id == TradeDocument.id',
)
container_from_id.comment = """This is the trade document used as container in a incoming lot"""
container_to_id = db.Column(
db.BigInteger,
db.ForeignKey('trade_document.id'),
nullable=False
)
container_to = db.relationship(
'TradeDocument',
primaryjoin='MoveOnContainer.container_to_id == TradeDocument.id',
)
container_to_id.comment = """This is the trade document used as container in a outgoing lot"""
class Migrate(JoinedTableMixin, ActionWithMultipleDevices):
"""Moves the devices to a new database/inventory. Devices cannot be
modified anymore at the previous database.

View File

@ -33,8 +33,8 @@ from ereuse_devicehub.resources.documents.device_row import (DeviceRow, StockRow
from ereuse_devicehub.resources.lot import LotView
from ereuse_devicehub.resources.lot.models import Lot
from ereuse_devicehub.resources.hash_reports import insert_hash, ReportHash, verify_hash
from ereuse_devicehub.resources.documents.models import RecycleDocument
from ereuse_devicehub.resources.documents.schemas import RecycleDocument as sh_document
# from ereuse_devicehub.resources.documents.models import RecycleDocument
# from ereuse_devicehub.resources.documents.schemas import RecycleDocument as sh_document
class Format(enum.Enum):
@ -291,24 +291,24 @@ class StampsView(View):
result=result)
class RecycleDocumentView(View):
"""
This view allow save one document as a proof of one container with some weight was send to recycling.
"""
# class RecycleDocumentView(View):
# """
# This view allow save one document as a proof of one container with some weight was send to recycling.
# """
def post(self):
# import pdb; pdb.set_trace()
data = request.get_data()
schema = sh_document()
doc = schema.loads(data)
document = RecycleDocument(**doc)
db.session.add(document)
# def post(self):
# # import pdb; pdb.set_trace()
# data = request.get_data()
# schema = sh_document()
# doc = schema.loads(data)
# document = RecycleDocument(**doc)
# db.session.add(document)
db.session().final_flush()
ret = jsonify(document)
ret.status_code = 201
db.session.commit()
return ret
# db.session().final_flush()
# ret = jsonify(document)
# ret.status_code = 201
# db.session.commit()
# return ret
class InternalStatsView(DeviceView):
@ -457,6 +457,5 @@ class DocumentDef(Resource):
wbconf_view = app.auth.requires_auth(wbconf_view)
self.add_url_rule('/wbconf/<string:wbtype>', view_func=wbconf_view, methods=get)
recycle_doc_view = RecycleDocumentView.as_view('RecycleDocumentView', definition=self, auth=app.auth)
self.add_url_rule('/recycle/', defaults={}, view_func=recycle_doc_view, methods={'POST'})
# recycle_doc_view = RecycleDocumentView.as_view('RecycleDocumentView', definition=self, auth=app.auth)
# self.add_url_rule('/recycle/', defaults={}, view_func=recycle_doc_view, methods={'POST'})

View File

@ -64,26 +64,3 @@ class DataWipeDocument(JoinedTableMixin, Document):
def __str__(self) -> str:
return '{0.file_name}'.format(self)
class RecycleDocument(JoinedTableMixin, Document):
"""Document than proof how any of weight go to recycling."""
weight = db.Column(db.Float(nullable=True))
weight.comment = """Weight than go to recycling"""
trade_document_id = db.Column(db.BigInteger, db.ForeignKey('trade_document.id'))
trade_document_id.comment = """This is the trade document used for send material to recyle"""
lot_id = db.Column(UUID(as_uuid=True),
db.ForeignKey('lot.id'),
nullable=False)
lot_id.comment = """This lot is the input lot if the material that will then go definitively to recycling"""
lot = db.relationship('Lot',
backref=backref('recycling_documents',
lazy=True,
cascade=CASCADE_OWN,
**_sorted_documents),
primaryjoin='RecycleDocument.lot_id == Lot.id')
def __str__(self) -> str:
return '{0.file_name}'.format(self)

View File

@ -35,21 +35,21 @@ class DataWipeDocument(Thing):
data['document_type'] = 'DataWipeDocument'
class RecycleDocument(Thing):
__doc__ = m.RecycleDocument.__doc__
file_hash = SanitizedStr(data_key='hash',
default='',
description=m.RecycleDocument.file_hash.comment,
validate=validate.Length(max=64))
weight = Float(required=False, validate=Range(0.1), description=m.RecycleDocument.weight.comment)
lot = NestedOn('Lot', only_query='id', description=m.RecycleDocument.lot.__doc__)
# class RecycleDocument(Thing):
# __doc__ = m.RecycleDocument.__doc__
# file_hash = SanitizedStr(data_key='hash',
# default='',
# description=m.RecycleDocument.file_hash.comment,
# validate=validate.Length(max=64))
# weight = Float(required=False, validate=Range(0.1), description=m.RecycleDocument.weight.comment)
# lot = NestedOn('Lot', only_query='id', description=m.RecycleDocument.lot.__doc__)
@post_load
def get_trade_document(self, data):
tradedocument = TradeDocument.query.filter_by(file_hash=data['file_hash']).one()
data['trade_document_id'] = tradedocument.id
data['file_name'] = tradedocument.file_name
data['date'] = tradedocument.date
data['id_document'] = tradedocument.id_document
data['url'] = tradedocument.url
data['document_type'] = 'RecycleDocument'
# @post_load
# def get_trade_document(self, data):
# tradedocument = TradeDocument.query.filter_by(file_hash=data['file_hash']).one()
# data['trade_document_id'] = tradedocument.id
# data['file_name'] = tradedocument.file_name
# data['date'] = tradedocument.date
# data['id_document'] = tradedocument.id_document
# data['url'] = tradedocument.url
# data['document_type'] = 'RecycleDocument'