passing in GPL-3
[odoo/odoo.git] / bin / addons / base / ir / ir_ui_view.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 from osv import fields,osv
24 from lxml import etree
25 import tools
26 import netsvc
27 import os
28
29 def _check_xml(self, cr, uid, ids, context={}):
30     return True
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)
39             return False
40     return True
41
42 class view_custom(osv.osv):
43     _name = 'ir.ui.view.custom'
44     _columns = {
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),
48     }
49 view_custom()
50
51 class view(osv.osv):
52     _name = 'ir.ui.view'
53     _columns = {
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((
58             ('tree','Tree'),
59             ('form','Form'),
60             ('graph', 'Graph'),
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),
66     }
67     _defaults = {
68         'arch': lambda *a: '<?xml version="1.0"?>\n<tree title="Unknwown">\n\t<field name="name"/>\n</tree>',
69         'priority': lambda *a: 16
70     }
71     _order = "priority"
72     _constraints = [
73         (_check_xml, 'Invalid XML for View Architecture!', ['arch'])
74     ]
75
76     def read(self, cr, uid, ids, fields=None, context={}, load='_classic_read'):
77
78         if not isinstance(ids, (list, tuple)):
79             ids = [ids]
80
81         result = super(view, self).read(cr, uid, ids, fields, context, load)
82
83         for rs in result:
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()
87                 if oview:
88                     rs['arch'] = oview[0]['arch']
89
90
91         return result
92
93     def write(self, cr, uid, ids, vals, context={}):
94
95         if not isinstance(ids, (list, tuple)):
96             ids = [ids]
97
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')}
102
103             # write fields except arch to the `ir.ui.view`
104             result = super(view, self).write(cr, uid, ids, vals, context)
105
106             if not vids:
107                 self.pool.get('ir.ui.view.custom').create(cr, uid, vals2)
108             else:
109                 self.pool.get('ir.ui.view.custom').write(cr, uid, vids, vals2)
110
111             return result
112
113         return super(view, self).write(cr, uid, ids, vals, context)
114 view()
115
116 class view_sc(osv.osv):
117     _name = 'ir.ui.view_sc'
118     _columns = {
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)
124     }
125
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)
129
130     _order = 'sequence'
131     _defaults = {
132         'resource': lambda *a: 'ir.ui.menu',
133         'user_id': lambda obj, cr, uid, context: uid,
134     }
135 view_sc()
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
138