diff --git a/.isort.cfg b/.isort.cfg index 1517121..22988e0 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -9,4 +9,4 @@ line_length=79 known_odoo=odoo known_odoo_addons=odoo.addons sections=FUTURE,STDLIB,THIRDPARTY,ODOO,ODOO_ADDONS,FIRSTPARTY,LOCALFOLDER -known_third_party=addons,cStringIO,lxml,openerp,requests,setuptools,werkzeug,xlsxwriter +known_third_party=addons,cStringIO,lxml,openerp,psycopg2,requests,setuptools,werkzeug,xlsxwriter diff --git a/easy_my_coop_api/demo/demo.xml b/easy_my_coop_api/demo/demo.xml index 72d2102..2e15bb0 100644 --- a/easy_my_coop_api/demo/demo.xml +++ b/easy_my_coop_api/demo/demo.xml @@ -16,4 +16,13 @@ 2 + + + 1 + + + + 2 + + diff --git a/easy_my_coop_api/models/external_id_mixin.py b/easy_my_coop_api/models/external_id_mixin.py index 91e3b9b..1e699fa 100644 --- a/easy_my_coop_api/models/external_id_mixin.py +++ b/easy_my_coop_api/models/external_id_mixin.py @@ -54,9 +54,10 @@ class ExternalIdMixin(models.AbstractModel): n = 100 while True: try: + next_id = self.external_id_sequence_id._next() self.sudo().write( { - "_api_external_id": self.external_id_sequence_id._next() + "_api_external_id": next_id } ) break diff --git a/easy_my_coop_api/services/account_invoice_service.py b/easy_my_coop_api/services/account_invoice_service.py index 29216e5..6a39ecf 100644 --- a/easy_my_coop_api/services/account_invoice_service.py +++ b/easy_my_coop_api/services/account_invoice_service.py @@ -27,11 +27,11 @@ class AccountInvoiceService(Component): """ def get(self, _id): - sr = self.env["account.invoice"].search( + ai = self.env["account.invoice"].search( [("_api_external_id", "=", _id)] ) - if sr: - return self._to_dict(sr) + if ai: + return self._to_dict(ai) else: raise wrapJsonException( NotFound(_("No invoice found for id %s") % _id) @@ -42,7 +42,7 @@ class AccountInvoiceService(Component): data = { "id": invoice.get_api_external_id(), - "name": invoice.name, + "number": invoice.number, "state": invoice.state, "type": invoice.type, "date": Date.to_string(invoice.date), diff --git a/easy_my_coop_api/services/schemas.py b/easy_my_coop_api/services/schemas.py index 542c9f0..9f6166d 100644 --- a/easy_my_coop_api/services/schemas.py +++ b/easy_my_coop_api/services/schemas.py @@ -124,7 +124,7 @@ S_INVOICE_LINE_RETURN_GET = { S_INVOICE_RETURN_GET = { "id": {"type": "integer", "required": True}, - "name": {"type": "string", "required": True, "empty": False}, + "number": {"type": "string", "required": True, "empty": False}, "state": {"type": "string", "required": True, "empty": False}, "type": {"type": "string", "required": True, "empty": False}, "date": {"type": "string", "required": True, "empty": False}, diff --git a/easy_my_coop_api/services/subscription_request_service.py b/easy_my_coop_api/services/subscription_request_service.py index 66e3a2e..ed05258 100644 --- a/easy_my_coop_api/services/subscription_request_service.py +++ b/easy_my_coop_api/services/subscription_request_service.py @@ -88,8 +88,9 @@ class SubscriptionRequestService(Component): _("Subscription request %s is not in draft state") % _id ) ) - sr.validate_subscription_request() - return self._to_dict(sr) + invoice = sr.validate_subscription_request() + invoice_service = self.work.component(usage="invoice") + return invoice_service.get(invoice.get_api_external_id()) def _to_dict(self, sr): sr.ensure_one() @@ -219,4 +220,4 @@ class SubscriptionRequestService(Component): return schemas.S_SUBSCRIPTION_REQUEST_VALIDATE def _validator_return_validate(self): - return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET + return schemas.S_INVOICE_RETURN_GET diff --git a/easy_my_coop_api/tests/test_account_invoice.py b/easy_my_coop_api/tests/test_account_invoice.py index 2eaf902..4919ef5 100644 --- a/easy_my_coop_api/tests/test_account_invoice.py +++ b/easy_my_coop_api/tests/test_account_invoice.py @@ -31,7 +31,7 @@ class TestAccountInvoiceController(BaseEMCRestCase): today = Date.to_string(Date.today()) self.demo_invoice_dict = { "id": 1, - "name": "Capital Release Example", + "number": "xxx", # can't guess it "partner": {"id": 1, "name": "Catherine des Champs"}, "account": {"id": 1, "name": "Cooperators"}, "journal": {"id": 1, "name": "Subscription Journal"}, @@ -79,7 +79,7 @@ class TestAccountInvoiceController(BaseEMCRestCase): self.capital_release = self.env["account.invoice"].create( { - "name": "Capital Release Example", + "number": "Capital Release Example", "partner_id": self.coop_candidate.id, "type": "out_invoice", "invoice_line_ids": capital_release_line, @@ -92,10 +92,14 @@ class TestAccountInvoiceController(BaseEMCRestCase): def test_service_get(self): external_id = self.capital_release.get_api_external_id() result = self.ai_service.get(external_id) - self.assertEquals(self.demo_invoice_dict, result) + expected = self.demo_invoice_dict.copy() + expected["number"] = result["number"] + self.assertEquals(expected, result) def test_route_get(self): external_id = self.capital_release.get_api_external_id() route = "/api/invoice/%s" % external_id content = self.http_get_content(route) - self.assertEquals(self.demo_invoice_dict, content) + expected = self.demo_invoice_dict.copy() + expected["number"] = content["number"] + self.assertEquals(expected, content) diff --git a/easy_my_coop_api/tests/test_external_id_mixin.py b/easy_my_coop_api/tests/test_external_id_mixin.py index 0f7f0a3..242e7a3 100644 --- a/easy_my_coop_api/tests/test_external_id_mixin.py +++ b/easy_my_coop_api/tests/test_external_id_mixin.py @@ -2,6 +2,9 @@ # Robin Keunen # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from psycopg2 import IntegrityError + +import odoo from odoo.fields import Date from odoo.tests import TransactionCase @@ -81,3 +84,17 @@ class TestExternalIdMixin(TransactionCase): self.assertTrue(bool(invoice.external_id_sequence_id)) self.assertEquals(external_id, invoice.get_api_external_id()) + + @odoo.tools.mute_logger("odoo.sql_db") + def test_duplicate_api_external_id_raises(self): + invoice_1 = self.env["account.invoice"].create( + {"name": "create passes"} + ) + external_id = invoice_1.get_api_external_id() + self.assertTrue(bool(invoice_1._api_external_id)) + + invoice_2 = self.env["account.invoice"].create( + {"name": "create passes"} + ) + with self.assertRaises(IntegrityError): + invoice_2._api_external_id = external_id diff --git a/easy_my_coop_api/tests/test_subscription_requests.py b/easy_my_coop_api/tests/test_subscription_requests.py index be1102b..8abb081 100644 --- a/easy_my_coop_api/tests/test_subscription_requests.py +++ b/easy_my_coop_api/tests/test_subscription_requests.py @@ -193,7 +193,7 @@ class TestSRController(BaseEMCRestCase): content = json.loads(response.content.decode("utf-8")) state = content.get("state") - self.assertEquals(state, "done") + self.assertEquals(state, "open") def test_service_validate_draft_request(self): self.sr_service.validate(self.demo_request_1.get_api_external_id())