Browse Source

[IMP] Improve shortcut model

pull/56/head
Adrien Peiffer 10 years ago
parent
commit
61637c566f
  1. 41
      web_shortcuts/model/view_sc.py
  2. 8
      web_shortcuts/static/src/js/web_shortcuts.js
  3. 2
      web_shortcuts/static/src/xml/web_shortcuts.xml

41
web_shortcuts/model/view_sc.py

@ -29,39 +29,36 @@
from openerp.osv import orm, fields from openerp.osv import orm, fields
class view_sc(orm.Model):
_name = 'ir.ui.view_sc'
class web_shortcut(orm.Model):
_name = 'web.shortcut'
_columns = { _columns = {
'name': fields.char('Shortcut Name', size=64), # Kept for backwards compatibility only - resource name used instead (translatable)
'res_id': fields.integer('Resource Ref.', help="Reference of the target resource, whose model/table depends on the 'Resource Name' field."),
'sequence': fields.integer('Sequence'),
'user_id': fields.many2one('res.users', 'User Ref.', required=True, ondelete='cascade', select=True),
'resource': fields.char('Resource Name', size=64, required=True, select=True)
'name': fields.char('Shortcut Name', size=64),
'menu_id': fields.many2one('ir.ui.menu'),
'user_id': fields.many2one('res.users', 'User Ref.', required=True,
ondelete='cascade', select=True),
} }
def _auto_init(self, cr, context=None):
super(view_sc, self)._auto_init(cr, context)
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_sc_user_id_resource\'')
if not cr.fetchone():
cr.execute('CREATE INDEX ir_ui_view_sc_user_id_resource ON ir_ui_view_sc (user_id, resource)')
def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context=None):
ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
results = self.read(cr, uid, ids, ['res_id'], context=context)
name_map = dict(self.pool.get(model).name_get(cr, uid, [x['res_id'] for x in results], context=context))
def get_user_shortcuts(self, cr, uid, user_id, context=None):
ids = self.search(cr, uid, [('user_id', '=', user_id)],
context=context)
results = self.read(cr, uid, ids, ['menu_id'], context=context)
name_map = dict(self.pool.get('ir.ui.menu')
.name_get(cr, uid, [x['menu_id'][0] for x in results],
context=context))
# Make sure to return only shortcuts pointing to exisintg menu items. # Make sure to return only shortcuts pointing to exisintg menu items.
filtered_results = filter(lambda result: result['res_id'] in name_map, results)
filtered_results = filter(lambda result: result['menu_id'][0] in
name_map, results)
for result in filtered_results: for result in filtered_results:
result.update(name=name_map[result['res_id']])
result.update(name=name_map[result['menu_id'][0]])
return filtered_results return filtered_results
_order = 'sequence,name'
_order = 'name'
_defaults = { _defaults = {
'resource': 'ir.ui.menu',
'user_id': lambda obj, cr, uid, context: uid, 'user_id': lambda obj, cr, uid, context: uid,
} }
_sql_constraints = [ _sql_constraints = [
('shortcut_unique', 'unique(res_id, resource, user_id)', 'Shortcut for this menu already exists!'),
('shortcut_unique', 'unique(menu_id,user_id)',
'Shortcut for this menu already exists!'),
] ]
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

8
web_shortcuts/static/src/js/web_shortcuts.js

@ -33,7 +33,7 @@ instance.web_shortcuts.Shortcuts = instance.web.Widget.extend({
this.on('display', this, this.display); this.on('display', this, this.display);
this.on('remove', this, this.remove); this.on('remove', this, this.remove);
this.on('click', this, this.click); this.on('click', this, this.click);
this.model = new instance.web.Model('ir.ui.view_sc');
this.model = new instance.web.Model('web.shortcut');
}, },
start: function() { start: function() {
var self = this; var self = this;
@ -46,9 +46,8 @@ instance.web_shortcuts.Shortcuts = instance.web.Widget.extend({
load: function() { load: function() {
var self = this; var self = this;
this.$el.find('.oe_systray_shortcuts_items').empty(); this.$el.find('.oe_systray_shortcuts_items').empty();
return this.model.call('get_sc', [
return this.model.call('get_user_shortcuts', [
instance.session.uid, instance.session.uid,
'ir.ui.menu',
instance.web.pyeval.eval('context', {}) instance.web.pyeval.eval('context', {})
]).done(function(shortcuts) { ]).done(function(shortcuts) {
_.each(shortcuts, function(sc) { _.each(shortcuts, function(sc) {
@ -138,8 +137,7 @@ instance.web.ViewManagerAction.include({
} else { } else {
shortcuts_menu.trigger('add', { shortcuts_menu.trigger('add', {
'user_id': self.session.uid, 'user_id': self.session.uid,
'res_id': self.session.active_id,
'resource': 'ir.ui.menu',
'menu_id': self.session.active_id,
'name': self.action.name 'name': self.action.name
}); });
} }

2
web_shortcuts/static/src/xml/web_shortcuts.xml

@ -13,7 +13,7 @@
</t> </t>
<t t-name="Systray.Shortcuts.Item"> <t t-name="Systray.Shortcuts.Item">
<li> <li>
<a href="#" t-att-data-id="shortcut.res_id" t-att-data-shortcut-id="shortcut.id">
<a href="#" t-att-data-id="shortcut.menu_id[0]" t-att-data-shortcut-id="shortcut.id">
<t t-esc="shortcut.name"/> <t t-esc="shortcut.name"/>
</a> </a>
</li> </li>

Loading…
Cancel
Save