Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][FWD] Add translated label of selection fields into returned js… #638

Merged
merged 1 commit into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shopinvader/services/abstract_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 14 additions & 0 deletions shopinvader/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions shopinvader/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions shopinvader/tests/test_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
Expand Down
47 changes: 27 additions & 20 deletions shopinvader_invoice/services/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"},
Expand All @@ -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
Expand Down
19 changes: 4 additions & 15 deletions shopinvader_invoice/tests/test_invoice_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down