From 8acca4d2cf407b36e4d2269aa222e50707521651 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 16 Nov 2020 20:32:28 +0100 Subject: [PATCH] definition rent endpoint --- ereuse_devicehub/config.py | 4 ++- ereuse_devicehub/resources/action/__init__.py | 5 --- ereuse_devicehub/resources/rent/__init__.py | 0 .../resources/rent/definitions.py | 34 +++++++++++++++++++ ereuse_devicehub/resources/rent/model.py | 0 ereuse_devicehub/resources/rent/views.py | 0 6 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 ereuse_devicehub/resources/rent/__init__.py create mode 100644 ereuse_devicehub/resources/rent/definitions.py create mode 100644 ereuse_devicehub/resources/rent/model.py create mode 100644 ereuse_devicehub/resources/rent/views.py diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index f7ff436f..7c28f5fd 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -14,6 +14,7 @@ from ereuse_devicehub.resources.device import definitions from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.enums import PriceSoftware from ereuse_devicehub.resources.versions import versions +from ereuse_devicehub.resources.rent import definitions as rent_def class DevicehubConfig(Config): @@ -27,7 +28,8 @@ class DevicehubConfig(Config): import_resource(proof), import_resource(documents), import_resource(inventory), - import_resource(versions)), + import_resource(versions), + import_resource(rent_def)), ) PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str] DB_USER = config('DB_USER', 'dhub') diff --git a/ereuse_devicehub/resources/action/__init__.py b/ereuse_devicehub/resources/action/__init__.py index 19a87f2c..59f49b46 100644 --- a/ereuse_devicehub/resources/action/__init__.py +++ b/ereuse_devicehub/resources/action/__init__.py @@ -228,11 +228,6 @@ class DonateDef(ActionDef): SCHEMA = schemas.Donate -class RentDef(ActionDef): - VIEW = None - SCHEMA = schemas.Rent - - class MakeAvailable(ActionDef): VIEW = None SCHEMA = schemas.MakeAvailable diff --git a/ereuse_devicehub/resources/rent/__init__.py b/ereuse_devicehub/resources/rent/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/resources/rent/definitions.py b/ereuse_devicehub/resources/rent/definitions.py new file mode 100644 index 00000000..8f2ee535 --- /dev/null +++ b/ereuse_devicehub/resources/rent/definitions.py @@ -0,0 +1,34 @@ +from typing import Callable, Iterable, Tuple +from flask import g +from flask.json import jsonify +from ereuse_devicehub.resources import action as act +from ereuse_devicehub.resources.action.models import Rent +from ereuse_devicehub.resources.device.models import Device +from teal.resource import Converters, Resource, View +from ereuse_devicehub import auth +from ereuse_devicehub.query import things_response + + +class RentingView(View): + @auth.Auth.requires_auth + def get(self, id): + return super().get(id) + + @auth.Auth.requires_auth + def post(self): + """ Create one rent """ + return jsonify('ok') + + def find(self, args: dict): + rents = Rent.query.filter() \ + .order_by(Rent.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 + ) + + +class RentDef(Resource): + VIEW = RentingView + SCHEMA = act.schemas.Rent diff --git a/ereuse_devicehub/resources/rent/model.py b/ereuse_devicehub/resources/rent/model.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/resources/rent/views.py b/ereuse_devicehub/resources/rent/views.py new file mode 100644 index 00000000..e69de29b