add Copyright and GPL license into the Header of Python files
[odoo/odoo.git] / bin / addons / base / ir / ir_ui_view.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 from osv import fields,osv
31 from xml import dom
32
33 def _check_xml(self, cr, uid, ids):
34         try:
35                 cr.execute('select arch from ir_ui_view where id in ('+','.join(map(str,ids))+')')
36                 for row in cr.fetchall():
37                         doc = dom.minidom.parseString(row[0])
38                 return True
39         except Exception, e:
40                 return False
41
42 class view(osv.osv):
43         _name = 'ir.ui.view'
44         _columns = {
45                 'name': fields.char('View Name',size=64,  required=True),
46                 'model': fields.char('Model', size=64, required=True),
47                 'priority': fields.integer('Priority', required=True),
48                 'type': fields.selection((
49                         ('tree','Tree'),
50                         ('form','Form'),
51                         ('graph', 'Graph'),
52                         ('calendar', 'Calendar')), 'View Type', required=True),
53                 'arch': fields.text('View Architecture', required=True),
54                 'inherit_id': fields.many2one('ir.ui.view', 'Inherited View'),
55                 'field_parent': fields.char('Childs Field',size=64)
56         }
57         _defaults = {
58                 'arch': lambda *a: '<?xml version="1.0"?>\n<tree title="Unknwown">\n\t<field name="name"/>\n</tree>',
59                 'priority': lambda *a: 16
60         }
61         _order = "priority"
62         _constraints = [
63                 (_check_xml, 'Invalid XML for View Architecture!', ['arch'])
64         ]
65
66 view()
67
68 class view_sc(osv.osv):
69         _name = 'ir.ui.view_sc'
70         _columns = {
71                 'name': fields.char('Shortcut Name', size=64, required=True),
72                 'res_id': fields.integer('Resource Ref.', required=True),
73                 'sequence': fields.integer('Sequence'),
74                 'user_id': fields.many2one('res.users', 'User Ref.', required=True, ondelete='cascade'),
75                 'resource': fields.char('Resource Name', size=64, required=True)
76         }
77         def create(self, cr, uid, vals, context=None):
78                 id=self.pool.get('ir.values').search(cr,uid,[('res_id','=',vals['res_id'])])
79                 if len(id):
80                         return super(view_sc, self).create(cr, uid, vals, context=context)
81                 else:
82                         raise osv.except_osv('Note !','You can not add root node as shortcut !')
83
84         def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context={}):
85                 ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
86                 return self.read(cr, uid, ids, ['res_id','name'], context=context)
87
88         _order = 'sequence'
89         _defaults = {
90                 'resource': lambda *a: 'ir.ui.menu',
91         }
92 view_sc()