mirror of
https://github.com/OCA/server-backend.git
synced 2025-02-18 09:52:42 +02:00
[ADD] server_action_navigate
This commit is contained in:
2
server_action_navigate/models/__init__.py
Normal file
2
server_action_navigate/models/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from . import ir_actions_server
|
||||
from . import ir_actions_server_navigate_line
|
||||
89
server_action_navigate/models/ir_actions_server.py
Normal file
89
server_action_navigate/models/ir_actions_server.py
Normal file
@@ -0,0 +1,89 @@
|
||||
# Copyright (C) 2020 - Today: GRAP (http://www.grap.coop)
|
||||
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class IrActionsServer(models.Model):
|
||||
_inherit = "ir.actions.server"
|
||||
|
||||
state = fields.Selection(selection_add=[("navigate", "Navigate")])
|
||||
|
||||
navigate_action_id = fields.Many2one(
|
||||
string="Navigation Action",
|
||||
comodel_name='ir.actions.act_window',
|
||||
domain="[('res_model', '=', max_navigate_line_model)]",
|
||||
help="Define here the action used when the navigation will be executed"
|
||||
" if empty, a generic action will be used.")
|
||||
|
||||
navigate_line_ids = fields.One2many(
|
||||
comodel_name="ir.actions.server.navigate.line",
|
||||
string="Navigate Lines", inverse_name="action_id")
|
||||
|
||||
max_navigate_line_sequence = fields.Integer(
|
||||
string='Max Navigate sequence in lines',
|
||||
compute='_compute_max_navigate_line',
|
||||
store=True
|
||||
)
|
||||
|
||||
max_navigate_line_model = fields.Char(
|
||||
string="Max Navigate Model in lines",
|
||||
compute="_compute_max_navigate_line",
|
||||
store=True)
|
||||
|
||||
@api.depends("navigate_line_ids", "model_id")
|
||||
def _compute_max_navigate_line(self):
|
||||
"""Allow to know the highest sequence entered in navigate lines.
|
||||
Then we add 1 to this value for the next sequence.
|
||||
This value is given to the context of the o2m field in the view.
|
||||
So when we create new navigate line, the sequence is automatically
|
||||
added as : max_sequence + 1
|
||||
"""
|
||||
for action in self:
|
||||
action.max_navigate_line_sequence = (
|
||||
max(action.mapped('navigate_line_ids.sequence') or [0]) + 1)
|
||||
action.max_navigate_line_model =\
|
||||
action.navigate_line_ids\
|
||||
and action.navigate_line_ids[-1].field_model\
|
||||
or action.model_id.model
|
||||
|
||||
def delete_last_line(self):
|
||||
self.ensure_one()
|
||||
self.navigate_line_ids[-1].unlink()
|
||||
self.navigate_action_id = False
|
||||
|
||||
@api.model
|
||||
def run_action_navigate_multi(self, action, eval_context=None):
|
||||
IrModel = self.env['ir.model']
|
||||
lines = action.navigate_line_ids
|
||||
if not lines:
|
||||
raise UserError(_(
|
||||
"The Action Server %s is not correctly set\n"
|
||||
" : No fields defined"))
|
||||
mapped_field_value = ".".join(lines.mapped("field_id.name"))
|
||||
|
||||
item_ids = eval_context['records'].mapped(mapped_field_value).ids
|
||||
domain = "[('id','in',[" + ','.join(map(str, item_ids)) + "])]"
|
||||
|
||||
# Use Defined action if defined
|
||||
if action.navigate_action_id:
|
||||
return_action = action.navigate_action_id
|
||||
result = return_action.read()[0]
|
||||
result['domain'] = domain
|
||||
else:
|
||||
# Otherwise, return a default action
|
||||
model_name = action.max_navigate_line_model
|
||||
model = IrModel.search([('model', '=', model_name)])
|
||||
view_mode = 'tree,form'
|
||||
result = {
|
||||
'name': model.name,
|
||||
'domain': domain,
|
||||
'res_model': model_name,
|
||||
'target': 'current',
|
||||
'type': 'ir.actions.act_window',
|
||||
'view_mode': view_mode,
|
||||
}
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,44 @@
|
||||
# Copyright (C) 2020 - Today: GRAP (http://www.grap.coop)
|
||||
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class IrActionsServerNavigateLine(models.Model):
|
||||
_name = "ir.actions.server.navigate.line"
|
||||
_description = "Server Actions Navigation Lines"
|
||||
_order = "sequence"
|
||||
|
||||
sequence = fields.Integer(string="Sequence", default=1)
|
||||
|
||||
field_model = fields.Char(
|
||||
string="Model", related="field_id.relation", store=True)
|
||||
|
||||
action_id = fields.Many2one(
|
||||
comodel_name="ir.actions.server", string="Action",
|
||||
required=True, ondelete="cascade")
|
||||
|
||||
field_id = fields.Many2one(
|
||||
comodel_name="ir.model.fields", string="Field",
|
||||
required=True)
|
||||
|
||||
# when adding a record, onchange is called for every field on the
|
||||
# form, also in editable list views
|
||||
@api.onchange('field_id')
|
||||
def _onchange_field_id(self):
|
||||
# check out the docstring of this in odoo/models.py
|
||||
lines = self.action_id.resolve_2many_commands(
|
||||
'navigate_line_ids',
|
||||
self.env.context.get('navigate_line_ids', []),
|
||||
)
|
||||
lines = sum(map(self.new, lines), self.browse([]))
|
||||
model = lines[-1:].field_id.relation or self.action_id.model_id.model
|
||||
return {
|
||||
'domain': {
|
||||
'field_id': [
|
||||
('ttype', 'in', ['many2one', 'one2many', 'many2many']),
|
||||
('model', '=', model),
|
||||
],
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user