forked from shopinvader/odoo-shopinvader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.0][ADD] Add wizard to bind shopinvader.partner (same behaviour li…
…ke partner portal access rights)
- Loading branch information
Showing
7 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright 2019 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo import exceptions | ||
|
||
from .common import CommonCase | ||
|
||
|
||
class TestShopinvaderPartnerBinding(CommonCase): | ||
""" | ||
Tests for shopinvader.partner.binding | ||
""" | ||
|
||
def setUp(self): | ||
super(TestShopinvaderPartnerBinding, self).setUp() | ||
self.binding_wiz_obj = self.env["shopinvader.partner.binding"] | ||
self.partner = self.env.ref("base.res_partner_2") | ||
|
||
def _get_shopinvader_partner(self, partner, backend): | ||
""" | ||
Get the shopinvader partner related to given partner on given backend. | ||
:param partner: res.partner | ||
:param backend: shopinvader.backend | ||
:return: shopinvader.partner | ||
""" | ||
shopinv_partner = partner.shopinvader_bind_ids.filtered( | ||
lambda r, b=backend: r.backend_id == b | ||
) | ||
return shopinv_partner | ||
|
||
def test_binding1(self): | ||
""" | ||
Test the binding by using the shopinvader.partner.binding wizard. | ||
:return: | ||
""" | ||
shopinv_partner = self._get_shopinvader_partner( | ||
self.partner, self.backend | ||
) | ||
# This partner shouldn't be already binded | ||
self.assertFalse(shopinv_partner) | ||
context = self.env.context.copy() | ||
context.update( | ||
{ | ||
"active_id": self.partner.id, | ||
"active_ids": self.partner.ids, | ||
"active_model": self.partner._name, | ||
} | ||
) | ||
wizard_obj = self.binding_wiz_obj.with_context(context) | ||
fields_list = wizard_obj.fields_get().keys() | ||
values = wizard_obj.default_get(fields_list) | ||
values.update({"shopinvader_backend_id": self.backend.id}) | ||
wizard = wizard_obj.create(values) | ||
wizard._onchange_shopinvader_backend_id() | ||
wizard.binding_lines.write({"bind": False}) | ||
with self.assertRaises(exceptions.UserError) as e: | ||
wizard.action_apply() | ||
self.assertIn("unbind is not implemented", e.exception.name) | ||
shopinv_partner = self._get_shopinvader_partner( | ||
self.partner, self.backend | ||
) | ||
# As we set bind = False, we check if the binding is not executed. | ||
self.assertFalse(shopinv_partner) | ||
wizard.binding_lines.write({"bind": True}) | ||
wizard.action_apply() | ||
shopinv_partner = self._get_shopinvader_partner( | ||
self.partner, self.backend | ||
) | ||
# But now we set bind = True so we check if it's done. | ||
self.assertTrue(shopinv_partner) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2019 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo import api, fields, models | ||
|
||
|
||
class ShopinvaderPartnerBinding(models.TransientModel): | ||
""" | ||
Wizard used to bind manually some partners on shopinvader backends | ||
""" | ||
|
||
_name = "shopinvader.partner.binding" | ||
_description = "Shopinvader partner binding" | ||
|
||
shopinvader_backend_id = fields.Many2one( | ||
comodel_name="shopinvader.backend", | ||
string="Shopinvader backend", | ||
required=True, | ||
ondelete="cascade", | ||
) | ||
binding_lines = fields.One2many( | ||
comodel_name="shopinvader.partner.binding.line", | ||
inverse_name="shopinvader_partner_binding_id", | ||
string="Lines", | ||
) | ||
|
||
@api.model | ||
def default_get(self, fields_list): | ||
""" | ||
Inherit the default_get to auto-fill the backend if only 1 is found | ||
:param fields_list: list of str | ||
:return: dict | ||
""" | ||
result = super(ShopinvaderPartnerBinding, self).default_get( | ||
fields_list | ||
) | ||
# Auto fill the backend if we have only 1 backend found | ||
backend = self.shopinvader_backend_id.search([], limit=2) | ||
if len(backend) == 1: | ||
result.update({"shopinvader_backend_id": backend.id}) | ||
return result | ||
|
||
@api.multi | ||
@api.onchange("shopinvader_backend_id") | ||
def _onchange_shopinvader_backend_id(self): | ||
""" | ||
Onchange for the shopinvader_backend_id field. | ||
Auto fill some info based on active_ids and selected backend. | ||
:return: | ||
""" | ||
if self.env.context.get("active_model") == "res.partner": | ||
partner_ids = self.env.context.get("active_ids", []) | ||
lines = [(6, False, [])] | ||
for partner in self.env["res.partner"].browse(partner_ids): | ||
shopinv_partner = partner.shopinvader_bind_ids.filtered( | ||
lambda x, b=self.shopinvader_backend_id: x.backend_id == b | ||
) | ||
if shopinv_partner: | ||
# If the user is already binded, ignore it | ||
continue | ||
values = {"partner_id": partner.id, "bind": False} | ||
lines.append((0, False, values)) | ||
self.binding_lines = lines | ||
|
||
@api.multi | ||
def action_apply(self): | ||
""" | ||
Apply binding | ||
:return: | ||
""" | ||
self.ensure_one() | ||
self.binding_lines.action_apply() | ||
return {"type": "ir.actions.act_window_close"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- Copyright 2019 ACSONE SA/NV | ||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> | ||
<odoo> | ||
<record model="ir.ui.view" id="shopinvader_partner_binding_form_view"> | ||
<field name="name">shopinvader.partner.binding.form (in shopinvader)</field> | ||
<field name="model">shopinvader.partner.binding</field> | ||
<field name="arch" type="xml"> | ||
<form> | ||
<group> | ||
<group> | ||
<field name="shopinvader_backend_id" widget="selection"/> | ||
</group> | ||
<group></group> | ||
</group> | ||
<div> | ||
Select which partners should belong to the Shopinvader backend in the list below. | ||
The email address of each selected contact must be valid and unique. | ||
Partners already binded are ignored. | ||
</div> | ||
<group> | ||
<field name="binding_lines" colspan="2" nolabel="1"> | ||
<tree editable="bottom" create="false" delete="false"> | ||
<field name="partner_id"/> | ||
<field name="email"/> | ||
<field name="bind"/> | ||
</tree> | ||
</field> | ||
</group> | ||
<footer> | ||
<button string="Apply" name="action_apply" type="object" class="btn-primary"/> | ||
<button string="Cancel" class="btn-default" special="cancel" /> | ||
</footer> | ||
</form> | ||
</field> | ||
</record> | ||
|
||
<act_window id="partner_binding_action" | ||
name="Bind to a website" | ||
src_model="res.partner" | ||
res_model="shopinvader.partner.binding" | ||
view_type="form" view_mode="form" | ||
key2="client_action_multi" target="new" | ||
groups="group_shopinvader_manager" | ||
/> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2019 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo import _, api, exceptions, fields, models | ||
|
||
|
||
class ShopinvaderPartnerBindingLine(models.TransientModel): | ||
""" | ||
Wizard lines used to bind manually some partners on shopinvader backends | ||
""" | ||
|
||
_name = "shopinvader.partner.binding.line" | ||
_description = "Shopinvader partner binding line" | ||
|
||
shopinvader_partner_binding_id = fields.Many2one( | ||
comodel_name="shopinvader.partner.binding", | ||
string="Wizard", | ||
required=True, | ||
ondelete="cascade", | ||
) | ||
partner_id = fields.Many2one( | ||
comodel_name="res.partner", | ||
string="Partner", | ||
required=True, | ||
ondelete="cascade", | ||
readonly=True, | ||
) | ||
email = fields.Char( | ||
related="partner_id.email", required=True, readonly=True | ||
) | ||
bind = fields.Boolean( | ||
help="Tick to bind the partner to the backend. Untick to unbind it." | ||
) | ||
|
||
@api.multi | ||
def action_apply(self): | ||
""" | ||
Bind selected partners | ||
:return: dict | ||
""" | ||
if self.filtered(lambda r: not r.bind): | ||
message = _( | ||
"The unbind is not implemented.\n" | ||
"If you want to continue, please delete lines where " | ||
"the bind field is not ticked." | ||
) | ||
raise exceptions.UserError(message) | ||
for record in self.filtered(lambda r: r.bind): | ||
backend = ( | ||
record.shopinvader_partner_binding_id.shopinvader_backend_id | ||
) | ||
# Ensure the binding doesn't exist yet | ||
partner_binding = record.partner_id.shopinvader_bind_ids.filtered( | ||
lambda x, b=backend: x.backend_id == b | ||
) | ||
if not partner_binding: | ||
bind_values = {"backend_id": backend.id} | ||
record.partner_id.write( | ||
{"shopinvader_bind_ids": [(0, False, bind_values)]} | ||
) | ||
return {} |