diff --git a/ereuse_devicehub/resources/action/__init__.py b/ereuse_devicehub/resources/action/__init__.py index 7fd91eb8..1e722a3c 100644 --- a/ereuse_devicehub/resources/action/__init__.py +++ b/ereuse_devicehub/resources/action/__init__.py @@ -4,7 +4,7 @@ from teal.resource import Converters, Resource from ereuse_devicehub.resources.action import schemas from ereuse_devicehub.resources.action.views import (ActionView, AllocateView, DeallocateView, - LiveView) + LiveView, TradeView) from ereuse_devicehub.resources.device.sync import Sync @@ -255,6 +255,11 @@ class CancelTradeDef(ActionDef): SCHEMA = schemas.CancelTrade +class TradeDef(ActionDef): + VIEW = TradeView + SCHEMA = schemas.Trade + + class ToDisposeProductDef(ActionDef): VIEW = None SCHEMA = schemas.ToDisposeProduct diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index ee92d840..09befc5d 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -457,11 +457,18 @@ class CancelReservation(Organize): class Trade(ActionWithMultipleDevices): __doc__ = m.Trade.__doc__ - shipping_date = DateTime(data_key='shippingDate') - invoice_number = SanitizedStr(validate=Length(max=STR_SIZE), data_key='invoiceNumber') - price = NestedOn(Price) - to = NestedOn(s_agent.Agent, only_query='id', required=True, comment=m.Trade.user_to_comment) - confirms = NestedOn(Organize) + date = DateTime(data_key='date', required=False) + price = Float(required=False, data_key='price') + user_to = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False) + user_from = SanitizedStr(validate=Length(max=STR_SIZE), data_key='userTo', required=False) + + +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): diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index 01295028..3b28ceec 100644 --- a/ereuse_devicehub/resources/action/views.py +++ b/ereuse_devicehub/resources/action/views.py @@ -16,11 +16,13 @@ from teal.db import ResourceNotFound from ereuse_devicehub.db import db from ereuse_devicehub.query import things_response 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.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity from ereuse_devicehub.resources.user.exceptions import InsufficientPermission +from ereuse_devicehub.resources.user.models import User SUPPORTED_WORKBENCH = StrictVersion('11.0') @@ -68,6 +70,31 @@ def move_json(tmp_snapshots, path_name, user, live=False): 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(): model = None