~marrakis/openobject-server/python-lib

« back to all changes in this revision

Viewing changes to openobject/addons/base/ir/ir_ui_view.py

  • Committer: Mathieu Leduc-Hamel
  • Date: 2010-02-11 20:36:24 UTC
  • Revision ID: mlhamel@arak4-20100211203624-4331pfj1xb8qtzw8
[IMP] New pythonic way to organise the code. Everything is under the package openobject.server and everything is refering to that. Now needed to clean again setup.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#    
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU Affero General Public License as
9
 
#    published by the Free Software Foundation, either version 3 of the
10
 
#    License, or (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU Affero General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU Affero General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19
 
#
20
 
##############################################################################
21
 
 
22
 
from openobject.osv import fields,osv
23
 
from lxml import etree
24
 
import openobject.tools.misc
25
 
import openobject.logger
26
 
import os
27
 
 
28
 
def _check_xml(self, cr, uid, ids, context={}):
29
 
    for view in self.browse(cr, uid, ids, context):
30
 
        eview = etree.fromstring(view.arch.encode('utf8'))
31
 
        frng = openobject.tools.misc.file_open(os.path.join('base','rng','view.rng'))
32
 
        relaxng_doc = etree.parse(frng)
33
 
        relaxng = etree.RelaxNG(relaxng_doc)
34
 
        if not relaxng.validate(eview):
35
 
            log = openobject.logger.Logger()
36
 
            log.notifyChannel('init', openobject.logger.LOG_ERROR, 'The view does not fit the required schema !')
37
 
            log.notifyChannel('init', openobject.logger.LOG_ERROR, openobject.tools.misc.ustr(relaxng.error_log.last_error))
38
 
            return False
39
 
    return True
40
 
 
41
 
class view_custom(osv.osv):
42
 
    _name = 'ir.ui.view.custom'
43
 
    _columns = {
44
 
        'ref_id': fields.many2one('ir.ui.view', 'Original View'),
45
 
        'user_id': fields.many2one('res.users', 'User'),
46
 
        'arch': fields.text('View Architecture', required=True),
47
 
    }
48
 
view_custom()
49
 
 
50
 
class view(osv.osv):
51
 
    _name = 'ir.ui.view'
52
 
    _columns = {
53
 
        'name': fields.char('View Name',size=64,  required=True),
54
 
        'model': fields.char('Object', size=64, required=True),
55
 
        'priority': fields.integer('Priority', required=True),
56
 
        'type': fields.selection((
57
 
            ('tree','Tree'),
58
 
            ('form','Form'),
59
 
            ('mdx','mdx'),
60
 
            ('graph', 'Graph'),
61
 
            ('calendar', 'Calendar'),
62
 
            ('gantt', 'Gantt'),
63
 
            ('search','Search')), 'View Type', required=True),
64
 
        'arch': fields.text('View Architecture', required=True),
65
 
        'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='cascade'),
66
 
        'field_parent': fields.char('Child Field',size=64),
67
 
    }
68
 
    _defaults = {
69
 
        'arch': lambda *a: '<?xml version="1.0"?>\n<tree string="Unknwown">\n\t<field name="name"/>\n</tree>',
70
 
        'priority': lambda *a: 16
71
 
    }
72
 
    _order = "priority"
73
 
    _constraints = [
74
 
        (_check_xml, 'Invalid XML for View Architecture!', ['arch'])
75
 
    ]
76
 
 
77
 
    def create(self, cr, uid, vals, context={}):
78
 
       if 'inherit_id' in vals and vals['inherit_id']:
79
 
           obj=self.browse(cr,uid,vals['inherit_id'])
80
 
           child=self.pool.get(vals['model'])
81
 
           error="Inherited view model [%s] and \
82
 
                                 \n\n base view model [%s] do not match \
83
 
                                 \n\n It should be same as base view model " \
84
 
                                 %(vals['model'],obj.model)
85
 
           try:
86
 
               if obj.model==child._inherit:
87
 
                pass
88
 
           except:
89
 
               if not obj.model==vals['model']:
90
 
                raise Exception(error)
91
 
 
92
 
       return super(view,self).create(cr, uid, vals, context={})
93
 
 
94
 
    def read(self, cr, uid, ids, fields=None, context={}, load='_classic_read'):
95
 
 
96
 
        if not isinstance(ids, (list, tuple)):
97
 
            ids = [ids]
98
 
 
99
 
        result = super(view, self).read(cr, uid, ids, fields, context, load)
100
 
 
101
 
        for rs in result:
102
 
            if rs.get('model') == 'board.board':
103
 
                cr.execute("select id,arch,ref_id from ir_ui_view_custom where user_id=%s and ref_id=%s", (uid, rs['id']))
104
 
                oview = cr.dictfetchall()
105
 
                if oview:
106
 
                    rs['arch'] = oview[0]['arch']
107
 
 
108
 
 
109
 
        return result
110
 
 
111
 
    def write(self, cr, uid, ids, vals, context={}):
112
 
 
113
 
        if not isinstance(ids, (list, tuple)):
114
 
            ids = [ids]
115
 
 
116
 
        exist = self.pool.get('ir.ui.view').browse(cr, uid, ids[0])
117
 
        if exist.model == 'board.board' and 'arch' in vals:
118
 
            vids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('user_id','=',uid), ('ref_id','=',ids[0])])
119
 
            vals2 = {'user_id': uid, 'ref_id': ids[0], 'arch': vals.pop('arch')}
120
 
 
121
 
            # write fields except arch to the `ir.ui.view`
122
 
            result = super(view, self).write(cr, uid, ids, vals, context)
123
 
 
124
 
            if not vids:
125
 
                self.pool.get('ir.ui.view.custom').create(cr, uid, vals2)
126
 
            else:
127
 
                self.pool.get('ir.ui.view.custom').write(cr, uid, vids, vals2)
128
 
 
129
 
            return result
130
 
 
131
 
        return super(view, self).write(cr, uid, ids, vals, context)
132
 
view()
133
 
 
134
 
class view_sc(osv.osv):
135
 
    _name = 'ir.ui.view_sc'
136
 
    _columns = {
137
 
        'name': fields.char('Shortcut Name', size=64, required=True),
138
 
        'res_id': fields.many2one('ir.ui.menu','Resource Ref.', ondelete='cascade'),
139
 
        'sequence': fields.integer('Sequence'),
140
 
        'user_id': fields.many2one('res.users', 'User Ref.', required=True, ondelete='cascade'),
141
 
        'resource': fields.char('Resource Name', size=64, required=True)
142
 
    }
143
 
 
144
 
    def get_sc(self, cr, uid, user_id, model='ir.ui.menu', context={}):
145
 
        ids = self.search(cr, uid, [('user_id','=',user_id),('resource','=',model)], context=context)
146
 
        return self.read(cr, uid, ids, ['res_id','name'], context=context)
147
 
 
148
 
    _order = 'sequence'
149
 
    _defaults = {
150
 
        'resource': lambda *a: 'ir.ui.menu',
151
 
        'user_id': lambda obj, cr, uid, context: uid,
152
 
    }
153
 
view_sc()
154
 
 
155
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
156