diff --git a/ereuse_devicehub/resources/action/views/trade.py b/ereuse_devicehub/resources/action/views/trade.py index 7b92673c..5f5efd31 100644 --- a/ereuse_devicehub/resources/action/views/trade.py +++ b/ereuse_devicehub/resources/action/views/trade.py @@ -290,15 +290,18 @@ class ConfirmRevokeView(ConfirmMixin): # then g.user can not to do the ConfirmRevoke more break - data['devices'] = OrderedSet(real_devices) + devices = OrderedSet(real_devices) + data['devices'] = devices # Change the owner for every devices - trade = data['action'] - for dev in data['devices']: - # TODO @cayop this should be the author of confirm actions instead of - # author of trade + # data['action'] == 'Revoke' + + trade = data['action'].action + for dev in devices: + # TODO @cayop if it's possible the both users insert devices into a lot, then there are problems dev.owner = trade.author if hasattr(dev, 'components'): for c in dev.components: c.owner = trade.author + trade.lot.devices.difference_update(devices) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 65aa5669..1833b027 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -1,5 +1,6 @@ import pathlib import copy +from flask import g from contextlib import suppress from fractions import Fraction from itertools import chain @@ -255,12 +256,82 @@ class Device(Thing): @property def trading(self): - """The actual trading state, or None if no Trade action has - ever been performed to this device.""" + """The trading state, or None if no Trade action has + ever been performed to this device. This extract the posibilities for to do""" + + # import pdb; pdb.set_trace() + trade = 'Trade' + confirm = 'Confirm' + revoke = 'Revoke' + revoke_pending = 'RevokePending' + confirm_revoke = 'ConfirmRevoke' + revoke_confirmed = 'RevokeConfirmed' + + types = [trade, confirm, revoke, confirm_revoke] + actions = copy.copy(self.actions) + actions.sort(key=lambda x: x.created) + actions.reverse() + + actions_trade = [] + + # get only the actions trade of the last trade + for ac in actions: + if ac.type in types: + actions_trade.append(ac) + + if ac.type == trade: + break + + # return the correct status of trade depending of the user + + ##### CASES ##### + ## User1 == owner of trade (This user have automatic Confirmation) + ## ======================= + ## Confirmation not User1 => Revoke + ## Confirmation User1 => Revoke + ## Revoke not User1 => ConfirmRevoke + ## Revoke User1 => RevokePending + ## RevokeConfirmation => RevokeConfirmed + ## + ## + ## User2 == Not owner of trade + ## ======================= + ## Confirmation not User2 => Confirm + ## Confirmation User2 => Revoke + ## Revoke not User2 => ConfirmRevoke + ## Revoke User2 => RevokePending + ## RevokeConfirmation => RevokeConfirmed + + for ac in actions_trade: + if ac.type == confirm_revoke: + # can to do revoke_confirmed + return confirm_revoke + + if ac.type == revoke and ac.user == g.user: + # can todo revoke_pending + return revoke_pending + + if ac.type == revoke and ac.user != g.user: + # can to do confirm_revoke + return revoke + + if ac.type == confirm and (ac.user == g.user or ac.action.author == g.user): + # can to do revoke + return confirm + + if ac.type == confirm and ac.user != g.user: + # can to do confirm + return trade + + @property + def revoke(self): + """If the actual trading state is an revoke action, this property show + the id of that revoke""" from ereuse_devicehub.resources.device import states with suppress(LookupError, ValueError): action = self.last_action_of(*states.Trading.actions()) - return states.Trading(action.__class__) + if action.type == 'Revoke': + return action.id @property def confirm_status(self): diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index 9a97d72d..828c9827 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -51,9 +51,11 @@ class Device(Thing): rate = NestedOn('Rate', dump_only=True, description=m.Device.rate.__doc__) price = NestedOn('Price', dump_only=True, description=m.Device.price.__doc__) trading = EnumField(states.Trading, dump_only=True, description=m.Device.trading.__doc__) + trading = SanitizedStr(dump_only=True, description='') physical = EnumField(states.Physical, dump_only=True, description=m.Device.physical.__doc__) traking= EnumField(states.Traking, dump_only=True, description=m.Device.physical.__doc__) usage = EnumField(states.Usage, dump_only=True, description=m.Device.physical.__doc__) + revoke = UUID(dump_only=True) physical_possessor = NestedOn('Agent', dump_only=True, data_key='physicalPossessor') production_date = DateTime('iso', description=m.Device.updated.comment, diff --git a/ereuse_devicehub/resources/device/states.py b/ereuse_devicehub/resources/device/states.py index bdfd1a03..f6ad1761 100644 --- a/ereuse_devicehub/resources/device/states.py +++ b/ereuse_devicehub/resources/device/states.py @@ -35,6 +35,9 @@ class Trading(State): """ Reserved = e.Reserve 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 7e7169ba..0a3ae13b 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -230,11 +230,12 @@ class LotDeviceView(LotBaseChildrenView): return users = [g.user.id] - if lot.trade: + # Not for the moment + # if lot.trade: # all users involved in the trade action can modify the lot - trade_users = [lot.trade.user_from.id, lot.trade.user_to.id] - if g.user in trade_users: - users = trade_users + # trade_users = [lot.trade.user_from.id, lot.trade.user_to.id] + # if g.user in trade_users: + # users = trade_users devices = set(Device.query.filter(Device.id.in_(ids)).filter( Device.owner_id.in_(users)))