Fixing schema trade
This commit is contained in:
parent
e1348e3809
commit
6c689878a4
|
@ -502,10 +502,20 @@ class Trade(ActionWithMultipleDevices):
|
||||||
document_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='documentID', required=False)
|
document_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='documentID', required=False)
|
||||||
date = DateTime(data_key='date', required=False)
|
date = DateTime(data_key='date', required=False)
|
||||||
price = Float(required=False, data_key='price')
|
price = Float(required=False, data_key='price')
|
||||||
user_to_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', missing='',
|
user_to_email = SanitizedStr(
|
||||||
required=False)
|
validate=Length(max=STR_SIZE),
|
||||||
user_from_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userFrom', missing='',
|
data_key='userToEmail',
|
||||||
required=False)
|
missing='',
|
||||||
|
required=False
|
||||||
|
)
|
||||||
|
user_to = NestedOn(s_user.User, dump_only=True, data_key='userTo')
|
||||||
|
user_from_email = SanitizedStr(
|
||||||
|
validate=Length(max=STR_SIZE),
|
||||||
|
data_key='userFromEmail',
|
||||||
|
missing='',
|
||||||
|
required=False
|
||||||
|
)
|
||||||
|
user_from = NestedOn(s_user.User, dump_only=True, data_key='userFrom')
|
||||||
code = SanitizedStr(validate=Length(max=STR_SIZE), data_key='code', required=False)
|
code = SanitizedStr(validate=Length(max=STR_SIZE), data_key='code', required=False)
|
||||||
confirm = Boolean(data_key='confirms', missing=False, description="""If you need confirmation of the user
|
confirm = Boolean(data_key='confirms', missing=False, description="""If you need confirmation of the user
|
||||||
you need actevate this field""")
|
you need actevate this field""")
|
||||||
|
@ -516,7 +526,7 @@ class Trade(ActionWithMultipleDevices):
|
||||||
|
|
||||||
@validates_schema
|
@validates_schema
|
||||||
def validate_lot(self, data: dict):
|
def validate_lot(self, data: dict):
|
||||||
if not g.user.email in [data['user_from_id'], data['user_to_id']]:
|
if not g.user.email in [data['user_from_email'], data['user_to_email']]:
|
||||||
txt = "you need to be one of the users of involved in the Trade"
|
txt = "you need to be one of the users of involved in the Trade"
|
||||||
raise ValidationError(txt)
|
raise ValidationError(txt)
|
||||||
|
|
||||||
|
@ -532,7 +542,7 @@ class Trade(ActionWithMultipleDevices):
|
||||||
data['devices'] = data['lot'].devices
|
data['devices'] = data['lot'].devices
|
||||||
|
|
||||||
@validates_schema
|
@validates_schema
|
||||||
def validate_user_to_id(self, data: dict):
|
def validate_user_to_email(self, data: dict):
|
||||||
"""
|
"""
|
||||||
- if user_to exist
|
- if user_to exist
|
||||||
* confirmation
|
* confirmation
|
||||||
|
@ -541,15 +551,14 @@ class Trade(ActionWithMultipleDevices):
|
||||||
* without confirmation
|
* without confirmation
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if data['user_to_id']:
|
if data['user_to_email']:
|
||||||
user_to = User.query.filter_by(email=data['user_to_id']).one()
|
user_to = User.query.filter_by(email=data['user_to_email']).one()
|
||||||
data['user_to_id'] = user_to.id
|
|
||||||
data['user_to'] = user_to
|
data['user_to'] = user_to
|
||||||
else:
|
else:
|
||||||
data['confirm'] = False
|
data['confirm'] = False
|
||||||
|
|
||||||
@validates_schema
|
@validates_schema
|
||||||
def validate_user_from_id(self, data: dict):
|
def validate_user_from_email(self, data: dict):
|
||||||
"""
|
"""
|
||||||
- if user_from exist
|
- if user_from exist
|
||||||
* confirmation
|
* confirmation
|
||||||
|
@ -558,13 +567,12 @@ class Trade(ActionWithMultipleDevices):
|
||||||
* without confirmation
|
* without confirmation
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not (data['user_from_id'] or data['user_to_id']):
|
if not (data['user_from_email'] or data['user_to_email']):
|
||||||
txt = "you need one user from or user to for to do a offer"
|
txt = "you need one user from or user to for to do a offer"
|
||||||
raise ValidationError(txt)
|
raise ValidationError(txt)
|
||||||
|
|
||||||
if data['user_from_id']:
|
if data['user_from_email']:
|
||||||
user_from = User.query.filter_by(email=data['user_from_id']).one()
|
user_from = User.query.filter_by(email=data['user_from_email']).one()
|
||||||
data['user_from_id'] = user_from.id
|
|
||||||
data['user_from'] = user_from
|
data['user_from'] = user_from
|
||||||
else:
|
else:
|
||||||
data['confirm'] = False
|
data['confirm'] = False
|
||||||
|
@ -572,7 +580,7 @@ class Trade(ActionWithMultipleDevices):
|
||||||
@validates_schema
|
@validates_schema
|
||||||
def validate_code(self, data: dict):
|
def validate_code(self, data: dict):
|
||||||
"""If the user not exist, you need a code to be able to do the traceability"""
|
"""If the user not exist, you need a code to be able to do the traceability"""
|
||||||
if data['user_from_id'] and data['user_to_id']:
|
if data['user_from_email'] and data['user_to_email']:
|
||||||
return
|
return
|
||||||
|
|
||||||
if not data.get('code'):
|
if not data.get('code'):
|
||||||
|
|
|
@ -29,6 +29,8 @@ 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)
|
a = resource_def.schema.load(data)
|
||||||
|
a.pop('user_to_email', '')
|
||||||
|
a.pop('user_from_email', '')
|
||||||
self.trade = Trade(**a)
|
self.trade = Trade(**a)
|
||||||
self.create_phantom_account()
|
self.create_phantom_account()
|
||||||
db.session.add(self.trade)
|
db.session.add(self.trade)
|
||||||
|
@ -36,7 +38,6 @@ class TradeView():
|
||||||
self.create_confirmations()
|
self.create_confirmations()
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
# import pdb; pdb.set_trace()
|
|
||||||
db.session().final_flush()
|
db.session().final_flush()
|
||||||
ret = self.schema.jsonify(self.trade)
|
ret = self.schema.jsonify(self.trade)
|
||||||
ret.status_code = 201
|
ret.status_code = 201
|
||||||
|
@ -57,7 +58,7 @@ class TradeView():
|
||||||
|
|
||||||
# check than the user than want to do the action is one of the users
|
# check than the user than want to do the action is one of the users
|
||||||
# involved in the action
|
# involved in the action
|
||||||
assert g.user.id in [self.trade.user_from_id, self.trade.user_to_id]
|
assert g.user in [self.trade.user_from, self.trade.user_to]
|
||||||
|
|
||||||
confirm_from = Confirm(user=self.trade.user_from,
|
confirm_from = Confirm(user=self.trade.user_from,
|
||||||
action=self.trade,
|
action=self.trade,
|
||||||
|
@ -78,12 +79,12 @@ class TradeView():
|
||||||
The same if exist to but not from
|
The same if exist to but not from
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if self.trade.user_from_id and self.trade.user_to_id:
|
if self.trade.user_from and self.trade.user_to:
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.trade.user_from_id and not self.trade.user_to_id:
|
if self.trade.user_from and not self.trade.user_to:
|
||||||
assert g.user.id == self.trade.user_from_id
|
assert g.user == self.trade.user_from
|
||||||
email = "{}_{}@dhub.com".format(str(self.trade.user_from_id), self.trade.code)
|
email = "{}_{}@dhub.com".format(str(self.trade.user_from.id), self.trade.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()
|
||||||
|
@ -94,8 +95,8 @@ class TradeView():
|
||||||
db.session.add(user)
|
db.session.add(user)
|
||||||
self.trade.user_to = user
|
self.trade.user_to = user
|
||||||
|
|
||||||
if not self.trade.user_from_id and self.trade.user_to_id:
|
if not self.trade.user_from and self.trade.user_to:
|
||||||
email = "{}_{}@dhub.com".format(str(self.trade.user_to_id), self.trade.code)
|
email = "{}_{}@dhub.com".format(str(self.trade.user_to.id), self.trade.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()
|
||||||
|
|
|
@ -4,6 +4,7 @@ from teal.marshmallow import SanitizedStr, URL, EnumField
|
||||||
from ereuse_devicehub.marshmallow import NestedOn
|
from ereuse_devicehub.marshmallow import NestedOn
|
||||||
from ereuse_devicehub.resources.deliverynote import schemas as s_deliverynote
|
from ereuse_devicehub.resources.deliverynote import schemas as s_deliverynote
|
||||||
from ereuse_devicehub.resources.device import schemas as s_device
|
from ereuse_devicehub.resources.device import schemas as s_device
|
||||||
|
from ereuse_devicehub.resources.action import schemas as s_action
|
||||||
from ereuse_devicehub.resources.enums import TransferState
|
from ereuse_devicehub.resources.enums import TransferState
|
||||||
from ereuse_devicehub.resources.lot import models as m
|
from ereuse_devicehub.resources.lot import models as m
|
||||||
from ereuse_devicehub.resources.models import STR_SIZE
|
from ereuse_devicehub.resources.models import STR_SIZE
|
||||||
|
@ -26,4 +27,5 @@ class Lot(Thing):
|
||||||
transfer_state = EnumField(TransferState, description=m.Lot.transfer_state.comment)
|
transfer_state = EnumField(TransferState, description=m.Lot.transfer_state.comment)
|
||||||
receiver_address = SanitizedStr(validate=f.validate.Length(max=42))
|
receiver_address = SanitizedStr(validate=f.validate.Length(max=42))
|
||||||
deliverynote = NestedOn(s_deliverynote.Deliverynote, dump_only=True)
|
deliverynote = NestedOn(s_deliverynote.Deliverynote, dump_only=True)
|
||||||
|
trade = NestedOn(s_action.Trade, dump_only=True)
|
||||||
is_temporary = f.Boolean(missing=True, data_key='isTemporary')
|
is_temporary = f.Boolean(missing=True, data_key='isTemporary')
|
||||||
|
|
|
@ -786,7 +786,7 @@ def test_offer_without_to(user: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [device.id],
|
'devices': [device.id],
|
||||||
'userFrom': user.email,
|
'userFromEmail': user.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -814,7 +814,7 @@ def test_offer_without_to(user: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [device.id],
|
'devices': [device.id],
|
||||||
'userFrom': user.email,
|
'userFromEmail': user.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -837,7 +837,7 @@ def test_offer_without_to(user: UserClient):
|
||||||
request_post2 = {
|
request_post2 = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [device2.id],
|
'devices': [device2.id],
|
||||||
'userFrom': user.email,
|
'userFromEmail': user.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -868,7 +868,7 @@ def test_offer_without_from(user: UserClient, user2: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [device.id],
|
'devices': [device.id],
|
||||||
'userTo': user2.email,
|
'userToEmail': user2.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -878,7 +878,7 @@ def test_offer_without_from(user: UserClient, user2: UserClient):
|
||||||
}
|
}
|
||||||
action, _ = user2.post(res=models.Action, data=request_post, status=422)
|
action, _ = user2.post(res=models.Action, data=request_post, status=422)
|
||||||
|
|
||||||
request_post['userTo'] = user.email
|
request_post['userToEmail'] = user.email
|
||||||
action, _ = user.post(res=models.Action, data=request_post)
|
action, _ = user.post(res=models.Action, data=request_post)
|
||||||
trade = models.Trade.query.one()
|
trade = models.Trade.query.one()
|
||||||
|
|
||||||
|
@ -946,8 +946,8 @@ def test_offer(user: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [],
|
'devices': [],
|
||||||
'userFrom': user.email,
|
'userFromEmail': user.email,
|
||||||
'userTo': user2.email,
|
'userToEmail': user2.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -973,8 +973,8 @@ def test_offer_without_devices(user: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [],
|
'devices': [],
|
||||||
'userFrom': user.email,
|
'userFromEmail': user.email,
|
||||||
'userTo': user2.email,
|
'userToEmail': user2.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -1052,8 +1052,8 @@ def test_endpoint_confirm(user: UserClient, user2: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [device_id],
|
'devices': [device_id],
|
||||||
'userFrom': user.email,
|
'userFromEmail': user.email,
|
||||||
'userTo': user2.email,
|
'userToEmail': user2.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -1093,8 +1093,8 @@ def test_confirm_revoke(user: UserClient, user2: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [device_id],
|
'devices': [device_id],
|
||||||
'userFrom': user.email,
|
'userFromEmail': user.email,
|
||||||
'userTo': user2.email,
|
'userToEmail': user2.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -1172,8 +1172,8 @@ def test_usecase_confirmation(user: UserClient, user2: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [],
|
'devices': [],
|
||||||
'userFrom': user2.email,
|
'userFromEmail': user2.email,
|
||||||
'userTo': user.email,
|
'userToEmail': user.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
@ -1183,6 +1183,8 @@ def test_usecase_confirmation(user: UserClient, user2: UserClient):
|
||||||
|
|
||||||
user.post(res=models.Action, data=request_post)
|
user.post(res=models.Action, data=request_post)
|
||||||
trade = models.Trade.query.one()
|
trade = models.Trade.query.one()
|
||||||
|
# l_after, _ = user.get(res=Lot, item=lot['id'])
|
||||||
|
# import pdb; pdb.set_trace()
|
||||||
|
|
||||||
# the SCRAP confirms 3 of the 10 devices in its outgoing lot
|
# the SCRAP confirms 3 of the 10 devices in its outgoing lot
|
||||||
request_confirm = {
|
request_confirm = {
|
||||||
|
@ -1360,8 +1362,8 @@ def test_confirmRevoke(user: UserClient, user2: UserClient):
|
||||||
request_post = {
|
request_post = {
|
||||||
'type': 'Trade',
|
'type': 'Trade',
|
||||||
'devices': [],
|
'devices': [],
|
||||||
'userFrom': user2.email,
|
'userFromEmail': user2.email,
|
||||||
'userTo': user.email,
|
'userToEmail': user.email,
|
||||||
'price': 10,
|
'price': 10,
|
||||||
'date': "2020-12-01T02:00:00+00:00",
|
'date': "2020-12-01T02:00:00+00:00",
|
||||||
'documentID': '1',
|
'documentID': '1',
|
||||||
|
|
Reference in New Issue