From 0d73bb1f630058eb5281c0db45f4451adc84591e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 3 Sep 2021 16:06:53 +0200 Subject: [PATCH] Build action moveOn --- ...c1897ce_adding_weight_to_tradedocuments.py | 1 + ereuse_devicehub/resources/action/models.py | 38 +++++++++++++++++ .../resources/documents/documents.py | 41 +++++++++---------- .../resources/documents/models.py | 23 ----------- .../resources/documents/schemas.py | 34 +++++++-------- 5 files changed, 76 insertions(+), 61 deletions(-) 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 6e259684..9be818e1 100644 --- a/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py +++ b/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py @@ -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()}') diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index fdbdf1fa..16098ead 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -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. diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index cfee0c12..50c0162f 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -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/', 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'}) diff --git a/ereuse_devicehub/resources/documents/models.py b/ereuse_devicehub/resources/documents/models.py index 3deb9784..9c557b3e 100644 --- a/ereuse_devicehub/resources/documents/models.py +++ b/ereuse_devicehub/resources/documents/models.py @@ -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) diff --git a/ereuse_devicehub/resources/documents/schemas.py b/ereuse_devicehub/resources/documents/schemas.py index 66bc2f20..d2302866 100644 --- a/ereuse_devicehub/resources/documents/schemas.py +++ b/ereuse_devicehub/resources/documents/schemas.py @@ -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'