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.

127 lines
4.5 KiB

  1. # coding: utf-8
  2. # © 2015 David BEAL @ Akretion <david.beal@akretion.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import _, api, models, fields
  5. class ErpHelp(models.AbstractModel):
  6. _name = 'erp.help'
  7. enduser_help = fields.Html(
  8. string="End User Help",
  9. help="Use this field to add custom content for documentation purpose\n"
  10. "mainly by power users ")
  11. advanced_help = fields.Text(
  12. string="Advanced Help", groups='base.group_no_one',
  13. help="Use this field to add custom content for documentation purpose\n"
  14. "mainly by developers or consultants")
  15. class IrModel(models.Model):
  16. _inherit = ['ir.model', 'erp.help']
  17. _name = 'ir.model'
  18. class IrActionsActwindow(models.Model):
  19. _inherit = ['ir.actions.act_window', 'erp.help']
  20. _name = 'ir.actions.act_window'
  21. _rpt_menu = False
  22. enduser_help_model = fields.Html(
  23. string='Enduser Help from Model', store="True",
  24. compute='_compute_model_help', inverse='_inverse_model_help',
  25. help="")
  26. advanced_help_model = fields.Text(
  27. string='Advanced Help from model', store="True",
  28. compute='_compute_model_help', inverse='_inverse_model_help',
  29. help="")
  30. action_help = fields.Boolean(string="Display Action Help")
  31. help_has_content = fields.Boolean(
  32. string="Content in help", compute='_compute_contains_help',
  33. help="One of the help has content")
  34. @api.one
  35. @api.depends('enduser_help', 'advanced_help',
  36. 'enduser_help_model', 'advanced_help_model')
  37. def _compute_contains_help(self):
  38. if (self.enduser_help or self.enduser_help_model or
  39. self.advanced_help or self.advanced_help_model):
  40. self.help_has_content = True
  41. else:
  42. self.help_has_content = False
  43. @api.multi
  44. def _compute_model_help(self):
  45. for rec in self:
  46. model = rec.env['ir.model'].search([('model', '=', rec.res_model)])
  47. rec.enduser_help_model = model.enduser_help
  48. rec.advanced_help_model = model.advanced_help
  49. def _inverse_model_help(self):
  50. for rec in self:
  51. model = rec.env['ir.model'].search([('model', '=', rec.res_model)])
  52. model.enduser_help = rec.enduser_help_model
  53. model.advanced_help = rec.advanced_help_model
  54. @api.multi
  55. def open_help_popup(self):
  56. """ Open in a new tab instead of in popup"""
  57. self.ensure_one()
  58. return {
  59. 'name': _('Open help for this action'),
  60. 'type': 'ir.actions.act_url',
  61. 'url': 'report/html/help_popup.tpl_help/%s' % self.id,
  62. 'target': 'new',
  63. }
  64. @api.model
  65. def get_help_actions(self):
  66. """ called by the template"""
  67. self._rpt_menu = self.get_main_menu()
  68. menu_names = self.get_menu_names(self._rpt_menu)
  69. actions = self.search([
  70. ('id', 'in', menu_names.keys()),
  71. '|', '|', '|',
  72. ('enduser_help', '!=', False),
  73. ('enduser_help_model', '!=', False),
  74. ('advanced_help', '!=', False),
  75. ('advanced_help_model', '!=', False),
  76. ])
  77. return actions
  78. @api.model
  79. def get_main_menu(self):
  80. self._rpt_menu = False
  81. ir_vals = self.env['ir.values'].search([
  82. ('key2', '=', 'tree_but_open'),
  83. ('key', '=', 'action'),
  84. ('res_id', '>', 0),
  85. ('value', '=', 'ir.actions.act_window,%s' % self.id),
  86. ])
  87. if ir_vals:
  88. # we only keep the first menu beacause we have no info on menu_id
  89. self._rpt_menu = self.env['ir.ui.menu'].browse(ir_vals[0].res_id)
  90. while self._rpt_menu.parent_id:
  91. self._rpt_menu = self._rpt_menu.parent_id
  92. return self._rpt_menu
  93. @api.model
  94. def get_menu_names(self, main_menu):
  95. """ @return dict {action_id: 'menu name'} """
  96. menus = self.env['ir.ui.menu'].search(
  97. [('id', 'child_of', main_menu.id)])
  98. ir_vals = self.env['ir.values'].search([
  99. ('key2', '=', 'tree_but_open'),
  100. ('key', '=', 'action'),
  101. ('res_id', 'in', menus.ids),
  102. ('value', 'like', 'ir.actions.act_window,%'),
  103. ])
  104. map_menu = {x.id: x.name for x in menus}
  105. return {int(x.value[22:]): map_menu[x.res_id] for x in ir_vals}
  106. def _anchorize(self, string):
  107. """ called by template """
  108. for char in ["'", '"', ' ']:
  109. string = string.replace(char, '-')
  110. return string