bugfix trade action

This commit is contained in:
Cayo Puigdefabregas 2021-03-18 16:36:19 +01:00
parent acbc252fe8
commit a66ddc8390
5 changed files with 23 additions and 16 deletions

View File

@ -115,10 +115,11 @@ class Dummy:
user1.post({'type': model.t, 'devices': [pc]}, res=m.Action) user1.post({'type': model.t, 'devices': [pc]}, res=m.Action)
# Perform a Sell to several devices # Perform a Sell to several devices
# import pdb; pdb.set_trace()
user1.post( user1.post(
{ {
'type': m.Sell.t, 'type': m.Sell.t,
'to': user1.user['individuals'][0]['id'], 'userTo': user1.user['email'],
'devices': list(itertools.islice(pcs, len(pcs) // 2)) 'devices': list(itertools.islice(pcs, len(pcs) // 2))
}, },
res=m.Action) res=m.Action)
@ -174,7 +175,7 @@ class Dummy:
user1.post( # Sell device user1.post( # Sell device
{ {
'type': m.Sell.t, 'type': m.Sell.t,
'to': user1.user['individuals'][0]['id'], 'userTo': user1.user['email'],
'devices': [sample_pc] 'devices': [sample_pc]
}, },
res=m.Action) res=m.Action)

View File

@ -21,6 +21,7 @@ from ereuse_devicehub.resources.enums import AppearanceRange, BiosAccessRange, F
from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE
from ereuse_devicehub.resources.schemas import Thing from ereuse_devicehub.resources.schemas import Thing
from ereuse_devicehub.resources.user import schemas as s_user from ereuse_devicehub.resources.user import schemas as s_user
from ereuse_devicehub.resources.user.models import User
class Action(Thing): class Action(Thing):
@ -459,8 +460,22 @@ class Trade(ActionWithMultipleDevices):
__doc__ = m.Trade.__doc__ __doc__ = m.Trade.__doc__
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 = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False) user_to_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False)
user_from = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False) user_from_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False)
@validates_schema
def validate_user_to_id(self, data: dict):
if 'user_to_id' in data:
user_to = User.query.filter_by(email=data['user_to_id']).one()
data['user_to_id'] = user_to.id
for dev in data['devices']:
dev.owner_id = user_to.id
@validates_schema
def validate_user_from_id(self, data: dict):
if 'user_from_id' in data:
user_to = User.query.filter_by(email=data['user_from_id']).one()
data['user_from_id'] = user_to.id
class OfferTrade(ActionWithMultipleDevices): class OfferTrade(ActionWithMultipleDevices):

View File

@ -75,18 +75,7 @@ class TradeView(View):
def post(self): def post(self):
res_json = request.get_json() res_json = request.get_json()
devices = res_json['devices']
if 'user_to' in res_json:
user_to_id = User.query.filter_by(email=res_json['user_to']).one().id
res_json.pop('user_to')
res_json['user_to_id'] = user_to_id
for dev in devices:
dev.owner_id = user_to_id
if 'user_from' in res_json:
res_json['user_from_id'] = User.query.filter_by(email=res_json['user_from']).one().id
res_json.pop('user_from')
res_obj = self.model(**res_json) res_obj = self.model(**res_json)
db.session.add(res_obj) db.session.add(res_obj)
db.session().final_flush() db.session().final_flush()
ret = self.schema.jsonify(res_obj) ret = self.schema.jsonify(res_obj)

View File

@ -740,6 +740,7 @@ def test_deallocate_bad_dates(user: UserClient):
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.xfail(reason='Old functionality')
@pytest.mark.parametrize('action_model_state', @pytest.mark.parametrize('action_model_state',
(pytest.param(ams, id=ams[0].__name__) (pytest.param(ams, id=ams[0].__name__)
for ams in [ for ams in [

View File

@ -54,6 +54,7 @@ def test_api_docs(client: Client):
'/metrics/', '/metrics/',
'/tags/', '/tags/',
'/tags/{tag_id}/device/{device_id}', '/tags/{tag_id}/device/{device_id}',
'/trades/',
'/users/', '/users/',
'/users/login/' '/users/login/'
# '/devices/{dev1_id}/merge/{dev2_id}', # '/devices/{dev1_id}/merge/{dev2_id}',
@ -119,4 +120,4 @@ def test_api_docs(client: Client):
'scheme': 'basic', 'scheme': 'basic',
'name': 'Authorization' 'name': 'Authorization'
} }
assert len(docs['definitions']) == 117 assert len(docs['definitions']) == 118