Skip to content

Commit

Permalink
[FIX] confirmation_wizard: Code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
geomer198 committed Aug 28, 2024
1 parent af7b05b commit 43eedac
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 54 deletions.
6 changes: 3 additions & 3 deletions confirmation_wizard/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Use confirm wizard which return method (return_type = method):
def change_address(self, address):
if self._context("skip_confirm", False):
return (
self.env["confirmation.message.wizard"]
self.env["confirmation.wizard"]
.with_context(skip_confirm=True)
.confirm_message(
"Are you ready change partner address",
Expand All @@ -65,10 +65,10 @@ Use confirm wizard which return window close (return_type = window_close):
def method(self):
...
return (
self.env["confirmation.message.wizard"]
self.env["confirmation.wizard"]
.with_context(invisible_cancel=True)
.confirm_no_action_message(
text="Message",
message="Message",
title="Notification"
)
)
Expand Down
2 changes: 1 addition & 1 deletion confirmation_wizard/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"depends": ["base"],
"data": [
"security/ir.model.access.csv",
"wizard/confirmation_message_wizard.xml",
"wizard/confirmation_wizard.xml",
],
}
6 changes: 3 additions & 3 deletions confirmation_wizard/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Use confirm wizard which return method (return_type = method):
def change_address(self, address):
if self._context("skip_confirm", False):
return (
self.env["confirmation.message.wizard"]
self.env["confirmation.wizard"]
.with_context(skip_confirm=True)
.confirm_message(
"Are you ready change partner address",
Expand All @@ -25,10 +25,10 @@ Use confirm wizard which return window close (return_type = window_close):
def method(self):
...
return (
self.env["confirmation.message.wizard"]
self.env["confirmation.wizard"]
.with_context(invisible_cancel=True)
.confirm_no_action_message(
text="Message",
message="Message",
title="Notification"
)
)
2 changes: 1 addition & 1 deletion confirmation_wizard/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_confirmation_message_wizard,access.confirmation.message.wizard,model_confirmation_message_wizard,base.group_system,1,1,1,1
access_confirmation_wizard,access.confirmation.wizard,model_confirmation_wizard,base.group_system,1,1,1,1
6 changes: 3 additions & 3 deletions confirmation_wizard/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ <h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<span class="k">def</span> <span class="nf">change_address</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">address</span><span class="p">):</span><span class="w">
</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_context</span><span class="p">(</span><span class="s2">&quot;skip_confirm&quot;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span><span class="w">
</span> <span class="k">return</span> <span class="p">(</span><span class="w">
</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="p">[</span><span class="s2">&quot;confirmation.message.wizard&quot;</span><span class="p">]</span><span class="w">
</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="p">[</span><span class="s2">&quot;confirmation.wizard&quot;</span><span class="p">]</span><span class="w">
</span> <span class="o">.</span><span class="n">with_context</span><span class="p">(</span><span class="n">skip_confirm</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="w">
</span> <span class="o">.</span><span class="n">confirm_message</span><span class="p">(</span><span class="w">
</span> <span class="s2">&quot;Are you ready change partner address&quot;</span><span class="p">,</span><span class="w">
Expand All @@ -407,10 +407,10 @@ <h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span><span class="w">
</span> <span class="o">...</span><span class="w">
</span> <span class="k">return</span> <span class="p">(</span><span class="w">
</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="p">[</span><span class="s2">&quot;confirmation.message.wizard&quot;</span><span class="p">]</span><span class="w">
</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="p">[</span><span class="s2">&quot;confirmation.wizard&quot;</span><span class="p">]</span><span class="w">
</span> <span class="o">.</span><span class="n">with_context</span><span class="p">(</span><span class="n">invisible_cancel</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="w">
</span> <span class="o">.</span><span class="n">confirm_no_action_message</span><span class="p">(</span><span class="w">
</span> <span class="n">text</span><span class="o">=</span><span class="s2">&quot;Message&quot;</span><span class="p">,</span><span class="w">
</span> <span class="n">message</span><span class="o">=</span><span class="s2">&quot;Message&quot;</span><span class="p">,</span><span class="w">
</span> <span class="n">title</span><span class="o">=</span><span class="s2">&quot;Notification&quot;</span><span class="w">
</span> <span class="p">)</span><span class="w">
</span> <span class="p">)</span>
Expand Down
30 changes: 16 additions & 14 deletions confirmation_wizard/tests/test_confirmation_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ def setUp(self):

def test_confirm_message(self):
"""Test flow when create confirmation wizard with method"""
confirmation_message_wizard_obj = self.env["confirmation.message.wizard"]
confirmation_wizard_obj = self.env["confirmation.wizard"]

# Default behavior
action = confirmation_message_wizard_obj.confirm_message(
action = confirmation_wizard_obj.confirm_message(
"Message Test",
self.partner,
)
excepted_action = self.env["ir.actions.actions"]._for_xml_id(
"confirmation_wizard.confirmation_message_wizard_action"
"confirmation_wizard.confirmation_wizard_action"
)
excepted_action["context"] = self.env.context
self.assertDictEqual(action, excepted_action, "Dicts must be the same")

# Wizard with title and context
excepted_title = "Confirm Wizard"
action = confirmation_message_wizard_obj.with_context(
action = confirmation_wizard_obj.with_context(
invisible_cancel=True
).confirm_message("Message Test", self.partner, title=excepted_title)
self.assertEqual(action["name"], excepted_title, "Title must be the same")
Expand All @@ -38,43 +38,45 @@ def test_confirm_message(self):

def test_confirm_no_action_message(self):
"""Test flow when create confirmation wizard with window close"""
confirmation_message_wizard_obj = self.env["confirmation.message.wizard"]
confirmation_wizard_obj = self.env["confirmation.wizard"]

excepted_title = "Confirm Wizard"
action = confirmation_message_wizard_obj.confirm_message(
action = confirmation_wizard_obj.confirm_message(
"Message Test", self.partner, title=excepted_title
)
self.assertEqual(action["name"], excepted_title, "Title must be the same")

def test_action_confirm_method(self):
"""Test flow when confirm wizard with return type method"""
vals = {
"text": "Message Test",
"message": "Message Test",
"res_ids": "",
"return_type": "method",
"res_model": "res.partner",
"callback_method": "",
"callback_params": {},
}
wizard = self.env["confirmation.message.wizard"].create(vals)
wizard = self.env["confirmation.wizard"].create(vals)
with self.assertRaises(UserError) as e:
wizard.action_confirm()
self.assertEqual(
str(e.exception), "Records (IDS: ) not found in model res.partner."
str(e.exception), "Records (IDS: '') not found in model 'res.partner'."
)

vals1 = {**vals, "res_ids": ",".join(map(str, self.partner.ids))}
wizard = self.env["confirmation.message.wizard"].create(vals1)
wizard = self.env["confirmation.wizard"].create(vals1)
with self.assertRaises(UserError) as e:
wizard.action_confirm()
self.assertEqual(str(e.exception), "Method () is not found!")
self.assertEqual(
str(e.exception), "Method '' is not found on model 'res.partner'."
)

vals2 = {
**vals1,
"callback_method": "write",
"callback_params": {"vals": {"name": "New Partner #1"}},
}
wizard = self.env["confirmation.message.wizard"].create(vals2)
wizard = self.env["confirmation.wizard"].create(vals2)
result = wizard.action_confirm()
self.assertTrue(result, "Result must be True")
self.assertEqual(
Expand All @@ -85,9 +87,9 @@ def test_action_confirm_method(self):

def test_action_confirm_window_close(self):
"""Test flow when confirm wizard with return type window close"""
wizard = self.env["confirmation.message.wizard"].create(
wizard = self.env["confirmation.wizard"].create(
{
"text": "Message Confirmation Text",
"message": "Message Confirmation Text",
"return_type": "window_close",
}
)
Expand Down
2 changes: 1 addition & 1 deletion confirmation_wizard/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import confirmation_message_wizard
from . import confirmation_wizard
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Copyright (C) 2024 Cetmix OÜ
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from ast import literal_eval

from odoo import _, api, fields, models


class ConfirmationMessageWizard(models.TransientModel):
_name = "confirmation.message.wizard"
class ConfirmationWizard(models.TransientModel):
_name = "confirmation.wizard"
_description = "Confirmation Wizard"

text = fields.Char(string="Confirm Message", required=True)
message = fields.Char(string="Confirm Message", required=True)

res_ids = fields.Char()
res_model = fields.Char()
Expand All @@ -22,41 +23,43 @@ class ConfirmationMessageWizard(models.TransientModel):
("method", "Return Method"),
],
default="window_close",
required=True,
)

@api.model
def _prepare_action(self, title=None):
action = self.env["ir.actions.actions"]._for_xml_id(
"confirmation_wizard.confirmation_message_wizard_action"
"confirmation_wizard.confirmation_wizard_action"
)
action.update(
{
"name": title or _("Confirmation"),
"context": self._context,
}
)
if title:
action["name"] = title
action["context"] = self._context
return action

@api.model
def confirm_message(
self, text, records, title=None, method=None, callback_params=None
self, message, records, title=None, method=None, callback_params=None
):
callback_params = callback_params or {}
res_ids = ",".join(map(str, records.ids))
wizard = self.create(
{
"text": text,
"res_ids": res_ids,
"message": message,
"res_ids": repr(records.ids),
"return_type": "method",
"res_model": records._name,
"callback_method": method,
"callback_params": callback_params,
"callback_params": callback_params or {},
}
)
return wizard._prepare_action(title)

@api.model
def confirm_no_action_message(self, text, title=None):
def confirm_no_action_message(self, message, title=None):
wizard = self.create(
{
"text": text,
"message": message,
"return_type": "window_close",
}
)
Expand All @@ -66,17 +69,18 @@ def _confirm_window_close(self):
return {"type": "ir.actions.act_window_close"}

def _confirm_method(self):
if self.res_ids:
res_ids = map(int, self.res_ids.split(","))
else:
res_ids = []
res_ids = literal_eval(self.res_ids) if self.res_ids else []
records = self.env[self.res_model].browse(res_ids)
if not records.exists():
raise models.UserError(
_(f"Records (IDS: {self.res_ids}) not found in model {self.res_model}.")
_("Records (IDS: '%(ids)s') not found in model '%(model)s'.")
% {"ids": self.res_ids, "model": self.res_model}
)
if not hasattr(records, self.callback_method):
raise models.UserError(
_("Method '%(callback_method)s' is not found on model '%(res_model)s'.")
% {"callback_method": self.callback_method, "res_model": self.res_model}
)
if not (records and hasattr(records, self.callback_method)):
raise models.UserError(_(f"Method ({self.callback_method}) is not found!"))
return getattr(records, self.callback_method)(**self.callback_params)

def action_confirm(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="confirmation_message_wizard_form_view" model="ir.ui.view">
<field name="name">confirmation.message.wizard.form.view</field>
<field name="model">confirmation.message.wizard</field>
<record id="confirmation_wizard_form_view" model="ir.ui.view">
<field name="name">confirmation.wizard.form.view</field>
<field name="model">confirmation.wizard</field>
<field name="arch" type="xml">
<form>
<field name="text" readonly="1" />
<field name="message" readonly="1" />
<footer>
<button
string="Confirm"
Expand All @@ -26,10 +26,10 @@
</field>
</record>

<record id="confirmation_message_wizard_action" model="ir.actions.act_window">
<record id="confirmation_wizard_action" model="ir.actions.act_window">
<field name="name">Confirmation</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">confirmation.message.wizard</field>
<field name="res_model">confirmation.wizard</field>
<field name="target">new</field>
</record>

Expand Down

0 comments on commit 43eedac

Please sign in to comment.