From 34def7bd70423ef10841ae462b9371938fe8a3c8 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 2 Nov 2021 14:25:49 +0100 Subject: [PATCH] fixint trading fuction --- ereuse_devicehub/resources/action/__init__.py | 5 -- ereuse_devicehub/resources/action/schemas.py | 79 +++---------------- .../resources/action/views/trade.py | 55 ++++++------- .../resources/action/views/views.py | 6 +- ereuse_devicehub/resources/device/models.py | 11 ++- ereuse_devicehub/resources/device/states.py | 1 - ereuse_devicehub/resources/lot/views.py | 2 +- 7 files changed, 47 insertions(+), 112 deletions(-) diff --git a/ereuse_devicehub/resources/action/__init__.py b/ereuse_devicehub/resources/action/__init__.py index e70bd18b..331d4dec 100644 --- a/ereuse_devicehub/resources/action/__init__.py +++ b/ereuse_devicehub/resources/action/__init__.py @@ -280,11 +280,6 @@ class ConfirmDef(ActionDef): SCHEMA = schemas.Confirm -class ConfirmRevokeDef(ActionDef): - VIEW = None - SCHEMA = schemas.ConfirmRevoke - - class RevokeDef(ActionDef): VIEW = None SCHEMA = schemas.Revoke diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 7c594375..bd81590a 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -442,14 +442,15 @@ class ActionStatus(Action): if not 'devices' in data.keys(): data['devices'] = [] - @post_load - def put_rol_user(self, data: dict): - for dev in data['devices']: - if dev.trading in [None, 'Revoke', 'ConfirmRevoke']: - return data - trade = [ac for ac in dev.actions if ac.t == 'Trade'][-1] - if trade.user_to != g.user: - data['rol_user'] = trade.user_to + # @post_load + # def put_rol_user(self, data: dict): + # TODO we need rebuild this functin + # for dev in data['devices']: + # if dev.trading in [None, 'Revoke']: + # return data + # trade = [ac for ac in dev.actions if ac.t == 'Trade'][-1] + # if trade.user_to != g.user: + # data['rol_user'] = trade.user_to class Recycling(ActionStatus): @@ -635,7 +636,7 @@ class RevokeDocument(ActionWithMultipleDocuments): class ConfirmRevokeDocument(ActionWithMultipleDocuments): - __doc__ = m.ConfirmRevoke.__doc__ + __doc__ = m.ConfirmRevokeDocument.__doc__ action = NestedOn('Action', only_query='id') @validates_schema @@ -662,66 +663,6 @@ class ConfirmRevokeDocument(ActionWithMultipleDocuments): data['action'] = doc.actions[-1] -class ConfirmRevoke(ActionWithMultipleDevices): - __doc__ = m.ConfirmRevoke.__doc__ - action = NestedOn('Action', only_query='id') - - @validates_schema - def validate_revoke(self, data: dict): - for dev in data['devices']: - # if device not exist in the Trade, then this query is wrong - if not dev in data['action'].devices: - txt = "Device {} not exist in the trade".format(dev.devicehub_id) - raise ValidationError(txt) - - for doc in data.get('documents', []): - # if document not exist in the Trade, then this query is wrong - if not doc in data['action'].documents: - txt = "Document {} not exist in the trade".format(doc.file_name) - raise ValidationError(txt) - - @validates_schema - def validate_docs(self, data): - """Check if there are or no one before confirmation, - This is not checked in the view becouse the list of documents is inmutable - - """ - if not data['devices'] == OrderedSet(): - return - - documents = [] - for doc in data['documents']: - actions = copy.copy(doc.actions) - actions.reverse() - for ac in actions: - if ac == data['action']: - # If document have the last action the action for confirm - documents.append(doc) - break - - if ac.t == 'Revoke' and not ac.user == g.user: - # If document is revoke before you can Confirm now - # and revoke is an action of one other user - documents.append(doc) - break - - if ac.t == ConfirmRevoke.t and ac.user == g.user: - # If document is confirmed we don't need confirmed again - break - - if ac.t == Confirm.t: - # if onwer of trade confirm again before than this user Confirm the - # revoke, then is not possible confirm the revoke - # - # If g.user confirm the trade before do a ConfirmRevoke - # then g.user can not to do the ConfirmRevoke more - break - - if not documents: - txt = 'No there are documents with revoke for confirm' - raise ValidationError(txt) - - class Trade(ActionWithMultipleDevices): __doc__ = m.Trade.__doc__ date = DateTime(data_key='date', required=False) diff --git a/ereuse_devicehub/resources/action/views/trade.py b/ereuse_devicehub/resources/action/views/trade.py index 9d330ace..601d2b17 100644 --- a/ereuse_devicehub/resources/action/views/trade.py +++ b/ereuse_devicehub/resources/action/views/trade.py @@ -3,7 +3,7 @@ from sqlalchemy.util import OrderedSet from teal.marshmallow import ValidationError from ereuse_devicehub.db import db -from ereuse_devicehub.resources.action.models import (Trade, Confirm, ConfirmRevoke, +from ereuse_devicehub.resources.action.models import (Trade, Confirm, Revoke, RevokeDocument, ConfirmDocument, ConfirmRevokeDocument) from ereuse_devicehub.resources.user.models import User @@ -231,42 +231,43 @@ class RevokeView(ConfirmMixin): self.model = delete_from_trade(lot, ids) -class ConfirmRevokeView(ConfirmMixin): - """Handler for manager the Confirmation register from post +# class ConfirmRevokeView(ConfirmMixin): +# """Handler for manager the Confirmation register from post - request_confirm_revoke = { - 'type': 'ConfirmRevoke', - 'action': action_revoke.id, - 'devices': [device_id] - } +# request_confirm_revoke = { +# 'type': 'ConfirmRevoke', +# 'action': action_revoke.id, +# 'devices': [device_id] +# } - """ +# """ - Model = ConfirmRevoke +# Model = ConfirmRevoke - def validate(self, data): - """All devices need to have the status of revoke.""" +# def validate(self, data): +# """All devices need to have the status of revoke.""" - if not data['action'].type == 'Revoke': - txt = 'Error: this action is not a revoke action' - ValidationError(txt) +# if not data['action'].type == 'Revoke': +# txt = 'Error: this action is not a revoke action' +# ValidationError(txt) - for dev in data['devices']: - if not dev.trading == 'Revoke': - txt = 'Some of devices do not have revoke to confirm' - ValidationError(txt) +# lot = data['action'].lot +# for dev in data['devices']: +# if not dev.trading(lot) == 'Revoke': +# txt = 'Some of devices do not have revoke to confirm' +# ValidationError(txt) - devices = OrderedSet(data['devices']) - data['devices'] = devices +# devices = OrderedSet(data['devices']) +# data['devices'] = devices - # Change the owner for every devices - # data['action'] == 'Revoke' +# # Change the owner for every devices +# # data['action'] == 'Revoke' - trade = data['action'].action - for dev in devices: - dev.reset_owner() +# trade = data['action'].action +# for dev in devices: +# dev.reset_owner() - trade.lot.devices.difference_update(devices) +# trade.lot.devices.difference_update(devices) class ConfirmDocumentMixin(): diff --git a/ereuse_devicehub/resources/action/views/views.py b/ereuse_devicehub/resources/action/views/views.py index 415effb8..82e3f5e0 100644 --- a/ereuse_devicehub/resources/action/views/views.py +++ b/ereuse_devicehub/resources/action/views/views.py @@ -15,7 +15,7 @@ from ereuse_devicehub.db import db from ereuse_devicehub.query import things_response from ereuse_devicehub.resources.action.models import (Action, Snapshot, VisualTest, InitTransfer, Live, Allocate, Deallocate, - Trade, Confirm, ConfirmRevoke, Revoke) + Trade, Confirm, Revoke) from ereuse_devicehub.resources.action.views import trade as trade_view from ereuse_devicehub.resources.action.views.snapshot import SnapshotView, save_json, move_json from ereuse_devicehub.resources.action.views.documents import ErasedView @@ -235,10 +235,6 @@ class ActionView(View): revoke = trade_view.RevokeView(json, resource_def, self.schema) return revoke.post() - if json['type'] == ConfirmRevoke.t: - confirm_revoke = trade_view.ConfirmRevokeView(json, resource_def, self.schema) - return confirm_revoke.post() - if json['type'] == 'RevokeDocument': revoke = trade_view.RevokeDocumentView(json, resource_def, self.schema) return revoke.post() diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index ce021b55..95040dd0 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -323,13 +323,14 @@ class Device(Thing): status = 0 confirms = {} revokes = {} - # acceptances = copy.copy(trade.acceptances) - # acceptances = sorted(acceptances, key=lambda x: x.created) - if not hasattr(ac, 'acceptances'): + if not hasattr(trade, 'acceptances'): return Status[status] - for ac in trade.acceptances: + acceptances = copy.copy(trade.acceptances) + acceptances = sorted(acceptances, key=lambda x: x.created) + + for ac in acceptances: if ac.user not in [user_from, user_to]: continue @@ -358,6 +359,8 @@ class Device(Thing): if all(revokes): status = 4 + return Status[status] + def trading2(self): """The trading state, or None if no Trade action has ever been performed to this device. This extract the posibilities for to do""" diff --git a/ereuse_devicehub/resources/device/states.py b/ereuse_devicehub/resources/device/states.py index 5177f701..3cfefe4b 100644 --- a/ereuse_devicehub/resources/device/states.py +++ b/ereuse_devicehub/resources/device/states.py @@ -37,7 +37,6 @@ class Trading(State): Trade = e.Trade Confirm = e.Confirm Revoke = e.Revoke - ConfirmRevoke = e.ConfirmRevoke Cancelled = e.CancelTrade Sold = e.Sell Donated = e.Donate diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index 83b3b7c5..d41f2008 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -13,7 +13,7 @@ from teal.resource import View from ereuse_devicehub.db import db from ereuse_devicehub.query import things_response from ereuse_devicehub.resources.device.models import Device, Computer -from ereuse_devicehub.resources.action.models import Trade, Confirm, Revoke, ConfirmRevoke +from ereuse_devicehub.resources.action.models import Trade, Confirm, Revoke from ereuse_devicehub.resources.lot.models import Lot, Path