create transfer in angular version for code trades

This commit is contained in:
Cayo Puigdefabregas 2022-06-01 10:29:55 +02:00
parent 2dd3b7c276
commit 7e4bdfdc69
1 changed files with 102 additions and 72 deletions

View File

@ -1,16 +1,21 @@
from flask import g from flask import g
from sqlalchemy.util import OrderedSet
from teal.marshmallow import ValidationError from teal.marshmallow import ValidationError
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action.models import (Trade, Confirm, from ereuse_devicehub.inventory.models import Transfer
Revoke, RevokeDocument, ConfirmDocument, from ereuse_devicehub.resources.action.models import (
ConfirmRevokeDocument) Confirm,
from ereuse_devicehub.resources.user.models import User ConfirmDocument,
ConfirmRevokeDocument,
Revoke,
RevokeDocument,
Trade,
)
from ereuse_devicehub.resources.lot.views import delete_from_trade from ereuse_devicehub.resources.lot.views import delete_from_trade
from ereuse_devicehub.resources.user.models import User
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 = {
@ -37,6 +42,7 @@ class TradeView():
db.session.add(self.trade) db.session.add(self.trade)
self.create_confirmations() self.create_confirmations()
self.create_automatic_trade() self.create_automatic_trade()
self.create_transfer()
def post(self): def post(self):
db.session().final_flush() db.session().final_flush()
@ -52,15 +58,15 @@ class TradeView():
# owner of the lot # owner of the lot
if self.trade.confirm: if self.trade.confirm:
if self.trade.devices: if self.trade.devices:
confirm_devs = Confirm(user=g.user, confirm_devs = Confirm(
action=self.trade, user=g.user, action=self.trade, devices=self.trade.devices
devices=self.trade.devices) )
db.session.add(confirm_devs) db.session.add(confirm_devs)
if self.trade.documents: if self.trade.documents:
confirm_docs = ConfirmDocument(user=g.user, confirm_docs = ConfirmDocument(
action=self.trade, user=g.user, action=self.trade, documents=self.trade.documents
documents=self.trade.documents) )
db.session.add(confirm_docs) db.session.add(confirm_docs)
return return
@ -70,12 +76,12 @@ 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(
action=self.trade, user=self.trade.user_from, action=self.trade, devices=self.trade.devices
devices=self.trade.devices) )
confirm_to = Confirm(user=self.trade.user_to, confirm_to = Confirm(
action=self.trade, user=self.trade.user_to, 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)
@ -124,6 +130,25 @@ class TradeView():
db.session.add(user) db.session.add(user)
self.data['user_from'] = user self.data['user_from'] = user
def create_transfer(self):
code = self.trade.code
confirm = self.trade.confirm
lot = self.trade.lot
user_from = None
user_to = None
if not self.trade.user_from.phantom:
user_from = self.trade.user_from
if not self.trade.user_to.phantom:
user_to = self.trade.user_to
if (user_from and user_to) or not code or confirm:
return
self.transfer = Transfer(
code=code, user_from=user_from, user_to=user_to, lot=lot
)
db.session.add(self.transfer)
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
if self.trade.confirm: if self.trade.confirm:
@ -134,7 +159,7 @@ class TradeView():
dev.change_owner(self.trade.user_to) dev.change_owner(self.trade.user_to)
class ConfirmMixin(): class ConfirmMixin:
""" """
Very Important: Very Important:
============== ==============
@ -184,7 +209,10 @@ class ConfirmView(ConfirmMixin):
trade = data['action'] trade = data['action']
lot = trade.lot lot = trade.lot
for dev in data['devices']: for dev in data['devices']:
if dev.trading(lot, simple=True) not in ['NeedConfirmation', 'NeedConfirmRevoke']: if dev.trading(lot, simple=True) not in [
'NeedConfirmation',
'NeedConfirmRevoke',
]:
raise ValidationError('Some devices not possible confirm.') raise ValidationError('Some devices not possible confirm.')
# Change the owner for every devices # Change the owner for every devices
@ -223,7 +251,7 @@ class RevokeView(ConfirmMixin):
self.model = delete_from_trade(lot, devices) self.model = delete_from_trade(lot, devices)
class ConfirmDocumentMixin(): class ConfirmDocumentMixin:
""" """
Very Important: Very Important:
============== ==============
@ -299,7 +327,9 @@ class RevokeDocumentView(ConfirmDocumentMixin):
for doc in data['documents']: for doc in data['documents']:
if not doc.trading in ['Document Confirmed', 'Confirm']: if not doc.trading in ['Document Confirmed', 'Confirm']:
txt = 'Some of documents do not have enough to confirm for to do a revoke' txt = (
'Some of documents do not have enough to confirm for to do a revoke'
)
ValidationError(txt) ValidationError(txt)
### End check ### ### End check ###