new version of trade

This commit is contained in:
Cayo Puigdefabregas 2021-03-18 10:59:38 +01:00
parent 9bd787c924
commit acbc252fe8
3 changed files with 46 additions and 7 deletions

View File

@ -4,7 +4,7 @@ from teal.resource import Converters, Resource
from ereuse_devicehub.resources.action import schemas from ereuse_devicehub.resources.action import schemas
from ereuse_devicehub.resources.action.views import (ActionView, AllocateView, DeallocateView, from ereuse_devicehub.resources.action.views import (ActionView, AllocateView, DeallocateView,
LiveView) LiveView, TradeView)
from ereuse_devicehub.resources.device.sync import Sync from ereuse_devicehub.resources.device.sync import Sync
@ -255,6 +255,11 @@ class CancelTradeDef(ActionDef):
SCHEMA = schemas.CancelTrade SCHEMA = schemas.CancelTrade
class TradeDef(ActionDef):
VIEW = TradeView
SCHEMA = schemas.Trade
class ToDisposeProductDef(ActionDef): class ToDisposeProductDef(ActionDef):
VIEW = None VIEW = None
SCHEMA = schemas.ToDisposeProduct SCHEMA = schemas.ToDisposeProduct

View File

@ -457,11 +457,18 @@ class CancelReservation(Organize):
class Trade(ActionWithMultipleDevices): class Trade(ActionWithMultipleDevices):
__doc__ = m.Trade.__doc__ __doc__ = m.Trade.__doc__
shipping_date = DateTime(data_key='shippingDate') date = DateTime(data_key='date', required=False)
invoice_number = SanitizedStr(validate=Length(max=STR_SIZE), data_key='invoiceNumber') price = Float(required=False, data_key='price')
price = NestedOn(Price) user_to = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False)
to = NestedOn(s_agent.Agent, only_query='id', required=True, comment=m.Trade.user_to_comment) user_from = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False)
confirms = NestedOn(Organize)
class OfferTrade(ActionWithMultipleDevices):
__doc__ = m.Trade.__doc__
date = DateTime(data_key='date', required=False)
document_id = SanitizedStr(validate=Length(max=STR_SIZE), data_key='documentID', required=False)
price = Float(required=False, data_key='price')
user_to = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=True)
class InitTransfer(Trade): class InitTransfer(Trade):

View File

@ -16,11 +16,13 @@ from teal.db import ResourceNotFound
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest, from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest,
InitTransfer, Live, Allocate, Deallocate) InitTransfer, Live, Allocate, Deallocate,
Trade)
from ereuse_devicehub.resources.device.models import Device, Computer, DataStorage from ereuse_devicehub.resources.device.models import Device, Computer, DataStorage
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
from ereuse_devicehub.resources.user.models import User
SUPPORTED_WORKBENCH = StrictVersion('11.0') SUPPORTED_WORKBENCH = StrictVersion('11.0')
@ -68,6 +70,31 @@ def move_json(tmp_snapshots, path_name, user, live=False):
os.remove(path_name) os.remove(path_name)
class TradeView(View):
model = Trade
def post(self):
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)
db.session.add(res_obj)
db.session().final_flush()
ret = self.schema.jsonify(res_obj)
ret.status_code = 201
db.session.commit()
return ret
class AllocateMix(): class AllocateMix():
model = None model = None