fixing automatic confirmation

This commit is contained in:
Cayo Puigdefabregas 2021-06-07 17:12:30 +02:00
parent 6546332fe5
commit e9a09ec2ae
1 changed files with 27 additions and 26 deletions

View File

@ -12,7 +12,7 @@ from ereuse_devicehub.resources.lot.views import delete_from_trade
class TradeView(): class TradeView():
"""Handler for manager the trade action register from post """Handler for manager the trade action register from post
request_post = { request_post = {
'type': 'Trade', 'type': 'Trade',
'devices': [device_id], 'devices': [device_id],
@ -29,14 +29,14 @@ class TradeView():
def __init__(self, data, resource_def, schema): def __init__(self, data, resource_def, schema):
self.schema = schema self.schema = schema
a = resource_def.schema.load(data) self.data = resource_def.schema.load(data)
a.pop('user_to_email', '') self.data.pop('user_to_email', '')
a.pop('user_from_email', '') self.data.pop('user_from_email', '')
self.trade = Trade(**a)
self.create_phantom_account() self.create_phantom_account()
self.trade = Trade(**self.data)
db.session.add(self.trade) db.session.add(self.trade)
self.create_automatic_trade()
self.create_confirmations() self.create_confirmations()
self.create_automatic_trade()
def post(self): def post(self):
db.session().final_flush() db.session().final_flush()
@ -52,7 +52,7 @@ class TradeView():
# owner of the lot # owner of the lot
if self.trade.confirm: if self.trade.confirm:
confirm = Confirm(user=g.user, confirm = Confirm(user=g.user,
action=self.trade, action=self.trade,
devices=self.trade.devices) devices=self.trade.devices)
db.session.add(confirm) db.session.add(confirm)
return return
@ -63,11 +63,11 @@ class TradeView():
txt = "You do not participate in this trading" txt = "You do not participate in this trading"
raise ValidationError(txt) raise ValidationError(txt)
confirm_from = Confirm(user=self.trade.user_from, confirm_from = Confirm(user=self.trade.user_from,
action=self.trade, action=self.trade,
devices=self.trade.devices) devices=self.trade.devices)
confirm_to = Confirm(user=self.trade.user_to, confirm_to = Confirm(user=self.trade.user_to,
action=self.trade, action=self.trade,
devices=self.trade.devices) devices=self.trade.devices)
db.session.add(confirm_from) db.session.add(confirm_from)
db.session.add(confirm_to) db.session.add(confirm_to)
@ -82,36 +82,40 @@ class TradeView():
The same if exist to but not from The same if exist to but not from
""" """
if self.trade.user_from and self.trade.user_to: user_from = self.data.get('user_from')
user_to = self.data.get('user_to')
code = self.data.get('code')
if user_from and user_to:
return return
if self.trade.confirm: if self.data['confirm']:
return return
if self.trade.user_from and not self.trade.user_to: if user_from and not user_to:
assert g.user == self.trade.user_from assert g.user == user_from
email = "{}_{}@dhub.com".format(str(self.trade.user_from.id), self.trade.code) email = "{}_{}@dhub.com".format(str(user_from.id), code)
users = User.query.filter_by(email=email) users = User.query.filter_by(email=email)
if users.first(): if users.first():
user = users.first() user = users.first()
self.trade.user_to = user self.data['user_to'] = user
return return
user = User(email=email, password='', active=False, phantom=True) user = User(email=email, password='', active=False, phantom=True)
db.session.add(user) db.session.add(user)
self.trade.user_to = user self.data['user_to'] = user
if not self.trade.user_from and self.trade.user_to: if not user_from and user_to:
email = "{}_{}@dhub.com".format(str(self.trade.user_to.id), self.trade.code) email = "{}_{}@dhub.com".format(str(user_to.id), code)
users = User.query.filter_by(email=email) users = User.query.filter_by(email=email)
if users.first(): if users.first():
user = users.first() user = users.first()
self.trade.user_from = user self.data['user_from'] = user
return return
user = User(email=email, password='', active=False, phantom=True) user = User(email=email, password='', active=False, phantom=True)
db.session.add(user) db.session.add(user)
self.trade.user_from = user self.data['user_from'] = user
def create_automatic_trade(self) -> None: def create_automatic_trade(self) -> None:
# not do nothing if it's neccesary confirmation explicity # not do nothing if it's neccesary confirmation explicity
@ -120,10 +124,7 @@ class TradeView():
# Change the owner for every devices # Change the owner for every devices
for dev in self.trade.devices: for dev in self.trade.devices:
dev.owner = self.trade.user_to dev.change_owner(self.trade.user_to)
if hasattr(dev, 'components'):
for c in dev.components:
c.owner = self.trade.user_to
class ConfirmMixin(): class ConfirmMixin():