This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/allocate/views.py

40 lines
1.1 KiB
Python
Raw Normal View History

2020-11-18 17:13:13 +00:00
from flask import g, request
from teal.resource import View
from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response
2020-11-21 12:17:23 +00:00
from ereuse_devicehub.resources.action.models import Allocate, Deallocate
2020-11-18 17:13:13 +00:00
2020-11-21 12:17:23 +00:00
class AllocateMix():
model = None
2020-11-18 18:00:43 +00:00
2020-11-18 17:13:13 +00:00
def post(self):
2020-11-21 12:17:23 +00:00
""" Create one res_obj """
2020-11-18 17:13:13 +00:00
res_json = request.get_json()
2020-11-21 12:17:23 +00:00
res_obj = model(**res_json)
db.session.add(res_obj)
2020-11-18 17:13:13 +00:00
db.session().final_flush()
2020-11-21 12:17:23 +00:00
ret = self.schema.jsonify(res_obj)
2020-11-18 17:13:13 +00:00
ret.status_code = 201
db.session.commit()
return ret
def find(self, args: dict):
2020-11-21 12:17:23 +00:00
res_objs = model.query.filter_by(author=g.user) \
.order_by(model.created.desc()) \
2020-11-18 17:13:13 +00:00
.paginate(per_page=200)
return things_response(
2020-11-21 12:17:23 +00:00
self.schema.dump(res_objs.items, many=True, nested=0),
res_objs.page, res_objs.per_page, res_objs.total,
res_objs.prev_num, res_objs.next_num
2020-11-18 17:13:13 +00:00
)
2020-11-21 12:17:23 +00:00
class AllocateView(AllocateMix):
model = Allocate
class DeallocateView(AllocateMix):
model = Deallocate