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
class ConfirmRevokeDef(ActionDef):
VIEW = None
SCHEMA = schemas.ConfirmRevoke
class RevokeDef(ActionDef):
VIEW = None
SCHEMA = schemas.Revoke

View file

@ -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)

View file

@ -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():

View file

@ -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()

View file

@ -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"""

View file

@ -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

View file

@ -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