Skip to content

Commit

Permalink
Merge PR #404 into 10.0
Browse files Browse the repository at this point in the history
Signed-off-by Cedric-Pigeon
  • Loading branch information
shopinvader-git-bot committed Mar 20, 2020
2 parents 789194f + 5bca8da commit f51b078
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 35 deletions.
2 changes: 2 additions & 0 deletions shopinvader/services/abstract_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 14 additions & 0 deletions shopinvader/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,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 @@ -78,6 +78,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 @@ -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):
Expand All @@ -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")
Expand Down
28 changes: 8 additions & 20 deletions shopinvader_invoice/services/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def _validator_return_get(self):
"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
Expand All @@ -72,6 +74,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 @@ -95,35 +99,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 @@ -33,19 +33,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 @@ -64,8 +51,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
)
Expand Down

0 comments on commit f51b078

Please sign in to comment.