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

78 lines
2.5 KiB
Python
Raw Normal View History

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