From 67d7e27cf9d191cda04d7a49df19ff7a76704a85 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 19 Nov 2020 21:04:14 +0100 Subject: [PATCH] adding view of receives --- ereuse_devicehub/resources/action/schemas.py | 5 +++- ereuse_devicehub/resources/action/views.py | 27 ++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 0eb3f1c4..397cf8b2 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -11,6 +11,7 @@ from teal.resource import Schema from ereuse_devicehub.marshmallow import NestedOn from ereuse_devicehub.resources import enums from ereuse_devicehub.resources.action import models as m +from ereuse_devicehub.resources.action import schemas as s_action from ereuse_devicehub.resources.agent import schemas as s_agent from ereuse_devicehub.resources.device import schemas as s_device from ereuse_devicehub.resources.enums import AppearanceRange, BiosAccessRange, FunctionalityRange, \ @@ -433,7 +434,9 @@ class TransferOwnershipBlockchain(Trade): class Receive(ActionWithMultipleDevices): __doc__ = m.Receive.__doc__ - role = EnumField(ReceiverRole) + agent_from = NestedOn(s_agent.Agent, only_query='id', required=False, comment=m.Receive.agent_from_id.comment) + agent_to = NestedOn(s_agent.Agent, only_query='id', required=False, comment=m.Receive.agent_to_id.comment) + action = NestedOn(s_action.Action, only_query='id', required=False, comment=m.Receive.action_id.comment) class Migrate(ActionWithMultipleDevices): diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index 8bc0f28b..84c92b0f 100644 --- a/ereuse_devicehub/resources/action/views.py +++ b/ereuse_devicehub/resources/action/views.py @@ -14,8 +14,9 @@ from teal.marshmallow import ValidationError from teal.resource import View from ereuse_devicehub.db import db -from ereuse_devicehub.resources.action.models import Action, RateComputer, Snapshot, VisualTest, \ - InitTransfer +from ereuse_devicehub.query import things_response +from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest, + InitTransfer, Receive) 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 @@ -173,7 +174,23 @@ class ActionView(View): class ReceiveView(View): def post(self): - return jsonify('Ok') + # return jsonify('Ok') + """ Create one receive """ + res_json = request.get_json() + receive = Receive(**res_json) + db.session.add(receive) + db.session().final_flush() + ret = self.schema.jsonify(receive) + ret.status_code = 201 + db.session.commit() + return ret - def find(self, args): - return jsonify('Ok find') + def find(self, args: dict): + receives = Receive.query.filter_by(author=g.user) \ + .order_by(Receive.created.desc()) \ + .paginate(per_page=200) + return things_response( + self.schema.dump(receives.items, many=True, nested=0), + receives.page, receives.per_page, receives.total, + receives.prev_num, receives.next_num + )