Set of label improvements to Open ERP Server.
[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-2009 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     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)
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', 'Original 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('Child Field',size=64),
66     }
67     _defaults = {
68         'arch': lambda *a: '<?xml version="1.0"?>\n<tree string="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 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)
84            try:
85                if obj.model==child._inherit:
86                 pass
87            except:
88                if not obj.model==vals['model']:
89                 raise Exception(error)
90
91        return super(view,self).create(cr, uid, vals, context={})
92
93     def read(self, cr, uid, ids, fields=None, context={}, load='_classic_read'):
94
95         if not isinstance(ids, (list, tuple)):
96             ids = [ids]
97
98         result = super(view, self).read(cr, uid, ids, fields, context, load)
99
100         for rs in result:
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()
104                 if oview:
105                     rs['arch'] = oview[0]['arch']
106
107
108         return result
109
110     def write(self, cr, uid, ids, vals, context={}):
111
112         if not isinstance(ids, (list, tuple)):
113             ids = [ids]
114
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')}
119
120             # write fields except arch to the `ir.ui.view`
121             result = super(view, self).write(cr, uid, ids, vals, context)
122
123             if not vids:
124                 self.pool.get('ir.ui.view.custom').create(cr, uid, vals2)
125             else:
126                 self.pool.get('ir.ui.view.custom').write(cr, uid, vids, vals2)
127
128             return result
129
130         return super(view, self).write(cr, uid, ids, vals, context)
131 view()
132
133 class view_sc(osv.osv):
134     _name = 'ir.ui.view_sc'
135     _columns = {
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)
141     }
142
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)
146
147     _order = 'sequence'
148     _defaults = {
149         'resource': lambda *a: 'ir.ui.menu',
150         'user_id': lambda obj, cr, uid, context: uid,
151     }
152 view_sc()
153
154 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
155