From 69368a8ca36fccc319b5766024398a7e076bac3c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 17 Nov 2020 19:21:32 +0100 Subject: [PATCH] rent with assigned concept --- ereuse_devicehub/resources/action/schemas.py | 14 ++++++++---- ereuse_devicehub/resources/rent/views.py | 24 +++++++++++++++----- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 48ae5970..ebd4cd97 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -460,8 +460,12 @@ class Transferred(ActionWithMultipleDevices): class Assigned(ActionWithMultipleDevices): __doc__ = m.Assigned.__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.to_comment) - confirms = NestedOn(Organize) + agent = NestedOn(s_agent.Agent, only_query='id', required=False, comment=m.Trade.to_comment) + description = SanitizedStr(default='', description=m.Action.description.comment) + start_time = DateTime(data_key='startTime', description=m.Action.start_time.comment) + end_time = DateTime(data_key='endTime', description=m.Action.end_time.comment) + assigned = SanitizedStr(validate=Length(min=1, max=STR_BIG_SIZE), + required=False, + description='The code of the agent to assigned.') + n_beneficiaries = Integer(validate=[Range(min=1, error="Value must be greater than 0")], + required=True) diff --git a/ereuse_devicehub/resources/rent/views.py b/ereuse_devicehub/resources/rent/views.py index 0b5659d6..e8ed15ad 100644 --- a/ereuse_devicehub/resources/rent/views.py +++ b/ereuse_devicehub/resources/rent/views.py @@ -1,25 +1,34 @@ +import uuid # from typing import Callable, Iterable, Tuple -# from flask import g +from flask import g, request # from flask.json import jsonify from teal.resource import View from ereuse_devicehub import auth +from ereuse_devicehub.db import db from ereuse_devicehub.query import things_response from ereuse_devicehub.resources.action.models import Assigned + class RentingView(View): @auth.Auth.requires_auth - def get(self, id): + def get(self, id: uuid.UUID) -> Assigned: return super().get(id) @auth.Auth.requires_auth def post(self): """ Create one rent """ - return super().get(id) - # return jsonify('ok') + res_json = request.get_json() + assigned = Assigned(**res_json) + db.session.add(assigned) + db.session().final_flush() + ret = self.schema.jsonify(assigned) + ret.status_code = 201 + db.session.commit() + return ret def find(self, args: dict): - rents = Assigned.query.filter() \ + rents = Assigned.query.filter_by(author=g.user) \ .order_by(Assigned.created.desc()) \ .paginate(per_page=200) return things_response( @@ -27,4 +36,7 @@ class RentingView(View): rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num ) - + def one(self, id: uuid.UUID): + """Gets one action.""" + assigned = Assigned.query.filter_by(id=id, author=g.user).one() + return self.schema.jsonify(assigned, nested=2)