rent with assigned concept

This commit is contained in:
Cayo Puigdefabregas 2020-11-17 19:21:32 +01:00
parent e0b338b414
commit 69368a8ca3
2 changed files with 27 additions and 11 deletions

View File

@ -460,8 +460,12 @@ class Transferred(ActionWithMultipleDevices):
class Assigned(ActionWithMultipleDevices): class Assigned(ActionWithMultipleDevices):
__doc__ = m.Assigned.__doc__ __doc__ = m.Assigned.__doc__
shipping_date = DateTime(data_key='shippingDate') agent = NestedOn(s_agent.Agent, only_query='id', required=False, comment=m.Trade.to_comment)
invoice_number = SanitizedStr(validate=Length(max=STR_SIZE), data_key='invoiceNumber') description = SanitizedStr(default='', description=m.Action.description.comment)
price = NestedOn(Price) start_time = DateTime(data_key='startTime', description=m.Action.start_time.comment)
to = NestedOn(s_agent.Agent, only_query='id', required=True, comment=m.Trade.to_comment) end_time = DateTime(data_key='endTime', description=m.Action.end_time.comment)
confirms = NestedOn(Organize) 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)

View File

@ -1,25 +1,34 @@
import uuid
# from typing import Callable, Iterable, Tuple # from typing import Callable, Iterable, Tuple
# from flask import g from flask import g, request
# from flask.json import jsonify # from flask.json import jsonify
from teal.resource import View from teal.resource import View
from ereuse_devicehub import auth from ereuse_devicehub import auth
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 Assigned from ereuse_devicehub.resources.action.models import Assigned
class RentingView(View): class RentingView(View):
@auth.Auth.requires_auth @auth.Auth.requires_auth
def get(self, id): def get(self, id: uuid.UUID) -> Assigned:
return super().get(id) return super().get(id)
@auth.Auth.requires_auth @auth.Auth.requires_auth
def post(self): def post(self):
""" Create one rent """ """ Create one rent """
return super().get(id) res_json = request.get_json()
# return jsonify('ok') 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): def find(self, args: dict):
rents = Assigned.query.filter() \ rents = Assigned.query.filter_by(author=g.user) \
.order_by(Assigned.created.desc()) \ .order_by(Assigned.created.desc()) \
.paginate(per_page=200) .paginate(per_page=200)
return things_response( return things_response(
@ -27,4 +36,7 @@ class RentingView(View):
rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num 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)