1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields,osv
23 from lxml import etree
24 from tools import graph
29 def _check_xml(self, cr, uid, ids, context={}):
30 for view in self.browse(cr, uid, ids, context):
31 eview = etree.fromstring(view.arch.encode('utf8'))
32 frng = tools.file_open(os.path.join('base','rng','view.rng'))
33 relaxng_doc = etree.parse(frng)
34 relaxng = etree.RelaxNG(relaxng_doc)
35 if not relaxng.validate(eview):
36 logger = netsvc.Logger()
37 logger.notifyChannel('init', netsvc.LOG_ERROR, 'The view does not fit the required schema !')
38 logger.notifyChannel('init', netsvc.LOG_ERROR, tools.ustr(relaxng.error_log.last_error))
42 class view_custom(osv.osv):
43 _name = 'ir.ui.view.custom'
45 'ref_id': fields.many2one('ir.ui.view', 'Original View'),
46 'user_id': fields.many2one('res.users', 'User'),
47 'arch': fields.text('View Architecture', required=True),
54 'name': fields.char('View Name',size=64, required=True),
55 'model': fields.char('Object', size=64, required=True),
56 'priority': fields.integer('Priority', required=True),
57 'type': fields.selection((
62 ('calendar', 'Calendar'),
63 ('diagram','Diagram'),
65 ('search','Search')), 'View Type', required=True),
66 'arch': fields.text('View Architecture', required=True),
67 'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='cascade'),
68 'field_parent': fields.char('Child Field',size=64),
69 'xml_id': fields.function(osv.osv.get_xml_id, type='char', size=128, string="XML ID",
73 'arch': '<?xml version="1.0"?>\n<tree string="Unknwown">\n\t<field name="name"/>\n</tree>',
78 (_check_xml, 'Invalid XML for View Architecture!', ['arch'])
81 def read(self, cr, uid, ids, fields=None, context={}, load='_classic_read'):
83 if not isinstance(ids, (list, tuple)):
86 result = super(view, self).read(cr, uid, ids, fields, context, load)
89 if rs.get('model') == 'board.board':
90 cr.execute("select id,arch,ref_id from ir_ui_view_custom where user_id=%s and ref_id=%s", (uid, rs['id']))
91 oview = cr.dictfetchall()
93 rs['arch'] = oview[0]['arch']
98 def write(self, cr, uid, ids, vals, context={}):
100 if not isinstance(ids, (list, tuple)):
103 exist = self.pool.get('ir.ui.view').browse(cr, uid, ids[0])
104 if exist.model == 'board.board' and 'arch' in vals:
105 vids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('user_id','=',uid), ('ref_id','=',ids[0])])
106 vals2 = {'user_id': uid, 'ref_id': ids[0], 'arch': vals.pop('arch')}
108 # write fields except arch to the `ir.ui.view`
109 result = super(view, self).write(cr, uid, ids, vals, context)
112 self.pool.get('ir.ui.view.custom').create(cr, uid, vals2)
114 self.pool.get('ir.ui.view.custom').write(cr, uid, vids, vals2)
118 return super(view, self).write(cr, uid, ids, vals, context)
120 def graph_get(self, cr, uid, id, model, node_obj, conn_obj, src_node, des_node,label,scale,context={}):
132 _Model_Obj=self.pool.get(model)
133 _Node_Obj=self.pool.get(node_obj)
134 _Arrow_Obj=self.pool.get(conn_obj)
136 for model_key,model_value in _Model_Obj._columns.items():
137 if model_value._type=='one2many':
138 if model_value._obj==node_obj:
139 _Node_Field=model_key
140 _Model_Field=model_value._fields_id
142 for node_key,node_value in _Node_Obj._columns.items():
143 if node_value._type=='one2many':
144 if node_value._obj==conn_obj:
145 if src_node in _Arrow_Obj._columns and flag:
146 _Source_Field=node_key
147 if des_node in _Arrow_Obj._columns and not flag:
148 _Destination_Field=node_key
151 datas = _Model_Obj.read(cr, uid, id, [],context)
152 for a in _Node_Obj.read(cr,uid,datas[_Node_Field],[]):
153 if a[_Source_Field] or a[_Destination_Field]:
154 nodes_name.append((a['id'],a['name']))
155 nodes.append(a['id'])
157 blank_nodes.append({'id': a['id'],'name':a['name']})
159 if a.has_key('flow_start') and a['flow_start']:
160 start.append(a['id'])
162 if not a[_Source_Field]:
163 no_ancester.append(a['id'])
164 for t in _Arrow_Obj.read(cr,uid, a[_Destination_Field],[]):
165 transitions.append((a['id'], t[des_node][0]))
166 tres[str(t['id'])] = (a['id'],t[des_node][0])
169 for lbl in eval(label):
170 if t.has_key(str(lbl)) and str(t[lbl])=='False':
171 label_string = label_string + ' '
173 label_string = label_string + " " + t[lbl]
174 labels[str(t['id'])] = (a['id'],label_string)
175 g = graph(nodes, transitions, no_ancester)
178 result = g.result_get()
180 for node in nodes_name:
181 results[str(node[0])] = result[node[0]]
182 results[str(node[0])]['name'] = node[1]
183 return {'nodes': results, 'transitions': tres, 'label' : labels, 'blank_nodes': blank_nodes}
186 class view_sc(osv.osv):
187 _name = 'ir.ui.view_sc'
189 'name': fields.char('Shortcut Name', size=64, required=True),
190 'res_id': fields.many2one('ir.ui.menu','Resource Ref.', ondelete='cascade'),
191 'sequence': fields.integer('Sequence'),
192 'user_id': fields.many2one('res.users', 'User Ref.', required=True, ondelete='cascade'),
193 'resource': fields.char('Resource Name', size=64, required=True)
196 def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context={}):
197 ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
198 return self.read(cr, uid, ids, ['res_id','name'], context=context)
202 'resource': lambda *a: 'ir.ui.menu',
203 'user_id': lambda obj, cr, uid, context: uid,
206 ('shortcut_unique', 'unique(res_id, user_id)', 'Shortcut for this menu already exists!'),
211 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: