From db3eb937711b3736f822b968e3ce5bff54aaea80 Mon Sep 17 00:00:00 2001 From: "robin.keunen" Date: Wed, 15 Apr 2020 13:02:39 +0200 Subject: [PATCH] [REF] emc_api: method order,denormalize schemas --- easy_my_coop_api/controllers/controllers.py | 4 +- easy_my_coop_api/readme/ROADMAP.rst | 7 ++ easy_my_coop_api/services/ping_service.py | 6 +- easy_my_coop_api/services/schemas.py | 64 +++++++++++-------- .../services/subscription_request_service.py | 24 +++---- 5 files changed, 58 insertions(+), 47 deletions(-) create mode 100644 easy_my_coop_api/readme/ROADMAP.rst diff --git a/easy_my_coop_api/controllers/controllers.py b/easy_my_coop_api/controllers/controllers.py index 3c97de4..6567255 100644 --- a/easy_my_coop_api/controllers/controllers.py +++ b/easy_my_coop_api/controllers/controllers.py @@ -18,7 +18,7 @@ class UserController(main.RestController): auth="public", csrf=False, ) - def test(self, _service_name, _id=None, **params): + def test(self, _service_name): return self._process_method( - _service_name, "test", _id=_id, params=params + _service_name, "test", _id=None, params=None, ) diff --git a/easy_my_coop_api/readme/ROADMAP.rst b/easy_my_coop_api/readme/ROADMAP.rst new file mode 100644 index 0000000..5698982 --- /dev/null +++ b/easy_my_coop_api/readme/ROADMAP.rst @@ -0,0 +1,7 @@ +The API should generate and use an external id for records instead +of odoo's generated id. It would make importing and export data as +well as migrating across versions easier. + +One way would be to use uuid but the default BaseRESTController +routes would need to be rewritten: they only take integer as ids. +Another way is to define a sequence per model. diff --git a/easy_my_coop_api/services/ping_service.py b/easy_my_coop_api/services/ping_service.py index 738722b..ce63f1c 100644 --- a/easy_my_coop_api/services/ping_service.py +++ b/easy_my_coop_api/services/ping_service.py @@ -16,12 +16,12 @@ class PingService(Component): Ping services (test the api) """ - def test(self): - return {"message": _("Called ping on ping API")} - def search(self): return {"message": _("Called search on ping API")} + def test(self): + return {"message": _("Called ping on ping API")} + def _validator_test(self): return {} diff --git a/easy_my_coop_api/services/schemas.py b/easy_my_coop_api/services/schemas.py index ba091ec..f20dab7 100644 --- a/easy_my_coop_api/services/schemas.py +++ b/easy_my_coop_api/services/schemas.py @@ -15,14 +15,22 @@ def date_validator(field, value, error): ) -# todo consistency: S_SR_GET, S_SR_RETURN_GET, S_SR_POST ... -# and denormalize dict rather than updating them +S_SUBSCRIPTION_REQUEST_GET = {"_id": {"type": "integer"}} - -S_SUBSCRIPTION_REQUEST_BASE = { - "name": {"type": "string", "required": True, "empty": False}, +S_SUBSCRIPTION_REQUEST_RETURN_GET = { + "id": {"type": "integer", "required": True}, "email": {"type": "string", "required": True, "empty": False}, + "name": {"type": "string", "required": True, "empty": False}, + "date": {"type": "string", "required": True, "empty": False}, + "state": {"type": "string", "required": True, "empty": False}, "ordered_parts": {"type": "integer", "required": True}, + "share_product": { + "type": "dict", + "schema": { + "id": {"type": "integer", "required": True}, + "name": {"type": "string", "required": True, "empty": False}, + }, + }, "address": { "type": "dict", "schema": { @@ -35,25 +43,37 @@ S_SUBSCRIPTION_REQUEST_BASE = { "lang": {"type": "string", "required": True, "empty": False}, } -S_SUBSCRIPTION_REQUEST_GET = { - **S_SUBSCRIPTION_REQUEST_BASE, - **{ - "id": {"type": "integer", "required": True}, - "date": {"type": "string", "required": True, "empty": False}, - "state": {"type": "string", "required": True, "empty": False}, - "share_product": { +S_SUBSCRIPTION_REQUEST_SEARCH = { + "date_from": {"type": "string", "check_with": date_validator}, + "date_to": {"type": "string", "check_with": date_validator}, +} + +S_SUBSCRIPTION_REQUEST_RETURN_SEARCH = { + "count": {"type": "integer", "required": True}, + "rows": { + "type": "list", + "schema": { "type": "dict", - "schema": { - "id": {"type": "integer", "required": True}, - "name": {"type": "string", "required": True, "empty": False}, - }, + "schema": S_SUBSCRIPTION_REQUEST_RETURN_GET, }, }, } S_SUBSCRIPTION_REQUEST_CREATE = { - **S_SUBSCRIPTION_REQUEST_BASE, - **{"share_product": {"type": "integer", "required": True}}, + "name": {"type": "string", "required": True, "empty": False}, + "email": {"type": "string", "required": True, "empty": False}, + "ordered_parts": {"type": "integer", "required": True}, + "share_product": {"type": "integer", "required": True}, + "address": { + "type": "dict", + "schema": { + "street": {"type": "string", "required": True, "empty": False}, + "zip_code": {"type": "string", "required": True, "empty": False}, + "city": {"type": "string", "required": True, "empty": False}, + "country": {"type": "string", "required": True, "empty": False}, + }, + }, + "lang": {"type": "string", "required": True, "empty": False}, } S_SUBSCRIPTION_REQUEST_UPDATE = { @@ -73,11 +93,3 @@ S_SUBSCRIPTION_REQUEST_UPDATE = { "lang": {"type": "string"}, "share_product": {"type": "integer"}, } - -S_SUBSCRIPTION_REQUEST_LIST = { - "count": {"type": "integer", "required": True}, - "rows": { - "type": "list", - "schema": {"type": "dict", "schema": S_SUBSCRIPTION_REQUEST_GET}, - }, -} diff --git a/easy_my_coop_api/services/subscription_request_service.py b/easy_my_coop_api/services/subscription_request_service.py index cc7133c..7017cbf 100644 --- a/easy_my_coop_api/services/subscription_request_service.py +++ b/easy_my_coop_api/services/subscription_request_service.py @@ -16,7 +16,8 @@ _logger = logging.getLogger(__name__) class SubscriptionRequestService(Component): _inherit = "base.rest.service" _name = "subscription.request.services" - _usage = "subscription_request" # service_name + # service_name todo subscription-request + _usage = "subscription_request" _collection = "emc.services" _description = """ Subscription requests @@ -137,34 +138,25 @@ class SubscriptionRequestService(Component): return params def _validator_get(self): - return {"_id": {"type": "integer"}} + return schemas.S_SUBSCRIPTION_REQUEST_GET def _validator_return_get(self): - return schemas.S_SUBSCRIPTION_REQUEST_GET + return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET def _validator_search(self): - return { - "date_from": { - "type": "string", - "check_with": schemas.date_validator, - }, - "date_to": { - "type": "string", - "check_with": schemas.date_validator, - }, - } + return schemas.S_SUBSCRIPTION_REQUEST_SEARCH def _validator_return_search(self): - return schemas.S_SUBSCRIPTION_REQUEST_LIST + return schemas.S_SUBSCRIPTION_REQUEST_RETURN_SEARCH def _validator_create(self): return schemas.S_SUBSCRIPTION_REQUEST_CREATE def _validator_return_create(self): - return schemas.S_SUBSCRIPTION_REQUEST_GET + return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET def _validator_update(self): return schemas.S_SUBSCRIPTION_REQUEST_UPDATE def _validator_return_update(self): - return schemas.S_SUBSCRIPTION_REQUEST_GET + return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET