fixint trading fuction

This commit is contained in:
Cayo Puigdefabregas 2021-11-02 14:25:49 +01:00
parent 283f869b4d
commit 34def7bd70
7 changed files with 47 additions and 112 deletions

View file

@ -280,11 +280,6 @@ class ConfirmDef(ActionDef):
SCHEMA = schemas.Confirm SCHEMA = schemas.Confirm
class ConfirmRevokeDef(ActionDef):
VIEW = None
SCHEMA = schemas.ConfirmRevoke
class RevokeDef(ActionDef): class RevokeDef(ActionDef):
VIEW = None VIEW = None
SCHEMA = schemas.Revoke SCHEMA = schemas.Revoke

View file

@ -442,14 +442,15 @@ class ActionStatus(Action):
if not 'devices' in data.keys(): if not 'devices' in data.keys():
data['devices'] = [] data['devices'] = []
@post_load # @post_load
def put_rol_user(self, data: dict): # def put_rol_user(self, data: dict):
for dev in data['devices']: # TODO we need rebuild this functin
if dev.trading in [None, 'Revoke', 'ConfirmRevoke']: # for dev in data['devices']:
return data # if dev.trading in [None, 'Revoke']:
trade = [ac for ac in dev.actions if ac.t == 'Trade'][-1] # return data
if trade.user_to != g.user: # trade = [ac for ac in dev.actions if ac.t == 'Trade'][-1]
data['rol_user'] = trade.user_to # if trade.user_to != g.user:
# data['rol_user'] = trade.user_to
class Recycling(ActionStatus): class Recycling(ActionStatus):
@ -635,7 +636,7 @@ class RevokeDocument(ActionWithMultipleDocuments):
class ConfirmRevokeDocument(ActionWithMultipleDocuments): class ConfirmRevokeDocument(ActionWithMultipleDocuments):
__doc__ = m.ConfirmRevoke.__doc__ __doc__ = m.ConfirmRevokeDocument.__doc__
action = NestedOn('Action', only_query='id') action = NestedOn('Action', only_query='id')
@validates_schema @validates_schema
@ -662,66 +663,6 @@ class ConfirmRevokeDocument(ActionWithMultipleDocuments):
data['action'] = doc.actions[-1] 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): class Trade(ActionWithMultipleDevices):
__doc__ = m.Trade.__doc__ __doc__ = m.Trade.__doc__
date = DateTime(data_key='date', required=False) date = DateTime(data_key='date', required=False)

View file

@ -3,7 +3,7 @@ from sqlalchemy.util import OrderedSet
from teal.marshmallow import ValidationError from teal.marshmallow import ValidationError
from ereuse_devicehub.db import db 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, Revoke, RevokeDocument, ConfirmDocument,
ConfirmRevokeDocument) ConfirmRevokeDocument)
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
@ -231,42 +231,43 @@ class RevokeView(ConfirmMixin):
self.model = delete_from_trade(lot, ids) self.model = delete_from_trade(lot, ids)
class ConfirmRevokeView(ConfirmMixin): # class ConfirmRevokeView(ConfirmMixin):
"""Handler for manager the Confirmation register from post # """Handler for manager the Confirmation register from post
request_confirm_revoke = { # request_confirm_revoke = {
'type': 'ConfirmRevoke', # 'type': 'ConfirmRevoke',
'action': action_revoke.id, # 'action': action_revoke.id,
'devices': [device_id] # 'devices': [device_id]
} # }
""" # """
Model = ConfirmRevoke # Model = ConfirmRevoke
def validate(self, data): # def validate(self, data):
"""All devices need to have the status of revoke.""" # """All devices need to have the status of revoke."""
if not data['action'].type == 'Revoke': # if not data['action'].type == 'Revoke':
txt = 'Error: this action is not a revoke action' # txt = 'Error: this action is not a revoke action'
ValidationError(txt) # ValidationError(txt)
for dev in data['devices']: # lot = data['action'].lot
if not dev.trading == 'Revoke': # for dev in data['devices']:
txt = 'Some of devices do not have revoke to confirm' # if not dev.trading(lot) == 'Revoke':
ValidationError(txt) # txt = 'Some of devices do not have revoke to confirm'
# ValidationError(txt)
devices = OrderedSet(data['devices']) # devices = OrderedSet(data['devices'])
data['devices'] = devices # data['devices'] = devices
# Change the owner for every devices # # Change the owner for every devices
# data['action'] == 'Revoke' # # data['action'] == 'Revoke'
trade = data['action'].action # trade = data['action'].action
for dev in devices: # for dev in devices:
dev.reset_owner() # dev.reset_owner()
trade.lot.devices.difference_update(devices) # trade.lot.devices.difference_update(devices)
class ConfirmDocumentMixin(): class ConfirmDocumentMixin():

View file

@ -15,7 +15,7 @@ from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import (Action, Snapshot, VisualTest, from ereuse_devicehub.resources.action.models import (Action, Snapshot, VisualTest,
InitTransfer, Live, Allocate, Deallocate, 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 import trade as trade_view
from ereuse_devicehub.resources.action.views.snapshot import SnapshotView, save_json, move_json from ereuse_devicehub.resources.action.views.snapshot import SnapshotView, save_json, move_json
from ereuse_devicehub.resources.action.views.documents import ErasedView 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) revoke = trade_view.RevokeView(json, resource_def, self.schema)
return revoke.post() 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': if json['type'] == 'RevokeDocument':
revoke = trade_view.RevokeDocumentView(json, resource_def, self.schema) revoke = trade_view.RevokeDocumentView(json, resource_def, self.schema)
return revoke.post() return revoke.post()

View file

@ -323,13 +323,14 @@ class Device(Thing):
status = 0 status = 0
confirms = {} confirms = {}
revokes = {} 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] 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]: if ac.user not in [user_from, user_to]:
continue continue
@ -358,6 +359,8 @@ class Device(Thing):
if all(revokes): if all(revokes):
status = 4 status = 4
return Status[status]
def trading2(self): def trading2(self):
"""The trading state, or None if no Trade action has """The trading state, or None if no Trade action has
ever been performed to this device. This extract the posibilities for to do""" ever been performed to this device. This extract the posibilities for to do"""

View file

@ -37,7 +37,6 @@ class Trading(State):
Trade = e.Trade Trade = e.Trade
Confirm = e.Confirm Confirm = e.Confirm
Revoke = e.Revoke Revoke = e.Revoke
ConfirmRevoke = e.ConfirmRevoke
Cancelled = e.CancelTrade Cancelled = e.CancelTrade
Sold = e.Sell Sold = e.Sell
Donated = e.Donate Donated = e.Donate

View file

@ -13,7 +13,7 @@ from teal.resource import View
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.device.models import Device, Computer 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 from ereuse_devicehub.resources.lot.models import Lot, Path