1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from osv import fields,osv
24 from lxml import etree
29 def _check_xml(self, cr, uid, ids, context={}):
31 for view in self.browse(cr, uid, ids, context):
32 eview = etree.fromstring(view.arch)
33 frng = tools.file_open(os.path.join('base','rng','view.rng'))
34 relaxng = etree.RelaxNG(file=frng)
35 if not relaxng.validate(eview):
36 logger = netsvc.Logger()
37 logger.notifyChannel('init', netsvc.LOG_ERROR, 'The view do not fit the required schema !')
38 logger.notifyChannel('init', netsvc.LOG_ERROR, 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', 'Orignal 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((
61 ('calendar', 'Calendar'),
62 ('gantt', 'Gantt')), 'View Type', required=True),
63 'arch': fields.text('View Architecture', required=True),
64 'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='cascade'),
65 'field_parent': fields.char('Childs Field',size=64),
68 'arch': lambda *a: '<?xml version="1.0"?>\n<tree title="Unknwown">\n\t<field name="name"/>\n</tree>',
69 'priority': lambda *a: 16
73 (_check_xml, 'Invalid XML for View Architecture!', ['arch'])
76 def read(self, cr, uid, ids, fields=None, context={}, load='_classic_read'):
78 if not isinstance(ids, (list, tuple)):
81 result = super(view, self).read(cr, uid, ids, fields, context, load)
84 if rs.get('model') == 'board.board':
85 cr.execute("select id,arch,ref_id from ir_ui_view_custom where user_id=%d and ref_id=%d", (uid, rs['id']))
86 oview = cr.dictfetchall()
88 rs['arch'] = oview[0]['arch']
93 def write(self, cr, uid, ids, vals, context={}):
95 if not isinstance(ids, (list, tuple)):
98 exist = self.pool.get('ir.ui.view').browse(cr, uid, ids[0])
99 if exist.model == 'board.board' and 'arch' in vals:
100 vids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('user_id','=',uid), ('ref_id','=',ids[0])])
101 vals2 = {'user_id': uid, 'ref_id': ids[0], 'arch': vals.pop('arch')}
103 # write fields except arch to the `ir.ui.view`
104 result = super(view, self).write(cr, uid, ids, vals, context)
107 self.pool.get('ir.ui.view.custom').create(cr, uid, vals2)
109 self.pool.get('ir.ui.view.custom').write(cr, uid, vids, vals2)
113 return super(view, self).write(cr, uid, ids, vals, context)
116 class view_sc(osv.osv):
117 _name = 'ir.ui.view_sc'
119 'name': fields.char('Shortcut Name', size=64, required=True),
120 'res_id': fields.many2one('ir.ui.menu','Resource Ref.', ondelete='cascade'),
121 'sequence': fields.integer('Sequence'),
122 'user_id': fields.many2one('res.users', 'User Ref.', required=True, ondelete='cascade'),
123 'resource': fields.char('Resource Name', size=64, required=True)
126 def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context={}):
127 ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
128 return self.read(cr, uid, ids, ['res_id','name'], context=context)
132 'resource': lambda *a: 'ir.ui.menu',
133 'user_id': lambda obj, cr, uid, context: uid,
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: