* Bug-Fix for the Dashboard
[odoo/odoo.git] / bin / addons / base / ir / ir_ui_view.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 #
29 ##############################################################################
30
31 from osv import fields,osv
32 from lxml import etree
33 import tools
34 import netsvc
35 import os
36
37 def _check_xml(self, cr, uid, ids, context={}):
38     return True
39     for view in self.browse(cr, uid, ids, context):
40         eview = etree.fromstring(view.arch)
41         frng = tools.file_open(os.path.join('base','rng','view.rng'))
42         relaxng = etree.RelaxNG(file=frng)
43         if not relaxng.validate(eview):
44             logger = netsvc.Logger()
45             logger.notifyChannel('init', netsvc.LOG_ERROR, 'The view do not fit the required schema !')
46             logger.notifyChannel('init', netsvc.LOG_ERROR, relaxng.error_log.last_error)
47             return False
48     return True
49
50 class view_custom(osv.osv):
51     _name = 'ir.ui.view.custom'
52     _columns = {
53         'ref_id': fields.many2one('ir.ui.view', 'Orignal View'),
54         'user_id': fields.many2one('res.users', 'User'),
55         'arch': fields.text('View Architecture', required=True),
56     }
57 view_custom()
58
59 class view(osv.osv):
60     _name = 'ir.ui.view'
61     _columns = {
62         'name': fields.char('View Name',size=64,  required=True),
63         'model': fields.char('Model', size=64, required=True),
64         'priority': fields.integer('Priority', required=True),
65         'type': fields.selection((
66             ('tree','Tree'),
67             ('form','Form'),
68             ('graph', 'Graph'),
69             ('calendar', 'Calendar')), 'View Type', required=True),
70         'arch': fields.text('View Architecture', required=True),
71         'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='cascade'),
72         'field_parent': fields.char('Childs Field',size=64),
73     }
74     _defaults = {
75         'arch': lambda *a: '<?xml version="1.0"?>\n<tree title="Unknwown">\n\t<field name="name"/>\n</tree>',
76         'priority': lambda *a: 16
77     }
78     _order = "priority"
79     _constraints = [
80         (_check_xml, 'Invalid XML for View Architecture!', ['arch'])
81     ]
82     
83     def read(self, cr, uid, ids, fields=None, context={}, load='_classic_read'):
84
85         if not isinstance(ids, (list, tuple)):
86             ids = [ids]
87
88         result = super(view, self).read(cr, uid, ids, fields, context, load)
89         print '************************** : ',result
90         
91         for rs in result:
92             if rs.get('model') == 'board.board':
93                 cr.execute("select id,arch,ref_id from ir_ui_view_custom where user_id=%d and ref_id=%d", (uid, rs['id']))
94                 oview = cr.dictfetchall()
95                 if oview:
96                     rs['arch'] = oview[0]['arch']
97         
98         
99         return result
100     
101     def write(self, cr, uid, ids, vals, context={}):
102
103         if not isinstance(ids, (list, tuple)):
104             ids = [ids]   
105
106         exist = self.pool.get('ir.ui.view').browse(cr, uid, ids[0])
107         if exist.model == 'board.board' and 'arch' in vals:
108             vids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('user_id','=',uid), ('ref_id','=',ids[0])])
109             vals2 = {'user_id': uid, 'ref_id': ids[0], 'arch': vals.pop('arch')}
110
111             # write fields except arch to the `ir.ui.view`
112             result = super(view, self).write(cr, uid, ids, vals, context)
113
114             if not vids:
115                 self.pool.get('ir.ui.view.custom').create(cr, uid, vals2)
116             else:
117                 self.pool.get('ir.ui.view.custom').write(cr, uid, vids, vals2)
118
119             return result
120
121         return super(view, self).write(cr, uid, ids, vals, context)
122 view()
123
124 class view_sc(osv.osv):
125     _name = 'ir.ui.view_sc'
126     _columns = {
127         'name': fields.char('Shortcut Name', size=64, required=True),
128         'res_id': fields.many2one('ir.values','Resource Ref.', ondelete='cascade'),
129         'sequence': fields.integer('Sequence'),
130         'user_id': fields.many2one('res.users', 'User Ref.', required=True, ondelete='cascade'),
131         'resource': fields.char('Resource Name', size=64, required=True)
132     }
133
134     def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context={}):
135         ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
136         return self.read(cr, uid, ids, ['res_id','name'], context=context)
137
138     _order = 'sequence'
139     _defaults = {
140         'resource': lambda *a: 'ir.ui.menu',
141         'user_id': lambda obj, cr, uid, context: uid,
142     }
143 view_sc()
144
145 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
146