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/assigned/views.py

43 lines
1.3 KiB
Python
Raw Normal View History

2020-11-17 18:21:32 +00:00
import uuid
2020-11-17 12:19:50 +00:00
# from typing import Callable, Iterable, Tuple
2020-11-17 18:21:32 +00:00
from flask import g, request
2020-11-17 12:19:50 +00:00
# from flask.json import jsonify
from teal.resource import View
from ereuse_devicehub import auth
2020-11-17 18:21:32 +00:00
from ereuse_devicehub.db import db
2020-11-17 12:19:50 +00:00
from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import Assigned
2020-11-17 18:21:32 +00:00
2020-11-18 10:31:23 +00:00
class AssignedView(View):
2020-11-17 12:19:50 +00:00
@auth.Auth.requires_auth
2020-11-17 18:21:32 +00:00
def get(self, id: uuid.UUID) -> Assigned:
2020-11-17 12:19:50 +00:00
return super().get(id)
@auth.Auth.requires_auth
def post(self):
""" Create one rent """
2020-11-17 18:21:32 +00:00
res_json = request.get_json()
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
2020-11-17 12:19:50 +00:00
def find(self, args: dict):
2020-11-17 18:21:32 +00:00
rents = Assigned.query.filter_by(author=g.user) \
2020-11-17 12:19:50 +00:00
.order_by(Assigned.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
)
2020-11-17 18:21:32 +00:00
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)