2021-05-10 09:47:56 +00:00
|
|
|
from flask import g
|
|
|
|
from sqlalchemy.util import OrderedSet
|
|
|
|
from teal.marshmallow import ValidationError
|
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
2021-06-24 17:09:12 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import (Trade, Confirm, ConfirmRevoke,
|
2021-06-28 14:06:07 +00:00
|
|
|
Revoke, RevokeDocument, ConfirmDocument,
|
|
|
|
ConfirmRevokeDocument)
|
2021-05-10 09:47:56 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2021-06-07 08:50:08 +00:00
|
|
|
from ereuse_devicehub.resources.lot.views import delete_from_trade
|
2021-05-10 09:47:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TradeView():
|
|
|
|
"""Handler for manager the trade action register from post
|
2021-05-19 11:59:59 +00:00
|
|
|
|
2021-05-10 09:47:56 +00:00
|
|
|
request_post = {
|
|
|
|
'type': 'Trade',
|
|
|
|
'devices': [device_id],
|
2021-05-19 11:59:59 +00:00
|
|
|
'documents': [document_id],
|
2021-05-10 09:47:56 +00:00
|
|
|
'userFrom': user2.email,
|
|
|
|
'userTo': user.email,
|
|
|
|
'price': 10,
|
|
|
|
'date': "2020-12-01T02:00:00+00:00",
|
|
|
|
'lot': lot['id'],
|
|
|
|
'confirm': True,
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, data, resource_def, schema):
|
|
|
|
self.schema = schema
|
2021-06-07 15:12:30 +00:00
|
|
|
self.data = resource_def.schema.load(data)
|
|
|
|
self.data.pop('user_to_email', '')
|
|
|
|
self.data.pop('user_from_email', '')
|
2021-05-10 09:47:56 +00:00
|
|
|
self.create_phantom_account()
|
2021-06-07 15:12:30 +00:00
|
|
|
self.trade = Trade(**self.data)
|
2021-05-10 09:47:56 +00:00
|
|
|
db.session.add(self.trade)
|
|
|
|
self.create_confirmations()
|
2021-06-07 15:12:30 +00:00
|
|
|
self.create_automatic_trade()
|
2021-05-10 09:47:56 +00:00
|
|
|
|
|
|
|
def post(self):
|
|
|
|
db.session().final_flush()
|
|
|
|
ret = self.schema.jsonify(self.trade)
|
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def create_confirmations(self) -> None:
|
|
|
|
"""Do the first confirmation for the user than do the action"""
|
|
|
|
|
|
|
|
# if the confirmation is mandatory, do automatic confirmation only for
|
|
|
|
# owner of the lot
|
|
|
|
if self.trade.confirm:
|
2021-05-21 11:16:30 +00:00
|
|
|
if self.trade.devices:
|
|
|
|
confirm_devs = Confirm(user=g.user,
|
|
|
|
action=self.trade,
|
|
|
|
devices=self.trade.devices)
|
|
|
|
db.session.add(confirm_devs)
|
|
|
|
|
|
|
|
if self.trade.documents:
|
2021-06-29 17:13:00 +00:00
|
|
|
confirm_docs = ConfirmDocument(user=g.user,
|
2021-05-21 11:16:30 +00:00
|
|
|
action=self.trade,
|
|
|
|
documents=self.trade.documents)
|
|
|
|
db.session.add(confirm_docs)
|
2021-05-10 09:47:56 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# check than the user than want to do the action is one of the users
|
|
|
|
# involved in the action
|
2021-10-25 11:24:22 +00:00
|
|
|
if g.user not in [self.trade.user_from, self.trade.user_to]:
|
2021-06-02 11:46:28 +00:00
|
|
|
txt = "You do not participate in this trading"
|
|
|
|
raise ValidationError(txt)
|
2021-05-10 09:47:56 +00:00
|
|
|
|
2021-06-07 15:12:30 +00:00
|
|
|
confirm_from = Confirm(user=self.trade.user_from,
|
|
|
|
action=self.trade,
|
2021-05-10 09:47:56 +00:00
|
|
|
devices=self.trade.devices)
|
2021-06-07 15:12:30 +00:00
|
|
|
confirm_to = Confirm(user=self.trade.user_to,
|
|
|
|
action=self.trade,
|
2021-05-10 09:47:56 +00:00
|
|
|
devices=self.trade.devices)
|
|
|
|
db.session.add(confirm_from)
|
|
|
|
db.session.add(confirm_to)
|
|
|
|
|
|
|
|
def create_phantom_account(self) -> None:
|
|
|
|
"""
|
|
|
|
If exist both users not to do nothing
|
|
|
|
If exist from but not to:
|
|
|
|
search if exist in the DB
|
|
|
|
if exist use it
|
|
|
|
else create new one
|
|
|
|
The same if exist to but not from
|
|
|
|
|
|
|
|
"""
|
2021-06-07 15:12:30 +00:00
|
|
|
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:
|
2021-05-10 09:47:56 +00:00
|
|
|
return
|
|
|
|
|
2021-06-07 15:12:30 +00:00
|
|
|
if self.data['confirm']:
|
2021-05-10 09:47:56 +00:00
|
|
|
return
|
|
|
|
|
2021-06-07 15:12:30 +00:00
|
|
|
if user_from and not user_to:
|
|
|
|
assert g.user == user_from
|
|
|
|
email = "{}_{}@dhub.com".format(str(user_from.id), code)
|
2021-05-10 09:47:56 +00:00
|
|
|
users = User.query.filter_by(email=email)
|
|
|
|
if users.first():
|
|
|
|
user = users.first()
|
2021-06-07 15:12:30 +00:00
|
|
|
self.data['user_to'] = user
|
2021-05-10 09:47:56 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
user = User(email=email, password='', active=False, phantom=True)
|
|
|
|
db.session.add(user)
|
2021-06-07 15:12:30 +00:00
|
|
|
self.data['user_to'] = user
|
2021-05-10 09:47:56 +00:00
|
|
|
|
2021-06-07 15:12:30 +00:00
|
|
|
if not user_from and user_to:
|
|
|
|
email = "{}_{}@dhub.com".format(str(user_to.id), code)
|
2021-05-10 09:47:56 +00:00
|
|
|
users = User.query.filter_by(email=email)
|
|
|
|
if users.first():
|
|
|
|
user = users.first()
|
2021-06-07 15:12:30 +00:00
|
|
|
self.data['user_from'] = user
|
2021-05-10 09:47:56 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
user = User(email=email, password='', active=False, phantom=True)
|
|
|
|
db.session.add(user)
|
2021-06-07 15:12:30 +00:00
|
|
|
self.data['user_from'] = user
|
2021-05-10 09:47:56 +00:00
|
|
|
|
|
|
|
def create_automatic_trade(self) -> None:
|
|
|
|
# not do nothing if it's neccesary confirmation explicity
|
|
|
|
if self.trade.confirm:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Change the owner for every devices
|
|
|
|
for dev in self.trade.devices:
|
2021-06-07 15:12:30 +00:00
|
|
|
dev.change_owner(self.trade.user_to)
|
2021-05-10 09:47:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfirmMixin():
|
|
|
|
"""
|
|
|
|
Very Important:
|
|
|
|
==============
|
|
|
|
All of this Views than inherit of this class is executed for users
|
|
|
|
than is not owner of the Trade action.
|
|
|
|
|
|
|
|
The owner of Trade action executed this actions of confirm and revoke from the
|
|
|
|
lot
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Model = None
|
|
|
|
|
|
|
|
def __init__(self, data, resource_def, schema):
|
|
|
|
self.schema = schema
|
|
|
|
a = resource_def.schema.load(data)
|
|
|
|
self.validate(a)
|
2021-06-24 17:09:12 +00:00
|
|
|
if not a['devices']:
|
2021-05-10 09:47:56 +00:00
|
|
|
raise ValidationError('Devices not exist.')
|
|
|
|
self.model = self.Model(**a)
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
db.session().final_flush()
|
|
|
|
ret = self.schema.jsonify(self.model)
|
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
class ConfirmView(ConfirmMixin):
|
|
|
|
"""Handler for manager the Confirmation register from post
|
|
|
|
|
|
|
|
request_confirm = {
|
|
|
|
'type': 'Confirm',
|
|
|
|
'action': trade.id,
|
|
|
|
'devices': [device_id]
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
Model = Confirm
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
"""If there are one device than have one confirmation,
|
|
|
|
then remove the list this device of the list of devices of this action
|
|
|
|
"""
|
|
|
|
real_devices = []
|
|
|
|
for dev in data['devices']:
|
2021-06-07 08:50:08 +00:00
|
|
|
ac = dev.last_action_trading
|
|
|
|
if ac.type == Confirm.t and not ac.user == g.user:
|
|
|
|
real_devices.append(dev)
|
2021-05-10 09:47:56 +00:00
|
|
|
|
|
|
|
data['devices'] = OrderedSet(real_devices)
|
|
|
|
|
|
|
|
# Change the owner for every devices
|
|
|
|
for dev in data['devices']:
|
2021-06-07 08:50:08 +00:00
|
|
|
user_to = data['action'].user_to
|
|
|
|
dev.change_owner(user_to)
|
2021-05-10 09:47:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RevokeView(ConfirmMixin):
|
|
|
|
"""Handler for manager the Revoke register from post
|
|
|
|
|
|
|
|
request_revoke = {
|
|
|
|
'type': 'Revoke',
|
|
|
|
'action': trade.id,
|
|
|
|
'devices': [device_id],
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Model = Revoke
|
|
|
|
|
2021-06-04 17:27:01 +00:00
|
|
|
def __init__(self, data, resource_def, schema):
|
|
|
|
self.schema = schema
|
|
|
|
a = resource_def.schema.load(data)
|
|
|
|
self.validate(a)
|
|
|
|
|
2021-05-10 09:47:56 +00:00
|
|
|
def validate(self, data):
|
2021-06-07 08:50:08 +00:00
|
|
|
"""All devices need to have the status of DoubleConfirmation."""
|
|
|
|
|
2021-06-04 17:27:01 +00:00
|
|
|
### check ###
|
|
|
|
if not data['devices']:
|
|
|
|
raise ValidationError('Devices not exist.')
|
2021-05-10 09:47:56 +00:00
|
|
|
|
2021-10-28 10:19:49 +00:00
|
|
|
lot = data['action'].lot
|
2021-05-10 09:47:56 +00:00
|
|
|
for dev in data['devices']:
|
2021-10-28 10:19:49 +00:00
|
|
|
if not dev.trading(lot) == 'TradeConfirmed':
|
2021-06-07 08:50:08 +00:00
|
|
|
txt = 'Some of devices do not have enough to confirm for to do a revoke'
|
|
|
|
ValidationError(txt)
|
2021-06-04 17:27:01 +00:00
|
|
|
### End check ###
|
2021-05-10 09:47:56 +00:00
|
|
|
|
2021-06-07 08:50:08 +00:00
|
|
|
ids = {d.id for d in data['devices']}
|
2021-06-04 17:27:01 +00:00
|
|
|
lot = data['action'].lot
|
2021-06-07 08:50:08 +00:00
|
|
|
self.model = delete_from_trade(lot, ids)
|
2021-05-10 09:47:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfirmRevokeView(ConfirmMixin):
|
|
|
|
"""Handler for manager the Confirmation register from post
|
|
|
|
|
|
|
|
request_confirm_revoke = {
|
|
|
|
'type': 'ConfirmRevoke',
|
|
|
|
'action': action_revoke.id,
|
|
|
|
'devices': [device_id]
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Model = ConfirmRevoke
|
|
|
|
|
|
|
|
def validate(self, data):
|
2021-06-07 08:50:08 +00:00
|
|
|
"""All devices need to have the status of revoke."""
|
|
|
|
|
|
|
|
if not data['action'].type == 'Revoke':
|
|
|
|
txt = 'Error: this action is not a revoke action'
|
|
|
|
ValidationError(txt)
|
|
|
|
|
2021-05-10 09:47:56 +00:00
|
|
|
for dev in data['devices']:
|
2021-06-07 08:50:08 +00:00
|
|
|
if not dev.trading == 'Revoke':
|
|
|
|
txt = 'Some of devices do not have revoke to confirm'
|
|
|
|
ValidationError(txt)
|
2021-05-10 09:47:56 +00:00
|
|
|
|
2021-06-07 08:50:08 +00:00
|
|
|
devices = OrderedSet(data['devices'])
|
2021-06-03 17:05:57 +00:00
|
|
|
data['devices'] = devices
|
2021-05-10 09:47:56 +00:00
|
|
|
|
|
|
|
# Change the owner for every devices
|
2021-06-03 17:05:57 +00:00
|
|
|
# data['action'] == 'Revoke'
|
|
|
|
|
|
|
|
trade = data['action'].action
|
|
|
|
for dev in devices:
|
2021-06-07 08:50:08 +00:00
|
|
|
dev.reset_owner()
|
2021-05-10 09:47:56 +00:00
|
|
|
|
2021-06-03 17:05:57 +00:00
|
|
|
trade.lot.devices.difference_update(devices)
|
2021-06-24 17:09:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfirmDocumentMixin():
|
|
|
|
"""
|
|
|
|
Very Important:
|
|
|
|
==============
|
|
|
|
All of this Views than inherit of this class is executed for users
|
|
|
|
than is not owner of the Trade action.
|
|
|
|
|
|
|
|
The owner of Trade action executed this actions of confirm and revoke from the
|
|
|
|
lot
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Model = None
|
|
|
|
|
|
|
|
def __init__(self, data, resource_def, schema):
|
|
|
|
self.schema = schema
|
|
|
|
a = resource_def.schema.load(data)
|
|
|
|
self.validate(a)
|
|
|
|
if not a['documents']:
|
|
|
|
raise ValidationError('Documents not exist.')
|
|
|
|
self.model = self.Model(**a)
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
db.session().final_flush()
|
|
|
|
ret = self.schema.jsonify(self.model)
|
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
class ConfirmDocumentView(ConfirmDocumentMixin):
|
|
|
|
"""Handler for manager the Confirmation register from post
|
|
|
|
|
|
|
|
request_confirm = {
|
|
|
|
'type': 'Confirm',
|
|
|
|
'action': trade.id,
|
2021-06-28 14:06:07 +00:00
|
|
|
'documents': [document_id],
|
2021-06-24 17:09:12 +00:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2021-06-28 14:06:07 +00:00
|
|
|
Model = ConfirmDocument
|
2021-06-24 17:09:12 +00:00
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
"""If there are one device than have one confirmation,
|
|
|
|
then remove the list this device of the list of devices of this action
|
|
|
|
"""
|
2021-06-28 14:06:07 +00:00
|
|
|
for doc in data['documents']:
|
|
|
|
ac = doc.trading
|
|
|
|
if not doc.trading in ['Confirm', 'Need Confirmation']:
|
|
|
|
txt = 'Some of documents do not have enough to confirm for to do a Doble Confirmation'
|
|
|
|
ValidationError(txt)
|
|
|
|
### End check ###
|
2021-06-24 17:09:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RevokeDocumentView(ConfirmDocumentMixin):
|
|
|
|
"""Handler for manager the Revoke register from post
|
|
|
|
|
|
|
|
request_revoke = {
|
|
|
|
'type': 'Revoke',
|
|
|
|
'action': trade.id,
|
2021-06-28 14:06:07 +00:00
|
|
|
'documents': [document_id],
|
2021-06-24 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Model = RevokeDocument
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
"""All devices need to have the status of DoubleConfirmation."""
|
|
|
|
|
|
|
|
### check ###
|
|
|
|
if not data['documents']:
|
|
|
|
raise ValidationError('Documents not exist.')
|
|
|
|
|
|
|
|
for doc in data['documents']:
|
2021-06-28 14:06:07 +00:00
|
|
|
if not doc.trading in ['Document Confirmed', 'Confirm']:
|
2021-06-24 17:09:12 +00:00
|
|
|
txt = 'Some of documents do not have enough to confirm for to do a revoke'
|
|
|
|
ValidationError(txt)
|
|
|
|
### End check ###
|
|
|
|
|
|
|
|
|
|
|
|
class ConfirmRevokeDocumentView(ConfirmDocumentMixin):
|
|
|
|
"""Handler for manager the Confirmation register from post
|
|
|
|
|
|
|
|
request_confirm_revoke = {
|
|
|
|
'type': 'ConfirmRevoke',
|
|
|
|
'action': action_revoke.id,
|
2021-06-28 14:06:07 +00:00
|
|
|
'documents': [document_id],
|
2021-06-24 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-06-28 14:06:07 +00:00
|
|
|
Model = ConfirmRevokeDocument
|
2021-06-24 17:09:12 +00:00
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
"""All devices need to have the status of revoke."""
|
|
|
|
|
2021-06-28 14:06:07 +00:00
|
|
|
if not data['action'].type == 'RevokeDocument':
|
2021-06-24 17:09:12 +00:00
|
|
|
txt = 'Error: this action is not a revoke action'
|
|
|
|
ValidationError(txt)
|
|
|
|
|
2021-06-28 14:06:07 +00:00
|
|
|
for doc in data['documents']:
|
|
|
|
if not doc.trading == 'Revoke':
|
|
|
|
txt = 'Some of documents do not have revoke to confirm'
|
2021-06-24 17:09:12 +00:00
|
|
|
ValidationError(txt)
|