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,29 +1,34 @@
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 = {
'type': 'Trade', 'type': 'Trade',
'devices': [device_id], 'devices': [device_id],
'documents': [document_id], 'documents': [document_id],
'userFrom': user2.email, 'userFrom': user2.email,
'userTo': user.email, 'userTo': user.email,
'price': 10, 'price': 10,
'date': "2020-12-01T02:00:00+00:00", 'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'], 'lot': lot['id'],
'confirm': True, 'confirm': True,
} }
""" """
@ -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,15 +159,15 @@ class TradeView():
dev.change_owner(self.trade.user_to) dev.change_owner(self.trade.user_to)
class ConfirmMixin(): class ConfirmMixin:
""" """
Very Important: Very Important:
============== ==============
All of this Views than inherit of this class is executed for users All of this Views than inherit of this class is executed for users
than is not owner of the Trade action. than is not owner of the Trade action.
The owner of Trade action executed this actions of confirm and revoke from the The owner of Trade action executed this actions of confirm and revoke from the
lot lot
""" """
@ -167,24 +192,27 @@ class ConfirmMixin():
class ConfirmView(ConfirmMixin): class ConfirmView(ConfirmMixin):
"""Handler for manager the Confirmation register from post """Handler for manager the Confirmation register from post
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade.id, 'action': trade.id,
'devices': [device_id] 'devices': [device_id]
} }
""" """
Model = Confirm Model = Confirm
def validate(self, data): def validate(self, data):
"""If there are one device than have one confirmation, """If there are one device than have one confirmation,
then remove the list this device of the list of devices of this action then remove the list this device of the list of devices of this action
""" """
real_devices = [] real_devices = []
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
@ -197,11 +225,11 @@ class ConfirmView(ConfirmMixin):
class RevokeView(ConfirmMixin): class RevokeView(ConfirmMixin):
"""Handler for manager the Revoke register from post """Handler for manager the Revoke register from post
request_revoke = { request_revoke = {
'type': 'Revoke', 'type': 'Revoke',
'action': trade.id, 'action': trade.id,
'devices': [device_id], 'devices': [device_id],
} }
""" """
@ -223,15 +251,15 @@ 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:
============== ==============
All of this Views than inherit of this class is executed for users All of this Views than inherit of this class is executed for users
than is not owner of the Trade action. than is not owner of the Trade action.
The owner of Trade action executed this actions of confirm and revoke from the The owner of Trade action executed this actions of confirm and revoke from the
lot lot
""" """
@ -256,18 +284,18 @@ class ConfirmDocumentMixin():
class ConfirmDocumentView(ConfirmDocumentMixin): class ConfirmDocumentView(ConfirmDocumentMixin):
"""Handler for manager the Confirmation register from post """Handler for manager the Confirmation register from post
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade.id, 'action': trade.id,
'documents': [document_id], 'documents': [document_id],
} }
""" """
Model = ConfirmDocument Model = ConfirmDocument
def validate(self, data): def validate(self, data):
"""If there are one device than have one confirmation, """If there are one device than have one confirmation,
then remove the list this device of the list of devices of this action then remove the list this device of the list of devices of this action
""" """
for doc in data['documents']: for doc in data['documents']:
ac = doc.trading ac = doc.trading
@ -280,11 +308,11 @@ class ConfirmDocumentView(ConfirmDocumentMixin):
class RevokeDocumentView(ConfirmDocumentMixin): class RevokeDocumentView(ConfirmDocumentMixin):
"""Handler for manager the Revoke register from post """Handler for manager the Revoke register from post
request_revoke = { request_revoke = {
'type': 'Revoke', 'type': 'Revoke',
'action': trade.id, 'action': trade.id,
'documents': [document_id], 'documents': [document_id],
} }
""" """
@ -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 ###
@ -307,11 +337,11 @@ class RevokeDocumentView(ConfirmDocumentMixin):
class ConfirmRevokeDocumentView(ConfirmDocumentMixin): class ConfirmRevokeDocumentView(ConfirmDocumentMixin):
"""Handler for manager the Confirmation register from post """Handler for manager the Confirmation register from post
request_confirm_revoke = { request_confirm_revoke = {
'type': 'ConfirmRevoke', 'type': 'ConfirmRevoke',
'action': action_revoke.id, 'action': action_revoke.id,
'documents': [document_id], 'documents': [document_id],
} }
""" """