From b7396b4f234dfe27adebc280eb9a9e0c56ae9201 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 9 Sep 2021 13:34:24 +0200 Subject: [PATCH] adding action moveOn inside of tradeDocuments --- ereuse_devicehub/resources/action/schemas.py | 11 ++++++++++- ereuse_devicehub/resources/tradedocument/models.py | 14 +++++++++----- tests/test_action.py | 10 +++++++++- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 9021862f..ec11dd8f 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, pre_load +from marshmallow import Schema as MarshmallowSchema, ValidationError, fields as f, validates_schema, pre_load, post_load from marshmallow.fields import Boolean, DateTime, Decimal, Float, Integer, Nested, String, \ TimeDelta, UUID from marshmallow.validate import Length, OneOf, Range @@ -856,7 +856,16 @@ class MoveOnDocument(Action): 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 + + @post_load + def adding_documents(self, data): + """Adding action in the 2 TradeDocuments""" + docs = OrderedSet() + docs.add(data['container_to']) + docs.add(data['container_from']) + data['documents'] = docs diff --git a/ereuse_devicehub/resources/tradedocument/models.py b/ereuse_devicehub/resources/tradedocument/models.py index 354fbb4d..28f4a8cf 100644 --- a/ereuse_devicehub/resources/tradedocument/models.py +++ b/ereuse_devicehub/resources/tradedocument/models.py @@ -101,9 +101,8 @@ class TradeDocument(Thing): revoke = 'Revoke' revoke_pending = 'Revoke Pending' confirm_revoke = 'Document Revoked' - if not self.actions: - return + return ac = self.actions[-1] @@ -111,7 +110,7 @@ class TradeDocument(Thing): # can to do revoke_confirmed return confirm_revoke - if ac.type == 'RevokeDocument': + if ac.type == 'RevokeDocument': if ac.user == g.user: # can todo revoke_pending return revoke_pending @@ -131,9 +130,14 @@ class TradeDocument(Thing): # can to do revoke return double_confirm - def _warning_actions(self, actions): - return sorted(ev for ev in actions if ev.severity >= Severity.Warning) + @property + def total_weight(self): + """Return all weight than this container have.""" + return sum([x.weight for x in self.actions if x.type == 'MoveOnDocument']) + self.weight + def _warning_actions(self, actions): + """Show warning actions""" + return sorted(ev for ev in actions if ev.severity >= Severity.Warning) def __lt__(self, other): return self.id < other.id diff --git a/tests/test_action.py b/tests/test_action.py index 1333fe78..4d2520c0 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -2478,11 +2478,13 @@ def test_moveOnDocument(user: UserClient, user2: UserClient): user.post(res=models.Action, data=request_trade) + description = 'This is a good description' request_moveOn = { 'type': 'MoveOnDocument', 'weight': 15, 'container_from': tradedocument_from['id'], - 'container_to': id_hash + 'container_to': id_hash, + 'description': description } doc, _ = user.post(res=models.Action, data=request_moveOn) @@ -2490,5 +2492,11 @@ def test_moveOnDocument(user: UserClient, user2: UserClient): assert doc['container_from']['id'] == tradedocument_from['id'] assert doc['container_to']['id'] == tradedocument_to['id'] + mvs= models.MoveOnDocument.query.filter().first() + trade_from = TradeDocument.query.filter_by(id=tradedocument_from['id']).one() + trade_to = TradeDocument.query.filter_by(id=tradedocument_to['id']).one() + assert trade_from in mvs.documents + assert trade_to in mvs.documents + assert description == mvs.description tradedocument_to, _ = user.post(res=TradeDocument, data=request_post2) user.post(res=models.Action, data=request_moveOn, status=422)