From 5adfad8a5e5bcf18ddb7c6b39e7e60afccfb6b4c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 18 Nov 2020 19:00:43 +0100 Subject: [PATCH] adding deallocate --- ereuse_devicehub/resources/action/models.py | 6 +-- .../resources/allocate/definitions.py | 7 ++- ereuse_devicehub/resources/allocate/views.py | 49 ++++++++++--------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 5f442e2d..06c9e6cc 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -320,11 +320,7 @@ class Allocate(JoinedTableMixin, ActionWithMultipleDevices): class Deallocate(JoinedTableMixin, ActionWithMultipleDevices): - @property - def allocate(self): - """The URL of the allocate than closes one allocate. """ - allocate = [a for a in self.devices[0].actions if is_instance(action, 'Allocate')][-1] - return urlutils.URL(url_for_resource('Allocate', item=allocate)) + pass class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice): diff --git a/ereuse_devicehub/resources/allocate/definitions.py b/ereuse_devicehub/resources/allocate/definitions.py index e5c6f62c..7cda6eb4 100644 --- a/ereuse_devicehub/resources/allocate/definitions.py +++ b/ereuse_devicehub/resources/allocate/definitions.py @@ -9,7 +9,6 @@ class AssignedDef(Resource): SCHEMA = schemas.Allocate -# class EndAssignedDef(Resource): - # VIEW = DeAllocateView - # SCHEMA = schemas.DeAllocate - +class EndAssignedDef(Resource): + VIEW = DeAllocateView + SCHEMA = schemas.Deallocate diff --git a/ereuse_devicehub/resources/allocate/views.py b/ereuse_devicehub/resources/allocate/views.py index b87c592e..4a368799 100644 --- a/ereuse_devicehub/resources/allocate/views.py +++ b/ereuse_devicehub/resources/allocate/views.py @@ -7,68 +7,71 @@ 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 Allocate +from ereuse_devicehub.resources.action.models import Allocate, Deallocate class AllocateView(View): @auth.Auth.requires_auth def get(self, id: uuid.UUID) -> Allocate: return super().get(id) - + @auth.Auth.requires_auth def post(self): - """ Create one rent """ + """ Create one allocate """ res_json = request.get_json() - assigned = Allocate(**res_json) - db.session.add(assigned) + allocate = Allocate(**res_json) + db.session.add(allocate) db.session().final_flush() - ret = self.schema.jsonify(assigned) + ret = self.schema.jsonify(allocate) ret.status_code = 201 db.session.commit() return ret def find(self, args: dict): - rents = Allocate.query.filter_by(author=g.user) \ + allocates = Allocate.query.filter_by(author=g.user) \ .order_by(Allocate.created.desc()) \ .paginate(per_page=200) return things_response( - self.schema.dump(rents.items, many=True, nested=0), - rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num + self.schema.dump(allocates.items, many=True, nested=0), + allocates.page, allocates.per_page, allocates.total, + allocates.prev_num, allocates.next_num ) def one(self, id: uuid.UUID): """Gets one action.""" - assigned = Allocate.query.filter_by(id=id, author=g.user).one() - return self.schema.jsonify(assigned, nested=2) + allocate = Allocate.query.filter_by(id=id, author=g.user).one() + return self.schema.jsonify(allocate, nested=2) + - class DeAllocateView(View): @auth.Auth.requires_auth def get(self, id: uuid.UUID) -> Allocate: return super().get(id) - + @auth.Auth.requires_auth def post(self): - """ Create one rent """ + """ Create one Deallocate """ res_json = request.get_json() - assigned = Allocate(**res_json) - db.session.add(assigned) + deallocate = Deallocate(**res_json) + db.session.add(deallocate) db.session().final_flush() - ret = self.schema.jsonify(assigned) + ret = self.schema.jsonify(deallocate) ret.status_code = 201 db.session.commit() return ret def find(self, args: dict): - rents = Allocate.query.filter_by(author=g.user) \ - .order_by(Allocate.created.desc()) \ + deallocates = Deallocate.query.filter_by(author=g.user) \ + .order_by(Deallocate.created.desc()) \ .paginate(per_page=200) return things_response( - self.schema.dump(rents.items, many=True, nested=0), - rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num + self.schema.dump(deallocates.items, many=True, nested=0), + deallocates.page, deallocates.per_page, deallocates.total, + deallocates.prev_num, deallocates.next_num ) def one(self, id: uuid.UUID): """Gets one action.""" - assigned = Allocate.query.filter_by(id=id, author=g.user).one() - return self.schema.jsonify(assigned, nested=2) + deallocate = Deallocate.query.filter_by(id=id, author=g.user).one() + res = self.schema.jsonify(deallocate, nested=2) + return res