From b033b3195aff25371796d221c9cd6e2b18412f25 Mon Sep 17 00:00:00 2001 From: "robin.keunen" Date: Thu, 11 Jun 2020 17:36:55 +0200 Subject: [PATCH] [IMP] emca: return dictionary for one2many fields --- .../services/account_invoice_service.py | 22 ++++++------- easy_my_coop_api/services/schemas.py | 31 +++++++++++-------- .../services/subscription_request_service.py | 11 ++++--- .../tests/test_account_invoice.py | 12 +++---- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/easy_my_coop_api/services/account_invoice_service.py b/easy_my_coop_api/services/account_invoice_service.py index 1eda236..54f5d50 100644 --- a/easy_my_coop_api/services/account_invoice_service.py +++ b/easy_my_coop_api/services/account_invoice_service.py @@ -41,12 +41,6 @@ class AccountInvoiceService(Component): def _to_dict(self, invoice): invoice.ensure_one() - if invoice.subscription_request: - sr_external_id = invoice.subscription_request.get_api_external_id() - else: - sr_external_id = None - - # todo return dictionaries for Many2one fields data = { "id": invoice.get_api_external_id(), "name": invoice.name, @@ -55,10 +49,12 @@ class AccountInvoiceService(Component): "date": Date.to_string(invoice.date), "date_due": Date.to_string(invoice.date_due), "date_invoice": Date.to_string(invoice.date_invoice), - "partner": invoice.partner_id.get_api_external_id(), - "journal": invoice.journal_id.get_api_external_id(), - "account": invoice.account_id.get_api_external_id(), - "subscription_request": sr_external_id, + "partner": self._one_to_many_to_dict(invoice.partner_id), + "journal": self._one_to_many_to_dict(invoice.journal_id), + "account": self._one_to_many_to_dict(invoice.account_id), + "subscription_request": self._one_to_many_to_dict( + invoice.subscription_request + ), "invoice_lines": [ self._line_to_dict(line) for line in invoice.invoice_line_ids ], @@ -68,8 +64,10 @@ class AccountInvoiceService(Component): def _line_to_dict(self, line): return { "name": line.name, - "account": line.account_id.get_api_external_id(), - "product": line.product_id.product_tmpl_id.get_api_external_id(), + "account": self._one_to_many_to_dict(line.account_id), + "product": self._one_to_many_to_dict( + line.product_id.product_tmpl_id + ), "quantity": line.quantity, "price_unit": line.price_unit, } diff --git a/easy_my_coop_api/services/schemas.py b/easy_my_coop_api/services/schemas.py index a1886fc..4f19bb8 100644 --- a/easy_my_coop_api/services/schemas.py +++ b/easy_my_coop_api/services/schemas.py @@ -15,6 +15,14 @@ def date_validator(field, value, error): ) +S_MANY_2_ONE = { + "type": "dict", + "schema": { + "id": {"type": "integer", "required": True}, + "name": {"type": "string", "required": True, "empty": False}, + }, +} + S_SUBSCRIPTION_REQUEST_GET = {"_id": {"type": "integer"}} S_SUBSCRIPTION_REQUEST_RETURN_GET = { @@ -24,13 +32,7 @@ S_SUBSCRIPTION_REQUEST_RETURN_GET = { "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}, - }, - }, + "share_product": S_MANY_2_ONE, "address": { "type": "dict", "schema": { @@ -110,8 +112,8 @@ S_INVOICE_LINE_RETURN_GET = { "type": "dict", "schema": { "name": {"type": "string", "required": True}, - "account": {"type": "integer", "required": True}, - "product": {"type": "integer", "required": True}, + "account": S_MANY_2_ONE, + "product": S_MANY_2_ONE, "quantity": {"type": "float", "required": True}, "price_unit": {"type": "float", "required": True}, }, @@ -128,9 +130,12 @@ S_INVOICE_RETURN_GET = { "date": {"type": "string", "required": True, "empty": False}, "date_due": {"type": "string", "required": True, "empty": False}, "date_invoice": {"type": "string", "required": True, "empty": False}, - "partner": {"type": "integer", "required": True}, - "journal": {"type": "integer", "required": True}, - "account": {"type": "integer", "required": True}, - "subscription_request": {"type": "integer", "nullable": True}, + "partner": S_MANY_2_ONE, + "journal": S_MANY_2_ONE, + "account": S_MANY_2_ONE, + "subscription_request": { + "type": "dict", + "schema": {"id": {"type": "integer"}, "name": {"type": "string"}}, + }, "invoice_lines": S_INVOICE_LINE_RETURN_GET, } diff --git a/easy_my_coop_api/services/subscription_request_service.py b/easy_my_coop_api/services/subscription_request_service.py index a56d68a..76f824d 100644 --- a/easy_my_coop_api/services/subscription_request_service.py +++ b/easy_my_coop_api/services/subscription_request_service.py @@ -111,10 +111,7 @@ class SubscriptionRequestService(Component): "state": sr.state, "date": Date.to_string(sr.date), "ordered_parts": sr.ordered_parts, - "share_product": { - "id": share_product.get_api_external_id(), - "name": share_product.name, - }, + "share_product": self._one_to_many_to_dict(share_product), "address": { "street": sr.address, "zip_code": sr.zip_code, @@ -125,6 +122,12 @@ class SubscriptionRequestService(Component): "capital_release_request": invoice_ids, } + def _one_to_many_to_dict(self, record): + if record: + return {"id": record.get_api_external_id(), "name": record.name} + else: + return {} + def _get_country(self, code): country = self.env["res.country"].search([("code", "=", code)]) if country: diff --git a/easy_my_coop_api/tests/test_account_invoice.py b/easy_my_coop_api/tests/test_account_invoice.py index 3fdf07a..2eaf902 100644 --- a/easy_my_coop_api/tests/test_account_invoice.py +++ b/easy_my_coop_api/tests/test_account_invoice.py @@ -32,10 +32,10 @@ class TestAccountInvoiceController(BaseEMCRestCase): self.demo_invoice_dict = { "id": 1, "name": "Capital Release Example", - "partner": 1, - "account": 1, - "journal": 1, - "subscription_request": None, + "partner": {"id": 1, "name": "Catherine des Champs"}, + "account": {"id": 1, "name": "Cooperators"}, + "journal": {"id": 1, "name": "Subscription Journal"}, + "subscription_request": {}, "state": "open", "date": today, "date_invoice": today, @@ -43,11 +43,11 @@ class TestAccountInvoiceController(BaseEMCRestCase): "type": "out_invoice", "invoice_lines": [ { - "account": 2, "name": "Share Type A", + "product": {"id": 1, "name": "Part A - Founder"}, "price_unit": 100.0, - "product": 1, "quantity": 2.0, + "account": {"id": 2, "name": "Equity"}, } ], }