fixing view action trade

This commit is contained in:
Cayo Puigdefabregas 2021-04-19 19:34:22 +02:00
parent fa4edfb9a2
commit 387c2e15e5
1 changed files with 23 additions and 16 deletions

View File

@ -17,7 +17,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, RateComputer, Snapshot, VisualTest, from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest,
InitTransfer, Live, Allocate, Deallocate, InitTransfer, Live, Allocate, Deallocate,
Trade, Offer) Trade, Confirm)
from ereuse_devicehub.resources.device.models import Device, Computer, DataStorage from ereuse_devicehub.resources.device.models import Device, Computer, DataStorage
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity
@ -254,7 +254,7 @@ class ActionView(View):
a = resource_def.schema.load(json) a = resource_def.schema.load(json)
Model = db.Model._decl_class_registry.data[json['type']]() Model = db.Model._decl_class_registry.data[json['type']]()
action = Model(**a) action = Model(**a)
if json['type'] == Offer.t: if json['type'] == Trade.t:
return self.offer(action) return self.offer(action)
db.session.add(action) db.session.add(action)
db.session().final_flush() db.session().final_flush()
@ -341,7 +341,8 @@ class ActionView(View):
"""Perform a InitTransfer action to change author_id of device""" """Perform a InitTransfer action to change author_id of device"""
pass pass
def offer(self, offer: Offer): def offer(self, offer: Trade):
self.create_first_confirmation(offer)
self.create_phantom_account(offer) self.create_phantom_account(offer)
db.session.add(offer) db.session.add(offer)
self.create_automatic_trade(offer) self.create_automatic_trade(offer)
@ -352,7 +353,17 @@ class ActionView(View):
db.session.commit() db.session.commit()
return ret return ret
def create_phantom_account(self, offer): def create_first_confirmation(self, offer: Trade) -> None:
"""Do the first confirmation for the user than do the action"""
# check than the user than want to do the action is one of the users
# involved in the action
assert g.user.id in [offer.user_from_id, offer.user_to_id]
confirm = Confirm(user=g.user, trade=offer, devices=offer.devices)
db.session.add(confirm)
def create_phantom_account(self, offer) -> None:
""" """
If exist both users not to do nothing If exist both users not to do nothing
If exist from but not to: If exist from but not to:
@ -363,9 +374,6 @@ class ActionView(View):
""" """
if offer.user_from_id and offer.user_to_id: if offer.user_from_id and offer.user_to_id:
# check than the user than want to do the action is one of the users
# involved in the action
assert g.user.id in [offer.user_from_id, offer.user_to_id]
return return
if offer.user_from_id and not offer.user_to_id: if offer.user_from_id and not offer.user_to_id:
@ -393,7 +401,8 @@ class ActionView(View):
db.session.add(user) db.session.add(user)
offer.user_from = user offer.user_from = user
def create_automatic_trade(self, offer: Offer): def create_automatic_trade(self, offer: Trade) -> None:
# not do nothing if it's neccesary confirmation explicity
if offer.confirm: if offer.confirm:
return return
@ -404,11 +413,9 @@ class ActionView(View):
for c in dev.components: for c in dev.components:
c.owner = offer.user_to c.owner = offer.user_to
# Create a new Trade # Create a new Confirmation for the user who does not perform the action
trade = Trade(accepted_by_from=True, user = offer.user_from
accepted_by_to=True, if g.user == offer.user_from:
confirm_transfer=True, user = offer.user_to
offer=offer, confirm = Confirm(user=user, trade=offer, devices=offer.devices)
devices=offer.devices db.session.add(confirm)
)
db.session.add(trade)