From cd6d3f2fb31b4b594ee5645e76800ecd1a41e926 Mon Sep 17 00:00:00 2001 From: "robin.keunen" Date: Thu, 9 Apr 2020 17:00:53 +0200 Subject: [PATCH] [ADD] emc_api: update subscription request route --- easy_my_coop_api/services/schemas.py | 20 ++++ .../services/subscription_request_service.py | 103 ++++++++++++------ easy_my_coop_api/tests/common.py | 1 + .../tests/test_subscription_requests.py | 13 +++ 4 files changed, 105 insertions(+), 32 deletions(-) diff --git a/easy_my_coop_api/services/schemas.py b/easy_my_coop_api/services/schemas.py index aed7533..ba091ec 100644 --- a/easy_my_coop_api/services/schemas.py +++ b/easy_my_coop_api/services/schemas.py @@ -16,6 +16,7 @@ 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_BASE = { @@ -39,6 +40,7 @@ S_SUBSCRIPTION_REQUEST_GET = { **{ "id": {"type": "integer", "required": True}, "date": {"type": "string", "required": True, "empty": False}, + "state": {"type": "string", "required": True, "empty": False}, "share_product": { "type": "dict", "schema": { @@ -54,6 +56,24 @@ S_SUBSCRIPTION_REQUEST_CREATE = { **{"share_product": {"type": "integer", "required": True}}, } +S_SUBSCRIPTION_REQUEST_UPDATE = { + "name": {"type": "string"}, + "email": {"type": "string"}, + "ordered_parts": {"type": "integer"}, + "state": {"type": "string"}, + "address": { + "type": "dict", + "schema": { + "street": {"type": "string"}, + "zip_code": {"type": "string"}, + "city": {"type": "string"}, + "country": {"type": "string"}, + }, + }, + "lang": {"type": "string"}, + "share_product": {"type": "integer"}, +} + S_SUBSCRIPTION_REQUEST_LIST = { "count": {"type": "integer", "required": True}, "rows": { diff --git a/easy_my_coop_api/services/subscription_request_service.py b/easy_my_coop_api/services/subscription_request_service.py index 49d9ca7..cc7133c 100644 --- a/easy_my_coop_api/services/subscription_request_service.py +++ b/easy_my_coop_api/services/subscription_request_service.py @@ -22,12 +22,56 @@ class SubscriptionRequestService(Component): Subscription requests """ + def get(self, _id): + sr = self.env["subscription.request"].browse(_id) + if sr: + return self._to_dict(sr) + else: + raise wrapJsonException( + NotFound(_("No subscription request for id %s") % _id) + ) + + def search(self, date_from=None, date_to=None): + _logger.info("search from %s to %s" % (date_from, date_to)) + + domain = [] + if date_from: + date_from = Date.from_string(date_from) + domain.append(("date", ">=", date_from)) + if date_to: + date_to = Date.from_string(date_to) + domain.append(("date", "<=", date_to)) + + requests = self.env["subscription.request"].search(domain) + + response = { + "count": len(requests), + "rows": [self._to_dict(sr) for sr in requests], + } + return response + + def create(self, **params): + params = self._prepare_create(params) + sr = self.env["subscription.request"].create(params) + return self._to_dict(sr) + + def update(self, _id, **params): + params = self._prepare_update(params) + sr = self.env["subscription.request"].browse(_id) + if not sr: + raise wrapJsonException( + NotFound(_("No subscription request for id %s") % _id) + ) + sr.write(params) + return self._to_dict(sr) + def _to_dict(self, sr): sr.ensure_one() return { "id": sr.id, "name": sr.name, "email": sr.email, + "state": sr.state, "date": Date.to_string(sr.date), "ordered_parts": sr.ordered_parts, "share_product": { @@ -68,40 +112,29 @@ class SubscriptionRequestService(Component): "lang": params["lang"], } - def get(self, _id): - # fixme remove sudo - sr = self.env["subscription.request"].sudo().search([("id", "=", _id)]) - if sr: - return self._to_dict(sr) + def _prepare_update(self, params): + if "address" in params: + address = params["address"] + if "country" in address: + country = self._get_country(address["country"]).id + address["country"] = country else: - raise wrapJsonException( - NotFound(_("No subscription request for id %s") % _id) - ) - - def search(self, date_from=None, date_to=None): - # fixme remove sudo - _logger.info("search from %s to %s" % (date_from, date_to)) - - domain = [] - if date_from: - date_from = Date.from_string(date_from) - domain.append(("date", ">=", date_from)) - if date_to: - date_to = Date.from_string(date_to) - domain.append(("date", "<=", date_to)) - - requests = self.env["subscription.request"].sudo().search(domain) - - response = { - "count": len(requests), - "rows": [self._to_dict(sr) for sr in requests], + address = {} + + params = { + "name": params.get("name"), + "email": params.get("email"), + "state": params.get("state"), + "ordered_parts": params.get("ordered_parts"), + "share_product_id": params.get("share_product"), + "address": address.get("street"), + "zip_code": address.get("zip_code"), + "city": address.get("city"), + "country_id": address.get("country"), + "lang": params.get("lang"), } - return response - - def create(self, **params): - params = self._prepare_create(params) - sr = self.env["subscription.request"].create(params) - return self._to_dict(sr) + params = {k: v for k, v in params.items() if v is not None} + return params def _validator_get(self): return {"_id": {"type": "integer"}} @@ -129,3 +162,9 @@ class SubscriptionRequestService(Component): def _validator_return_create(self): return schemas.S_SUBSCRIPTION_REQUEST_GET + + def _validator_update(self): + return schemas.S_SUBSCRIPTION_REQUEST_UPDATE + + def _validator_return_update(self): + return schemas.S_SUBSCRIPTION_REQUEST_GET diff --git a/easy_my_coop_api/tests/common.py b/easy_my_coop_api/tests/common.py index 9f65a16..39e7a6f 100644 --- a/easy_my_coop_api/tests/common.py +++ b/easy_my_coop_api/tests/common.py @@ -48,6 +48,7 @@ class BaseEMCRestCase(BaseRestCase): "name": "Manuel Dublues", "email": "manuel@demo.net", "date": date, + "state": "draft", "ordered_parts": 3, "share_product": { "id": self.demo_share_product.id, diff --git a/easy_my_coop_api/tests/test_subscription_requests.py b/easy_my_coop_api/tests/test_subscription_requests.py index eef6b96..df97d3e 100644 --- a/easy_my_coop_api/tests/test_subscription_requests.py +++ b/easy_my_coop_api/tests/test_subscription_requests.py @@ -120,6 +120,7 @@ class TestSRController(BaseEMCRestCase): **data, **{ "date": Date.to_string(Date.today()), + "state": "draft", "share_product": { "id": self.demo_share_product.id, "name": self.demo_share_product.name, @@ -127,3 +128,15 @@ class TestSRController(BaseEMCRestCase): }, } self.assertEquals(expected, content) + + def test_route_update(self): + url = "/api/subscription_request/%s" % self.demo_request_1.id + data = {"state": "done"} + + response = self.http_post(url, data=data) + self.assertEquals(response.status_code, 200) + content = json.loads(response.content) + + expected = self.demo_request_1_dict + expected["state"] = "done" + self.assertEquals(expected, content)