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.

67 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. #
  4. # Authors: Adrien Peiffer
  5. # Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu)
  6. # All Rights Reserved
  7. #
  8. # WARNING: This program as such is intended to be used by professional
  9. # programmers who take the whole responsibility of assessing all potential
  10. # consequences resulting from its eventual inadequacies and bugs.
  11. # End users who are looking for a ready-to-use solution with commercial
  12. # guarantees and support are strongly advised to contact a Free Software
  13. # Service Company.
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as
  17. # published by the Free Software Foundation, either version 3 of the
  18. # License, or (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. #
  28. #
  29. from openerp.osv import orm, fields
  30. class view_sc(orm.Model):
  31. _name = 'ir.ui.view_sc'
  32. _columns = {
  33. 'name': fields.char('Shortcut Name', size=64), # Kept for backwards compatibility only - resource name used instead (translatable)
  34. 'res_id': fields.integer('Resource Ref.', help="Reference of the target resource, whose model/table depends on the 'Resource Name' field."),
  35. 'sequence': fields.integer('Sequence'),
  36. 'user_id': fields.many2one('res.users', 'User Ref.', required=True, ondelete='cascade', select=True),
  37. 'resource': fields.char('Resource Name', size=64, required=True, select=True)
  38. }
  39. def _auto_init(self, cr, context=None):
  40. super(view_sc, self)._auto_init(cr, context)
  41. cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_sc_user_id_resource\'')
  42. if not cr.fetchone():
  43. cr.execute('CREATE INDEX ir_ui_view_sc_user_id_resource ON ir_ui_view_sc (user_id, resource)')
  44. def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context=None):
  45. ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
  46. results = self.read(cr, uid, ids, ['res_id'], context=context)
  47. name_map = dict(self.pool.get(model).name_get(cr, uid, [x['res_id'] for x in results], context=context))
  48. # Make sure to return only shortcuts pointing to exisintg menu items.
  49. filtered_results = filter(lambda result: result['res_id'] in name_map, results)
  50. for result in filtered_results:
  51. result.update(name=name_map[result['res_id']])
  52. return filtered_results
  53. _order = 'sequence,name'
  54. _defaults = {
  55. 'resource': 'ir.ui.menu',
  56. 'user_id': lambda obj, cr, uid, context: uid,
  57. }
  58. _sql_constraints = [
  59. ('shortcut_unique', 'unique(res_id, resource, user_id)', 'Shortcut for this menu already exists!'),
  60. ]
  61. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: