You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.4 KiB

  1. # Copyright (C) 2020 - Today: GRAP (http://www.grap.coop)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import UserError
  6. class IrActionsServer(models.Model):
  7. _inherit = "ir.actions.server"
  8. state = fields.Selection(
  9. selection_add=[("navigate", "Navigate")], ondelete={"navigate": "set default"}
  10. )
  11. navigate_action_id = fields.Many2one(
  12. string="Navigation Action",
  13. comodel_name="ir.actions.act_window",
  14. domain="[('res_model', '=', max_navigate_line_model)]",
  15. help="Define here the action used when the navigation will be executed"
  16. " if empty, a generic action will be used.",
  17. )
  18. navigate_line_ids = fields.One2many(
  19. comodel_name="ir.actions.server.navigate.line",
  20. string="Navigate Lines",
  21. inverse_name="action_id",
  22. )
  23. max_navigate_line_sequence = fields.Integer(
  24. string="Max Navigate sequence in lines",
  25. compute="_compute_max_navigate_line",
  26. store=True,
  27. )
  28. max_navigate_line_model = fields.Char(
  29. string="Max Navigate Model in lines",
  30. compute="_compute_max_navigate_line",
  31. store=True,
  32. )
  33. @api.depends("navigate_line_ids", "model_id")
  34. def _compute_max_navigate_line(self):
  35. """Allow to know the highest sequence entered in navigate lines.
  36. Then we add 1 to this value for the next sequence.
  37. This value is given to the context of the o2m field in the view.
  38. So when we create new navigate line, the sequence is automatically
  39. added as : max_sequence + 1
  40. """
  41. for action in self:
  42. action.max_navigate_line_sequence = (
  43. max(action.mapped("navigate_line_ids.sequence") or [0]) + 1
  44. )
  45. action.max_navigate_line_model = (
  46. action.navigate_line_ids
  47. and action.navigate_line_ids[-1].field_model
  48. or action.model_id.model
  49. )
  50. def delete_last_line(self):
  51. self.ensure_one()
  52. self.navigate_line_ids[-1].unlink()
  53. self.navigate_action_id = False
  54. @api.model
  55. def run_action_navigate_multi(self, action, eval_context=None):
  56. IrModel = self.env["ir.model"]
  57. lines = action.navigate_line_ids
  58. if not lines:
  59. raise UserError(
  60. _("The Action Server %s is not correctly set\n" " : No fields defined")
  61. )
  62. mapped_field_value = ".".join(lines.mapped("field_id.name"))
  63. item_ids = eval_context["records"].mapped(mapped_field_value).ids
  64. domain = "[('id','in',[" + ",".join(map(str, item_ids)) + "])]"
  65. # Use Defined action if defined
  66. if action.navigate_action_id:
  67. return_action = action.navigate_action_id
  68. result = return_action.read()[0]
  69. result["domain"] = domain
  70. else:
  71. # Otherwise, return a default action
  72. model_name = action.max_navigate_line_model
  73. model = IrModel.search([("model", "=", model_name)])
  74. view_mode = "tree,form"
  75. result = {
  76. "name": model.name,
  77. "domain": domain,
  78. "res_model": model_name,
  79. "target": "current",
  80. "type": "ir.actions.act_window",
  81. "view_mode": view_mode,
  82. }
  83. return result