not allow than other users can insert devices, show better the status of trade for devices

This commit is contained in:
Cayo Puigdefabregas 2021-06-03 19:05:57 +02:00
parent 8d1e6bb2d7
commit a0824f986b
5 changed files with 92 additions and 12 deletions

View file

@ -290,15 +290,18 @@ class ConfirmRevokeView(ConfirmMixin):
# then g.user can not to do the ConfirmRevoke more # then g.user can not to do the ConfirmRevoke more
break break
data['devices'] = OrderedSet(real_devices) devices = OrderedSet(real_devices)
data['devices'] = devices
# Change the owner for every devices # Change the owner for every devices
trade = data['action'] # data['action'] == 'Revoke'
for dev in data['devices']:
# TODO @cayop this should be the author of confirm actions instead of trade = data['action'].action
# author of trade 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 dev.owner = trade.author
if hasattr(dev, 'components'): if hasattr(dev, 'components'):
for c in dev.components: for c in dev.components:
c.owner = trade.author c.owner = trade.author
trade.lot.devices.difference_update(devices)

View file

@ -1,5 +1,6 @@
import pathlib import pathlib
import copy import copy
from flask import g
from contextlib import suppress from contextlib import suppress
from fractions import Fraction from fractions import Fraction
from itertools import chain from itertools import chain
@ -255,12 +256,82 @@ class Device(Thing):
@property @property
def trading(self): def trading(self):
"""The actual 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.""" 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 from ereuse_devicehub.resources.device import states
with suppress(LookupError, ValueError): with suppress(LookupError, ValueError):
action = self.last_action_of(*states.Trading.actions()) action = self.last_action_of(*states.Trading.actions())
return states.Trading(action.__class__) if action.type == 'Revoke':
return action.id
@property @property
def confirm_status(self): def confirm_status(self):

View file

@ -51,9 +51,11 @@ class Device(Thing):
rate = NestedOn('Rate', dump_only=True, description=m.Device.rate.__doc__) rate = NestedOn('Rate', dump_only=True, description=m.Device.rate.__doc__)
price = NestedOn('Price', dump_only=True, description=m.Device.price.__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 = 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__) physical = EnumField(states.Physical, dump_only=True, description=m.Device.physical.__doc__)
traking= EnumField(states.Traking, 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__) 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') physical_possessor = NestedOn('Agent', dump_only=True, data_key='physicalPossessor')
production_date = DateTime('iso', production_date = DateTime('iso',
description=m.Device.updated.comment, description=m.Device.updated.comment,

View file

@ -35,6 +35,9 @@ class Trading(State):
""" """
Reserved = e.Reserve Reserved = e.Reserve
Trade = e.Trade Trade = e.Trade
Confirm = e.Confirm
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

@ -230,11 +230,12 @@ class LotDeviceView(LotBaseChildrenView):
return return
users = [g.user.id] 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 # all users involved in the trade action can modify the lot
trade_users = [lot.trade.user_from.id, lot.trade.user_to.id] # trade_users = [lot.trade.user_from.id, lot.trade.user_to.id]
if g.user in trade_users: # if g.user in trade_users:
users = trade_users # users = trade_users
devices = set(Device.query.filter(Device.id.in_(ids)).filter( devices = set(Device.query.filter(Device.id.in_(ids)).filter(
Device.owner_id.in_(users))) Device.owner_id.in_(users)))