adding view of receives

This commit is contained in:
Cayo Puigdefabregas 2020-11-19 21:04:14 +01:00
parent a1eb0f4c90
commit 67d7e27cf9
2 changed files with 26 additions and 6 deletions

View File

@ -11,6 +11,7 @@ from teal.resource import Schema
from ereuse_devicehub.marshmallow import NestedOn from ereuse_devicehub.marshmallow import NestedOn
from ereuse_devicehub.resources import enums from ereuse_devicehub.resources import enums
from ereuse_devicehub.resources.action import models as m 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.agent import schemas as s_agent
from ereuse_devicehub.resources.device import schemas as s_device from ereuse_devicehub.resources.device import schemas as s_device
from ereuse_devicehub.resources.enums import AppearanceRange, BiosAccessRange, FunctionalityRange, \ from ereuse_devicehub.resources.enums import AppearanceRange, BiosAccessRange, FunctionalityRange, \
@ -433,7 +434,9 @@ class TransferOwnershipBlockchain(Trade):
class Receive(ActionWithMultipleDevices): class Receive(ActionWithMultipleDevices):
__doc__ = m.Receive.__doc__ __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): class Migrate(ActionWithMultipleDevices):

View File

@ -14,8 +14,9 @@ from teal.marshmallow import ValidationError
from teal.resource import View from teal.resource import View
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action.models import Action, RateComputer, Snapshot, VisualTest, \ from ereuse_devicehub.query import things_response
InitTransfer 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.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
@ -173,7 +174,23 @@ class ActionView(View):
class ReceiveView(View): class ReceiveView(View):
def post(self): 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): def find(self, args: dict):
return jsonify('Ok find') 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
)