1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 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={}):
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, 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((
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('Child Field',size=64),
68 'arch': lambda *a: '<?xml version="1.0"?>\n<tree string="Unknwown">\n\t<field name="name"/>\n</tree>',
69 'priority': lambda *a: 16
73 (_check_xml, 'Invalid XML for View Architecture!', ['arch'])
76 def create(self, cr, uid, vals, context={}):
77 if 'inherit_id' in vals and vals['inherit_id']:
78 obj=self.browse(cr,uid,vals['inherit_id'])
79 child=self.pool.get(vals['model'])
80 error="Inherited view model [%s] and \
81 \n\n base view model [%s] do not match \
82 \n\n It should be same as base view model " \
83 %(vals['model'],obj.model)
85 if obj.model==child._inherit:
88 if not obj.model==vals['model']:
89 raise Exception(error)
91 return super(view,self).create(cr, uid, vals, context={})
93 def read(self, cr, uid, ids, fields=None, context={}, load='_classic_read'):
95 if not isinstance(ids, (list, tuple)):
98 result = super(view, self).read(cr, uid, ids, fields, context, load)
101 if rs.get('model') == 'board.board':
102 cr.execute("select id,arch,ref_id from ir_ui_view_custom where user_id=%s and ref_id=%s", (uid, rs['id']))
103 oview = cr.dictfetchall()
105 rs['arch'] = oview[0]['arch']
110 def write(self, cr, uid, ids, vals, context={}):
112 if not isinstance(ids, (list, tuple)):
115 exist = self.pool.get('ir.ui.view').browse(cr, uid, ids[0])
116 if exist.model == 'board.board' and 'arch' in vals:
117 vids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('user_id','=',uid), ('ref_id','=',ids[0])])
118 vals2 = {'user_id': uid, 'ref_id': ids[0], 'arch': vals.pop('arch')}
120 # write fields except arch to the `ir.ui.view`
121 result = super(view, self).write(cr, uid, ids, vals, context)
124 self.pool.get('ir.ui.view.custom').create(cr, uid, vals2)
126 self.pool.get('ir.ui.view.custom').write(cr, uid, vids, vals2)
130 return super(view, self).write(cr, uid, ids, vals, context)
133 class view_sc(osv.osv):
134 _name = 'ir.ui.view_sc'
136 'name': fields.char('Shortcut Name', size=64, required=True),
137 'res_id': fields.many2one('ir.ui.menu','Resource Ref.', ondelete='cascade'),
138 'sequence': fields.integer('Sequence'),
139 'user_id': fields.many2one('res.users', 'User Ref.', required=True, ondelete='cascade'),
140 'resource': fields.char('Resource Name', size=64, required=True)
143 def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context={}):
144 ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
145 return self.read(cr, uid, ids, ['res_id','name'], context=context)
149 'resource': lambda *a: 'ir.ui.menu',
150 'user_id': lambda obj, cr, uid, context: uid,
154 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: