Browse Source

[IMP] emca: sr_service.validate returns the new invoice

pull/115/head
robin.keunen 4 years ago
parent
commit
dc8b2276d9
  1. 2
      .isort.cfg
  2. 9
      easy_my_coop_api/demo/demo.xml
  3. 3
      easy_my_coop_api/models/external_id_mixin.py
  4. 8
      easy_my_coop_api/services/account_invoice_service.py
  5. 2
      easy_my_coop_api/services/schemas.py
  6. 7
      easy_my_coop_api/services/subscription_request_service.py
  7. 12
      easy_my_coop_api/tests/test_account_invoice.py
  8. 17
      easy_my_coop_api/tests/test_external_id_mixin.py
  9. 2
      easy_my_coop_api/tests/test_subscription_requests.py

2
.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

9
easy_my_coop_api/demo/demo.xml

@ -16,4 +16,13 @@
<record id="easy_my_coop.subscription_request_waiting_demo" model="subscription.request">
<field name="_api_external_id">2</field>
</record>
<record id="easy_my_coop.product_template_share_type_1_demo" model="product.template">
<field name="_api_external_id">1</field>
</record>
<record id="easy_my_coop.product_template_share_type_2_demo" model="product.template">
<field name="_api_external_id">2</field>
</record>
</odoo>

3
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

8
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),

2
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},

7
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

12
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)

17
easy_my_coop_api/tests/test_external_id_mixin.py

@ -2,6 +2,9 @@
# Robin Keunen <robin@coopiteasy.be>
# 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

2
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())

Loading…
Cancel
Save