diff --git a/shopinvader/services/abstract_sale.py b/shopinvader/services/abstract_sale.py index 6092404b13..f19f205257 100644 --- a/shopinvader/services/abstract_sale.py +++ b/shopinvader/services/abstract_sale.py @@ -25,9 +25,11 @@ def _parser_product(self): def _convert_one_sale(self, sale): sale.ensure_one() + state_label = self._get_selection_label(sale, "shopinvader_state") return { "id": sale.id, "state": sale.shopinvader_state, + "state_label": state_label, "name": sale.name, "date": sale.date_order, "step": self._convert_step(sale), diff --git a/shopinvader/services/service.py b/shopinvader/services/service.py index 23cc1fca33..23f542ae93 100644 --- a/shopinvader/services/service.py +++ b/shopinvader/services/service.py @@ -114,6 +114,20 @@ def _get(self, _id): def _get_base_search_domain(self): return [] + def _get_selection_label(self, record, field): + """ + Get the translated label of the record selection field + :param record: recordset + :param field: str + :return: str + """ + if field not in record._fields: + return "" + # convert_to_export(...) give the label of the selection (translated). + return record._fields.get(field).convert_to_export( + record[field], record + ) + def _get_openapi_default_parameters(self): defaults = super( BaseShopinvaderService, self diff --git a/shopinvader/tests/common.py b/shopinvader/tests/common.py index b0c8f35e1b..938f917241 100644 --- a/shopinvader/tests/common.py +++ b/shopinvader/tests/common.py @@ -97,6 +97,17 @@ def setUp(self): def cleanupShopinvaderResponseTestMode(): shopinvader_response.set_testmode(False) + def _get_selection_label(self, record, field): + """ + Get the translated label of the record selection field + :param record: recordset + :param field: str + :return: str + """ + return record._fields.get(field).convert_to_export( + record[field], record + ) + class ProductCommonCase(CommonCase): def setUp(self): diff --git a/shopinvader/tests/test_sale.py b/shopinvader/tests/test_sale.py index 6911577086..e5ffff1d3c 100644 --- a/shopinvader/tests/test_sale.py +++ b/shopinvader/tests/test_sale.py @@ -38,6 +38,11 @@ def test_read_sale(self): res = self.service.get(self.sale.id) self.assertEqual(res["id"], self.sale.id) self.assertEqual(res["name"], self.sale.name) + self.assertEqual(res["state"], self.sale.shopinvader_state) + self.assertEqual( + res["state_label"], + self._get_selection_label(self.sale, "shopinvader_state"), + ) def test_cart_are_not_readable_as_sale(self): with self.assertRaises(MissingError): @@ -50,6 +55,9 @@ def test_list_sale(self): sale = res["data"][0] self.assertEqual(sale["id"], self.sale.id) self.assertEqual(sale["name"], self.sale.name) + self.assertEqual(sale["state"], self.sale.shopinvader_state) + state_label = self._get_selection_label(self.sale, "shopinvader_state") + self.assertEqual(sale["state_label"], state_label) def test_hack_read_other_customer_sale(self): sale = self.env.ref("sale.sale_order_1") diff --git a/shopinvader_invoice/services/invoice.py b/shopinvader_invoice/services/invoice.py index 8744ea6871..4261aabfc6 100644 --- a/shopinvader_invoice/services/invoice.py +++ b/shopinvader_invoice/services/invoice.py @@ -30,6 +30,27 @@ def _validator_search(self): } return schema + def _validator_return_get(self): + """ + Output validator for the search + :return: dict + """ + invoice_schema = { + "invoice_id": {"type": "integer"}, + "number": {"type": "string"}, + "date_invoice": {"type": "string"}, + "amount_total": {"type": "float"}, + "amount_tax": {"type": "float"}, + "amount_untaxed": {"type": "float"}, + "amount_due": {"type": "float"}, + "type": {"type": "string"}, + "type_label": {"type": "string"}, + "state": {"type": "string"}, + "state_label": {"type": "string"}, + } + schema = {"data": {"type": "dict", "schema": invoice_schema}} + return schema + def _validator_return_search(self): """ Output validator for the search @@ -45,6 +66,8 @@ def _validator_return_search(self): "amount_due": {"type": "float"}, "type": {"type": "string"}, "state": {"type": "string"}, + "type_label": {"type": "string"}, + "state_label": {"type": "string"}, } schema = { "size": {"type": "integer"}, @@ -68,35 +91,19 @@ def _get_parser_invoice(self): "amount_tax", "amount_untaxed", "residual:amount_due", + "type", + "state", ] return to_parse - def _get_selection_label(self, invoice, field): - """ - Get the translated label of the invoice selection field - :param invoice: account.invoice recordset - :param field: str - :return: str - """ - if field not in invoice._fields: - return "" - # _description_selection return a list of tuple (str, str). - # Exactly like the definition of Selection field but this function - # translate possible values. - type_dict = dict( - invoice._fields.get(field)._description_selection(invoice.env) - ) - technical_value = invoice[field] - return type_dict.get(technical_value, technical_value) - def _to_json_invoice(self, invoice): invoice.ensure_one() parser = self._get_parser_invoice() values = invoice.jsonify(parser)[0] values.update( { - "type": self._get_selection_label(invoice, "type"), - "state": self._get_selection_label(invoice, "state"), + "type_label": self._get_selection_label(invoice, "type"), + "state_label": self._get_selection_label(invoice, "state"), } ) return values diff --git a/shopinvader_invoice/tests/test_invoice_service.py b/shopinvader_invoice/tests/test_invoice_service.py index 10d5f83891..7144a53984 100644 --- a/shopinvader_invoice/tests/test_invoice_service.py +++ b/shopinvader_invoice/tests/test_invoice_service.py @@ -31,19 +31,6 @@ def setUp(self, *args, **kwargs): ) as work: self.service_guest = work.component(usage="invoice") - def _get_selection_label(self, invoice, field): - """ - Get the translated label of the invoice selection field - :param invoice: account.invoice recordset - :param field: str - :return: str - """ - technical_type = invoice[field] - type_dict = dict( - invoice._fields.get(field)._description_selection(invoice.env) - ) - return type_dict.get(technical_type, technical_type) - def _check_data_content(self, data, invoices): """ Check data based on given invoices @@ -63,8 +50,10 @@ def _check_data_content(self, data, invoices): current_data.get("date_invoice"), fields.Date.to_string(invoice.date_invoice), ) - self.assertEquals(current_data.get("state"), state_label) - self.assertEquals(current_data.get("type"), type_label) + self.assertEquals(current_data.get("state"), invoice.state) + self.assertEquals(current_data.get("type"), invoice.type) + self.assertEquals(current_data.get("state_label"), state_label) + self.assertEquals(current_data.get("type_label"), type_label) self.assertEquals( current_data.get("amount_total"), invoice.amount_total )