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.

52 lines
2.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-today OpenERP SA (<http://www.openerp.com>)
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models, fields, api
  22. class web_shortcut(models.Model):
  23. _name = 'web.shortcut'
  24. name = fields.Char('Shortcut Name', size=64)
  25. menu_id = fields.Many2one('ir.ui.menu')
  26. user_id = fields.Many2one('res.users', 'User Ref.', required=True,
  27. ondelete='cascade', select=True,
  28. default=lambda obj, cr, uid, context: uid)
  29. _sql_constraints = [
  30. ('shortcut_unique', 'unique(menu_id,user_id)',
  31. 'Shortcut for this menu already exists!'),
  32. ]
  33. @api.model
  34. def get_user_shortcuts(self, user_id):
  35. shortcuts = self.search([('user_id', '=', user_id)])
  36. results = shortcuts.read(['menu_id'])
  37. ir_ui_menu_obj = self.env['ir.ui.menu']
  38. menus = ir_ui_menu_obj.search([('id', 'in', [x['menu_id'][0]
  39. for x in results])])
  40. name_map = dict(menus.name_get())
  41. # Make sure to return only shortcuts pointing to existing menu items.
  42. filtered_results = filter(lambda result: result['menu_id'][0] in
  43. name_map, results)
  44. for result in filtered_results:
  45. result.update(name=name_map[result['menu_id'][0]])
  46. return filtered_results