adding deallocate

This commit is contained in:
Cayo Puigdefabregas 2020-11-18 19:00:43 +01:00
parent cc6f16934d
commit 5adfad8a5e
3 changed files with 30 additions and 32 deletions

View file

@ -320,11 +320,7 @@ class Allocate(JoinedTableMixin, ActionWithMultipleDevices):
class Deallocate(JoinedTableMixin, ActionWithMultipleDevices): class Deallocate(JoinedTableMixin, ActionWithMultipleDevices):
@property pass
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))
class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice): class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice):

View file

@ -9,7 +9,6 @@ class AssignedDef(Resource):
SCHEMA = schemas.Allocate SCHEMA = schemas.Allocate
# class EndAssignedDef(Resource): class EndAssignedDef(Resource):
# VIEW = DeAllocateView VIEW = DeAllocateView
# SCHEMA = schemas.DeAllocate SCHEMA = schemas.Deallocate

View file

@ -7,68 +7,71 @@ from teal.resource import View
from ereuse_devicehub import auth from ereuse_devicehub import auth
from ereuse_devicehub.db import db 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 Allocate from ereuse_devicehub.resources.action.models import Allocate, Deallocate
class AllocateView(View): class AllocateView(View):
@auth.Auth.requires_auth @auth.Auth.requires_auth
def get(self, id: uuid.UUID) -> Allocate: def get(self, id: uuid.UUID) -> Allocate:
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 allocate """
res_json = request.get_json() res_json = request.get_json()
assigned = Allocate(**res_json) allocate = Allocate(**res_json)
db.session.add(assigned) db.session.add(allocate)
db.session().final_flush() db.session().final_flush()
ret = self.schema.jsonify(assigned) ret = self.schema.jsonify(allocate)
ret.status_code = 201 ret.status_code = 201
db.session.commit() db.session.commit()
return ret return ret
def find(self, args: dict): 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()) \ .order_by(Allocate.created.desc()) \
.paginate(per_page=200) .paginate(per_page=200)
return things_response( return things_response(
self.schema.dump(rents.items, many=True, nested=0), self.schema.dump(allocates.items, many=True, nested=0),
rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num allocates.page, allocates.per_page, allocates.total,
allocates.prev_num, allocates.next_num
) )
def one(self, id: uuid.UUID): def one(self, id: uuid.UUID):
"""Gets one action.""" """Gets one action."""
assigned = Allocate.query.filter_by(id=id, author=g.user).one() allocate = Allocate.query.filter_by(id=id, author=g.user).one()
return self.schema.jsonify(assigned, nested=2) return self.schema.jsonify(allocate, nested=2)
class DeAllocateView(View): class DeAllocateView(View):
@auth.Auth.requires_auth @auth.Auth.requires_auth
def get(self, id: uuid.UUID) -> Allocate: def get(self, id: uuid.UUID) -> Allocate:
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 Deallocate """
res_json = request.get_json() res_json = request.get_json()
assigned = Allocate(**res_json) deallocate = Deallocate(**res_json)
db.session.add(assigned) db.session.add(deallocate)
db.session().final_flush() db.session().final_flush()
ret = self.schema.jsonify(assigned) ret = self.schema.jsonify(deallocate)
ret.status_code = 201 ret.status_code = 201
db.session.commit() db.session.commit()
return ret return ret
def find(self, args: dict): def find(self, args: dict):
rents = Allocate.query.filter_by(author=g.user) \ deallocates = Deallocate.query.filter_by(author=g.user) \
.order_by(Allocate.created.desc()) \ .order_by(Deallocate.created.desc()) \
.paginate(per_page=200) .paginate(per_page=200)
return things_response( return things_response(
self.schema.dump(rents.items, many=True, nested=0), self.schema.dump(deallocates.items, many=True, nested=0),
rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num deallocates.page, deallocates.per_page, deallocates.total,
deallocates.prev_num, deallocates.next_num
) )
def one(self, id: uuid.UUID): def one(self, id: uuid.UUID):
"""Gets one action.""" """Gets one action."""
assigned = Allocate.query.filter_by(id=id, author=g.user).one() deallocate = Deallocate.query.filter_by(id=id, author=g.user).one()
return self.schema.jsonify(assigned, nested=2) res = self.schema.jsonify(deallocate, nested=2)
return res