diff --git a/shopinvader/services/abstract_sale.py b/shopinvader/services/abstract_sale.py index 7b9e5ca40f..ab84f5a810 100644 --- a/shopinvader/services/abstract_sale.py +++ b/shopinvader/services/abstract_sale.py @@ -22,9 +22,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 c95692a634..1d5fc6a536 100644 --- a/shopinvader/services/service.py +++ b/shopinvader/services/service.py @@ -98,6 +98,24 @@ 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 "" + # _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( + record._fields.get(field)._description_selection(record.env) + ) + technical_value = record[field] + return type_dict.get(technical_value, technical_value) + def _get_openapi_default_parameters(self): defaults = super( BaseShopinvaderService, self diff --git a/shopinvader/tests/common.py b/shopinvader/tests/common.py index 1ebd82237a..f73c708a8b 100644 --- a/shopinvader/tests/common.py +++ b/shopinvader/tests/common.py @@ -75,6 +75,19 @@ 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 + """ + technical_type = record[field] + type_dict = dict( + record._fields.get(field)._description_selection(record.env) + ) + return type_dict.get(technical_type, technical_type) + class ProductCommonCase(CommonCase): def setUp(self): diff --git a/shopinvader/tests/test_sale.py b/shopinvader/tests/test_sale.py index ea0497bbe3..3d76a9d08d 100644 --- a/shopinvader/tests/test_sale.py +++ b/shopinvader/tests/test_sale.py @@ -39,6 +39,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): @@ -51,6 +56,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 1efa027399..c81d3ccd79 100644 --- a/shopinvader_invoice/services/invoice.py +++ b/shopinvader_invoice/services/invoice.py @@ -46,6 +46,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"}, @@ -69,35 +71,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 7d5ebdb850..c75bef1e8d 100644 --- a/shopinvader_invoice/tests/test_invoice_service.py +++ b/shopinvader_invoice/tests/test_invoice_service.py @@ -32,19 +32,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): self.assertEquals( current_data.get("date_invoice"), 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 )