2009-10-20 10:52:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
2010-02-02 11:54:22 +00:00
|
|
|
#
|
2008-11-10 10:15:05 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2009-10-14 12:32:15 +00:00
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 12:32:15 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 12:32:15 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2009-10-14 12:32:15 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2010-02-02 11:54:22 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import fields,osv
|
2008-07-01 21:31:46 +00:00
|
|
|
from lxml import etree
|
2010-02-02 11:54:22 +00:00
|
|
|
from tools import graph
|
2010-05-03 23:49:53 +00:00
|
|
|
from tools.safe_eval import safe_eval as eval
|
2008-07-01 21:31:46 +00:00
|
|
|
import tools
|
|
|
|
import os
|
2010-07-27 15:00:51 +00:00
|
|
|
import logging
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2012-01-24 11:47:30 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2008-08-07 10:57:47 +00:00
|
|
|
class view_custom(osv.osv):
|
2008-08-06 09:01:54 +00:00
|
|
|
_name = 'ir.ui.view.custom'
|
2011-06-01 10:52:09 +00:00
|
|
|
_order = 'create_date desc' # search(limit=1) should return the last customization
|
2008-07-22 14:24:36 +00:00
|
|
|
_columns = {
|
2011-07-27 09:54:14 +00:00
|
|
|
'ref_id': fields.many2one('ir.ui.view', 'Original View', select=True, required=True, ondelete='cascade'),
|
|
|
|
'user_id': fields.many2one('res.users', 'User', select=True, required=True, ondelete='cascade'),
|
2008-08-07 10:57:47 +00:00
|
|
|
'arch': fields.text('View Architecture', required=True),
|
2008-07-22 14:24:36 +00:00
|
|
|
}
|
2010-12-10 12:47:16 +00:00
|
|
|
|
|
|
|
def _auto_init(self, cr, context=None):
|
|
|
|
super(view_custom, self)._auto_init(cr, context)
|
|
|
|
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_custom_user_id_ref_id\'')
|
|
|
|
if not cr.fetchone():
|
|
|
|
cr.execute('CREATE INDEX ir_ui_view_custom_user_id_ref_id ON ir_ui_view_custom (user_id, ref_id)')
|
2008-08-07 10:57:47 +00:00
|
|
|
view_custom()
|
2008-08-06 09:01:54 +00:00
|
|
|
|
|
|
|
class view(osv.osv):
|
|
|
|
_name = 'ir.ui.view'
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('View Name',size=64, required=True),
|
2010-12-10 12:47:16 +00:00
|
|
|
'model': fields.char('Object', size=64, required=True, select=True),
|
2010-09-27 07:12:29 +00:00
|
|
|
'priority': fields.integer('Sequence', required=True),
|
2008-08-06 09:01:54 +00:00
|
|
|
'type': fields.selection((
|
|
|
|
('tree','Tree'),
|
|
|
|
('form','Form'),
|
2009-03-08 18:58:44 +00:00
|
|
|
('mdx','mdx'),
|
2008-08-06 09:01:54 +00:00
|
|
|
('graph', 'Graph'),
|
2008-10-10 12:30:23 +00:00
|
|
|
('calendar', 'Calendar'),
|
2010-02-01 09:53:55 +00:00
|
|
|
('diagram','Diagram'),
|
2009-09-17 07:27:12 +00:00
|
|
|
('gantt', 'Gantt'),
|
2011-08-02 09:36:59 +00:00
|
|
|
('kanban', 'Kanban'),
|
2010-12-10 12:47:16 +00:00
|
|
|
('search','Search')), 'View Type', required=True, select=True),
|
2008-08-06 09:01:54 +00:00
|
|
|
'arch': fields.text('View Architecture', required=True),
|
2010-12-10 12:47:16 +00:00
|
|
|
'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='cascade', select=True),
|
2009-01-26 17:40:29 +00:00
|
|
|
'field_parent': fields.char('Child Field',size=64),
|
2011-09-06 07:57:11 +00:00
|
|
|
'xml_id': fields.function(osv.osv.get_xml_id, type='char', size=128, string="External ID",
|
2012-01-04 13:30:27 +00:00
|
|
|
help="ID of the view defined in xml file"),
|
2008-08-06 09:01:54 +00:00
|
|
|
}
|
|
|
|
_defaults = {
|
2010-06-15 06:08:47 +00:00
|
|
|
'arch': '<?xml version="1.0"?>\n<tree string="My view">\n\t<field name="name"/>\n</tree>',
|
2010-05-26 23:04:09 +00:00
|
|
|
'priority': 16
|
2008-08-06 09:01:54 +00:00
|
|
|
}
|
2010-12-10 22:42:58 +00:00
|
|
|
_order = "priority,name"
|
2011-11-13 19:27:42 +00:00
|
|
|
|
|
|
|
def _check_xml(self, cr, uid, ids, context=None):
|
|
|
|
for view in self.browse(cr, uid, ids, context):
|
|
|
|
eview = etree.fromstring(view.arch.encode('utf8'))
|
2012-04-17 09:16:19 +00:00
|
|
|
if eview.get('layout') or eview.get('validate'):
|
|
|
|
continue
|
2011-11-13 19:27:42 +00:00
|
|
|
frng = tools.file_open(os.path.join('base','rng','view.rng'))
|
|
|
|
try:
|
|
|
|
relaxng_doc = etree.parse(frng)
|
|
|
|
relaxng = etree.RelaxNG(relaxng_doc)
|
|
|
|
if not relaxng.validate(eview):
|
|
|
|
for error in relaxng.error_log:
|
2012-01-24 11:47:30 +00:00
|
|
|
_logger.error(tools.ustr(error))
|
2011-11-13 19:27:42 +00:00
|
|
|
return False
|
|
|
|
finally:
|
|
|
|
frng.close()
|
|
|
|
return True
|
|
|
|
|
2008-08-06 09:01:54 +00:00
|
|
|
_constraints = [
|
|
|
|
(_check_xml, 'Invalid XML for View Architecture!', ['arch'])
|
|
|
|
]
|
2008-09-23 05:36:21 +00:00
|
|
|
|
2010-12-10 12:47:16 +00:00
|
|
|
def _auto_init(self, cr, context=None):
|
|
|
|
super(view, self)._auto_init(cr, context)
|
|
|
|
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_model_type_inherit_id\'')
|
|
|
|
if not cr.fetchone():
|
|
|
|
cr.execute('CREATE INDEX ir_ui_view_model_type_inherit_id ON ir_ui_view (model, type, inherit_id)')
|
|
|
|
|
2011-09-19 15:24:34 +00:00
|
|
|
def get_inheriting_views_arch(self, cr, uid, view_id, model, context=None):
|
|
|
|
"""Retrieves the architecture of views that inherit from the given view.
|
2011-09-18 23:59:47 +00:00
|
|
|
|
2011-09-19 15:24:34 +00:00
|
|
|
:param int view_id: id of the view whose inheriting views should be retrieved
|
|
|
|
:param str model: model identifier of the view's related model (for double-checking)
|
|
|
|
:rtype: list of tuples
|
|
|
|
:return: [(view_arch,view_id), ...]
|
2011-09-18 23:59:47 +00:00
|
|
|
"""
|
2011-09-19 15:24:34 +00:00
|
|
|
cr.execute("""SELECT arch, id FROM ir_ui_view WHERE inherit_id=%s AND model=%s
|
|
|
|
ORDER BY priority""",
|
|
|
|
(view_id, model))
|
2011-09-18 23:59:47 +00:00
|
|
|
return cr.fetchall()
|
|
|
|
|
2011-11-07 15:19:49 +00:00
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2008-08-07 10:57:47 +00:00
|
|
|
if not isinstance(ids, (list, tuple)):
|
2008-09-23 05:36:21 +00:00
|
|
|
ids = [ids]
|
2011-01-07 16:03:37 +00:00
|
|
|
result = super(view, self).write(cr, uid, ids, vals, context)
|
2008-08-07 10:57:47 +00:00
|
|
|
|
2011-01-07 16:03:37 +00:00
|
|
|
# drop the corresponding view customizations (used for dashboards for example), otherwise
|
|
|
|
# not all users would see the updated views
|
|
|
|
custom_view_ids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('ref_id','in',ids)])
|
|
|
|
if custom_view_ids:
|
|
|
|
self.pool.get('ir.ui.view.custom').unlink(cr, uid, custom_view_ids)
|
2008-08-07 10:57:47 +00:00
|
|
|
|
2011-01-07 16:03:37 +00:00
|
|
|
return result
|
2010-02-02 11:54:22 +00:00
|
|
|
|
2011-11-07 15:19:49 +00:00
|
|
|
def graph_get(self, cr, uid, id, model, node_obj, conn_obj, src_node, des_node, label, scale, context=None):
|
2010-02-22 10:18:23 +00:00
|
|
|
nodes=[]
|
|
|
|
nodes_name=[]
|
|
|
|
transitions=[]
|
|
|
|
start=[]
|
|
|
|
tres={}
|
2010-04-13 12:57:53 +00:00
|
|
|
labels={}
|
2010-02-22 10:18:23 +00:00
|
|
|
no_ancester=[]
|
2010-02-26 09:53:21 +00:00
|
|
|
blank_nodes = []
|
2010-02-22 10:18:23 +00:00
|
|
|
|
|
|
|
_Model_Obj=self.pool.get(model)
|
|
|
|
_Node_Obj=self.pool.get(node_obj)
|
|
|
|
_Arrow_Obj=self.pool.get(conn_obj)
|
2010-02-05 12:58:52 +00:00
|
|
|
|
|
|
|
for model_key,model_value in _Model_Obj._columns.items():
|
2010-02-05 14:39:02 +00:00
|
|
|
if model_value._type=='one2many':
|
2010-02-05 12:58:52 +00:00
|
|
|
if model_value._obj==node_obj:
|
|
|
|
_Node_Field=model_key
|
2010-02-10 11:45:18 +00:00
|
|
|
_Model_Field=model_value._fields_id
|
2010-02-05 12:58:52 +00:00
|
|
|
flag=False
|
|
|
|
for node_key,node_value in _Node_Obj._columns.items():
|
|
|
|
if node_value._type=='one2many':
|
2010-02-10 10:44:16 +00:00
|
|
|
if node_value._obj==conn_obj:
|
|
|
|
if src_node in _Arrow_Obj._columns and flag:
|
|
|
|
_Source_Field=node_key
|
|
|
|
if des_node in _Arrow_Obj._columns and not flag:
|
|
|
|
_Destination_Field=node_key
|
|
|
|
flag = True
|
2010-02-05 12:58:52 +00:00
|
|
|
|
|
|
|
datas = _Model_Obj.read(cr, uid, id, [],context)
|
|
|
|
for a in _Node_Obj.read(cr,uid,datas[_Node_Field],[]):
|
2010-02-26 08:30:41 +00:00
|
|
|
if a[_Source_Field] or a[_Destination_Field]:
|
|
|
|
nodes_name.append((a['id'],a['name']))
|
|
|
|
nodes.append(a['id'])
|
2010-02-26 09:53:21 +00:00
|
|
|
else:
|
|
|
|
blank_nodes.append({'id': a['id'],'name':a['name']})
|
|
|
|
|
2010-02-10 10:44:16 +00:00
|
|
|
if a.has_key('flow_start') and a['flow_start']:
|
2010-02-05 12:58:52 +00:00
|
|
|
start.append(a['id'])
|
|
|
|
else:
|
|
|
|
if not a[_Source_Field]:
|
|
|
|
no_ancester.append(a['id'])
|
|
|
|
for t in _Arrow_Obj.read(cr,uid, a[_Destination_Field],[]):
|
|
|
|
transitions.append((a['id'], t[des_node][0]))
|
2010-02-18 06:38:58 +00:00
|
|
|
tres[str(t['id'])] = (a['id'],t[des_node][0])
|
2010-04-13 13:18:40 +00:00
|
|
|
label_string = ""
|
2010-04-14 11:59:14 +00:00
|
|
|
if label:
|
|
|
|
for lbl in eval(label):
|
2011-08-03 11:05:59 +00:00
|
|
|
if t.has_key(tools.ustr(lbl)) and tools.ustr(t[lbl])=='False':
|
2010-04-14 11:59:14 +00:00
|
|
|
label_string = label_string + ' '
|
|
|
|
else:
|
2011-08-03 11:05:59 +00:00
|
|
|
label_string = label_string + " " + tools.ustr(t[lbl])
|
2010-04-13 13:18:40 +00:00
|
|
|
labels[str(t['id'])] = (a['id'],label_string)
|
2010-02-05 12:58:52 +00:00
|
|
|
g = graph(nodes, transitions, no_ancester)
|
|
|
|
g.process(start)
|
|
|
|
g.scale(*scale)
|
|
|
|
result = g.result_get()
|
|
|
|
results = {}
|
|
|
|
for node in nodes_name:
|
|
|
|
results[str(node[0])] = result[node[0]]
|
|
|
|
results[str(node[0])]['name'] = node[1]
|
2010-08-15 11:00:28 +00:00
|
|
|
return {'nodes': results,
|
|
|
|
'transitions': tres,
|
|
|
|
'label' : labels,
|
|
|
|
'blank_nodes': blank_nodes,
|
|
|
|
'node_parent_field': _Model_Field,}
|
2006-12-07 13:41:40 +00:00
|
|
|
view()
|
|
|
|
|
|
|
|
class view_sc(osv.osv):
|
2008-07-22 14:24:36 +00:00
|
|
|
_name = 'ir.ui.view_sc'
|
|
|
|
_columns = {
|
2011-01-15 04:50:58 +00:00
|
|
|
'name': fields.char('Shortcut Name', size=64), # Kept for backwards compatibility only - resource name used instead (translatable)
|
2011-01-10 13:55:17 +00:00
|
|
|
'res_id': fields.integer('Resource Ref.', help="Reference of the target resource, whose model/table depends on the 'Resource Name' field."),
|
2008-07-22 14:24:36 +00:00
|
|
|
'sequence': fields.integer('Sequence'),
|
2010-12-10 12:47:16 +00:00
|
|
|
'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)
|
2008-07-22 14:24:36 +00:00
|
|
|
}
|
2008-06-12 10:45:39 +00:00
|
|
|
|
2010-12-10 12:47:16 +00:00
|
|
|
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)')
|
|
|
|
|
2011-01-15 04:50:58 +00:00
|
|
|
def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context=None):
|
2008-07-22 14:24:36 +00:00
|
|
|
ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
|
2011-06-24 05:40:56 +00:00
|
|
|
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))
|
2011-04-06 10:05:07 +00:00
|
|
|
# Make sure to return only shortcuts pointing to exisintg menu items.
|
2011-06-24 05:40:56 +00:00
|
|
|
filtered_results = filter(lambda result: result['res_id'] in name_map, results)
|
|
|
|
for result in filtered_results:
|
|
|
|
result.update(name=name_map[result['res_id']])
|
|
|
|
return filtered_results
|
2007-06-28 18:00:41 +00:00
|
|
|
|
2010-12-10 22:42:58 +00:00
|
|
|
_order = 'sequence,name'
|
2008-07-22 14:24:36 +00:00
|
|
|
_defaults = {
|
|
|
|
'resource': lambda *a: 'ir.ui.menu',
|
|
|
|
'user_id': lambda obj, cr, uid, context: uid,
|
|
|
|
}
|
2010-05-12 11:48:23 +00:00
|
|
|
_sql_constraints = [
|
2010-08-19 09:06:50 +00:00
|
|
|
('shortcut_unique', 'unique(res_id, resource, user_id)', 'Shortcut for this menu already exists!'),
|
2010-05-12 11:48:23 +00:00
|
|
|
]
|
2010-09-27 07:12:29 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
view_sc()
|
2008-07-23 15:01:27 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|