Browse Source
[ADD] emca: external_id_mixin
[ADD] emca: external_id_mixin
[IMP] emca: use external_id for product_templatepull/116/head
robin.keunen
5 years ago
14 changed files with 222 additions and 140 deletions
-
16easy_my_coop/demo/coop.xml
-
2easy_my_coop_api/__manifest__.py
-
19easy_my_coop_api/data/sequences.xml
-
13easy_my_coop_api/demo/demo.xml
-
3easy_my_coop_api/models/__init__.py
-
22easy_my_coop_api/models/account_invoice.py
-
76easy_my_coop_api/models/external_id_mixin.py
-
24easy_my_coop_api/models/subscription_request.py
-
17easy_my_coop_api/services/subscription_request_service.py
-
1easy_my_coop_api/tests/__init__.py
-
32easy_my_coop_api/tests/common.py
-
83easy_my_coop_api/tests/test_external_id_mixin.py
-
3easy_my_coop_api/tests/test_registry.py
-
51easy_my_coop_api/tests/test_subscription_requests.py
@ -1,19 +0,0 @@ |
|||||
<?xml version="1.0" encoding="utf-8"?> |
|
||||
<!-- |
|
||||
Copyright 2020 Coop IT Easy |
|
||||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
|
||||
--> |
|
||||
<odoo> |
|
||||
<data noupdate="1"> |
|
||||
<record id="seq_subscription_request_external_id" model="ir.sequence"> |
|
||||
<field name="name">Subscritpion Request External ID sequence</field> |
|
||||
<field name="code">subscription.request.external.id</field> |
|
||||
<field name="number_next">3</field> |
|
||||
</record> |
|
||||
<record id="seq_account_invoice_external_id" model="ir.sequence"> |
|
||||
<field name="name">Subscritpion Request External ID sequence</field> |
|
||||
<field name="code">account.invoice.external.id</field> |
|
||||
<field name="number_next">3</field> |
|
||||
</record> |
|
||||
</data> |
|
||||
</odoo> |
|
@ -1,3 +1,2 @@ |
|||||
from . import auth_api_key |
from . import auth_api_key |
||||
from . import subscription_request |
|
||||
from . import account_invoice |
|
||||
|
from . import external_id_mixin |
@ -1,22 +0,0 @@ |
|||||
# Copyright 2020 Coop IT Easy SCRL fs |
|
||||
# Robin Keunen <robin@coopiteasy.be> |
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
|
||||
|
|
||||
from odoo import api, fields, models |
|
||||
|
|
||||
|
|
||||
class AccountInvoice(models.Model): |
|
||||
_inherit = "account.invoice" |
|
||||
|
|
||||
external_id = fields.Integer( |
|
||||
string="External ID", index=True, required=False |
|
||||
) |
|
||||
|
|
||||
@api.multi |
|
||||
def get_external_id(self): |
|
||||
self.ensure_one() |
|
||||
if not self.external_id: |
|
||||
self.external_id = self.env["ir.sequence"].next_by_code( |
|
||||
"account.invoice.external.id" |
|
||||
) |
|
||||
return self.external_id |
|
@ -0,0 +1,76 @@ |
|||||
|
# Copyright 2020 Coop IT Easy SCRL fs |
||||
|
# Robin Keunen <robin@coopiteasy.be> |
||||
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
||||
|
|
||||
|
from odoo import api, fields, models |
||||
|
|
||||
|
|
||||
|
class ExternalIdMixin(models.AbstractModel): |
||||
|
_name = "external.id.mixin" |
||||
|
_description = "External ID Mixin" |
||||
|
|
||||
|
# do not access directly, always use get_api_external_id method |
||||
|
_api_external_id = fields.Integer( |
||||
|
string="External ID", index=True, required=False |
||||
|
) |
||||
|
external_id_sequence_id = fields.Many2one( |
||||
|
comodel_name="ir.sequence", |
||||
|
string="External ID Sequence", |
||||
|
required=False, |
||||
|
) |
||||
|
|
||||
|
@api.multi |
||||
|
def set_external_sequence(self): |
||||
|
self.ensure_one() |
||||
|
code = "%s.external.id" % self._name |
||||
|
Sequence = self.env["ir.sequence"] |
||||
|
# check if code was created for that model |
||||
|
sequence = Sequence.search([("code", "=", code)]) |
||||
|
if not sequence: |
||||
|
sequence = Sequence.sudo().create( |
||||
|
{"name": code, "code": code, "number_next": 1} |
||||
|
) |
||||
|
|
||||
|
self.sudo().write({"external_id_sequence_id": sequence.id}) |
||||
|
return True |
||||
|
|
||||
|
@api.multi |
||||
|
def get_api_external_id(self): |
||||
|
self.ensure_one() |
||||
|
if not self.external_id_sequence_id: |
||||
|
self.set_external_sequence() |
||||
|
if not self._api_external_id: |
||||
|
self.sudo().write( |
||||
|
{"_api_external_id": self.external_id_sequence_id._next()} |
||||
|
) |
||||
|
return self._api_external_id |
||||
|
|
||||
|
|
||||
|
class ResPartner(models.Model): |
||||
|
_name = "res.partner" |
||||
|
_inherit = ["res.partner", "external.id.mixin"] |
||||
|
|
||||
|
|
||||
|
class SubscriptionRequest(models.Model): |
||||
|
_name = "subscription.request" |
||||
|
_inherit = ["subscription.request", "external.id.mixin"] |
||||
|
|
||||
|
|
||||
|
class AccountAccount(models.Model): |
||||
|
_name = "account.account" |
||||
|
_inherit = ["account.account", "external.id.mixin"] |
||||
|
|
||||
|
|
||||
|
class AccountJournal(models.Model): |
||||
|
_name = "account.journal" |
||||
|
_inherit = ["account.journal", "external.id.mixin"] |
||||
|
|
||||
|
|
||||
|
class AccountInvoice(models.Model): |
||||
|
_name = "account.invoice" |
||||
|
_inherit = ["account.invoice", "external.id.mixin"] |
||||
|
|
||||
|
|
||||
|
class ProductTemplate(models.Model): |
||||
|
_name = "product.template" |
||||
|
_inherit = ["product.template", "external.id.mixin"] |
@ -1,24 +0,0 @@ |
|||||
# Copyright 2020 Coop IT Easy SCRL fs |
|
||||
# Robin Keunen <robin@coopiteasy.be> |
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
|
||||
|
|
||||
from os.path import join |
|
||||
|
|
||||
from odoo import api, fields, models |
|
||||
|
|
||||
|
|
||||
class SubscriptionRequest(models.Model): |
|
||||
_inherit = "subscription.request" |
|
||||
|
|
||||
external_id = fields.Integer( |
|
||||
string="External ID", index=True, required=False |
|
||||
) |
|
||||
|
|
||||
@api.multi |
|
||||
def get_external_id(self): |
|
||||
self.ensure_one() |
|
||||
if not self.external_id: |
|
||||
self.external_id = self.env["ir.sequence"].next_by_code( |
|
||||
"subscription.request.external.id" |
|
||||
) |
|
||||
return self.external_id |
|
@ -1,3 +1,4 @@ |
|||||
from . import test_ping |
from . import test_ping |
||||
from . import test_registry |
from . import test_registry |
||||
|
from . import test_external_id_mixin |
||||
from . import test_subscription_requests |
from . import test_subscription_requests |
@ -0,0 +1,83 @@ |
|||||
|
# Copyright 2020 Coop IT Easy SCRL fs |
||||
|
# Robin Keunen <robin@coopiteasy.be> |
||||
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
||||
|
|
||||
|
from odoo.fields import Date |
||||
|
from odoo.tests import TransactionCase |
||||
|
|
||||
|
|
||||
|
class TestExternalIdMixin(TransactionCase): |
||||
|
def test_res_partner_api_external_id(self): |
||||
|
partner = self.env["res.partner"].create({"name": "Test Partner"}) |
||||
|
self.assertFalse(partner._api_external_id) |
||||
|
self.assertFalse(partner.external_id_sequence_id) |
||||
|
|
||||
|
external_id = partner.get_api_external_id() |
||||
|
self.assertTrue(bool(partner._api_external_id)) |
||||
|
self.assertTrue(bool(partner.external_id_sequence_id)) |
||||
|
|
||||
|
self.assertEquals(external_id, partner.get_api_external_id()) |
||||
|
|
||||
|
def test_subscription_request_api_external_id(self): |
||||
|
share_type = self.browse_ref( |
||||
|
"easy_my_coop.product_template_share_type_2_demo" |
||||
|
).product_variant_id |
||||
|
sr = self.env["subscription.request"].create( |
||||
|
{ |
||||
|
"name": "test create request", |
||||
|
"email": "test@demo.net", |
||||
|
"address": "schaerbeekstraat", |
||||
|
"zip_code": "1111", |
||||
|
"city": "Brussels", |
||||
|
"country_id": self.ref("base.be"), |
||||
|
"date": Date.today(), |
||||
|
"source": "manual", |
||||
|
"ordered_parts": 3, |
||||
|
"share_product_id": share_type.id, |
||||
|
"lang": "en_US", |
||||
|
} |
||||
|
) |
||||
|
self.assertFalse(sr._api_external_id) |
||||
|
self.assertFalse(sr.external_id_sequence_id) |
||||
|
|
||||
|
external_id = sr.get_api_external_id() |
||||
|
self.assertTrue(bool(sr._api_external_id)) |
||||
|
self.assertTrue(bool(sr.external_id_sequence_id)) |
||||
|
|
||||
|
self.assertEquals(external_id, sr.get_api_external_id()) |
||||
|
|
||||
|
def test_account_journal_api_external_id(self): |
||||
|
bank = self.env["res.partner.bank"].create( |
||||
|
{ |
||||
|
"acc_number": "test", |
||||
|
"partner_id": self.env.user.company_id.partner_id.id, |
||||
|
} |
||||
|
) |
||||
|
journal = self.env["account.journal"].create( |
||||
|
{ |
||||
|
"name": "test journal", |
||||
|
"code": "123", |
||||
|
"type": "bank", |
||||
|
"company_id": self.env.ref("base.main_company").id, |
||||
|
"bank_account_id": bank.id, |
||||
|
} |
||||
|
) |
||||
|
self.assertFalse(journal._api_external_id) |
||||
|
self.assertFalse(journal.external_id_sequence_id) |
||||
|
|
||||
|
external_id = journal.get_api_external_id() |
||||
|
self.assertTrue(bool(journal._api_external_id)) |
||||
|
self.assertTrue(bool(journal.external_id_sequence_id)) |
||||
|
|
||||
|
self.assertEquals(external_id, journal.get_api_external_id()) |
||||
|
|
||||
|
def test_account_invoice_api_external_id(self): |
||||
|
invoice = self.env["account.invoice"].create({"name": "create passes"}) |
||||
|
self.assertFalse(invoice._api_external_id) |
||||
|
self.assertFalse(invoice.external_id_sequence_id) |
||||
|
|
||||
|
external_id = invoice.get_api_external_id() |
||||
|
self.assertTrue(bool(invoice._api_external_id)) |
||||
|
self.assertTrue(bool(invoice.external_id_sequence_id)) |
||||
|
|
||||
|
self.assertEquals(external_id, invoice.get_api_external_id()) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue