[FIX] board: filter available dashboards
[odoo/odoo.git] / addons / board / board.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #    Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (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 Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from operator import itemgetter
24 from textwrap import dedent
25
26 from openerp import tools
27 from openerp.osv import fields, osv
28
29 class board_board(osv.osv):
30     _name = 'board.board'
31     _description = "Board"
32     _auto = False
33     _columns = {}
34
35     @tools.cache()
36     def list(self, cr, uid, context=None):
37         Actions = self.pool.get('ir.actions.act_window')
38         Menus = self.pool.get('ir.ui.menu')
39         IrValues = self.pool.get('ir.values')
40
41         act_ids = Actions.search(cr, uid, [('res_model', '=', self._name)], context=context)
42         refs = ['%s,%s' % (Actions._name, act_id) for act_id in act_ids]
43
44         # cannot search "action" field on menu (non stored function field without search_fnct)
45         irv_ids = IrValues.search(cr, uid, [
46             ('model', '=', 'ir.ui.menu'),
47             ('key', '=', 'action'),
48             ('key2', '=', 'tree_but_open'),
49             ('value', 'in', refs),
50         ], context=context)
51         menu_ids = map(itemgetter('res_id'), IrValues.read(cr, uid, irv_ids, ['res_id'], context=context))
52         menu_ids = Menus._filter_visible_menus(cr, uid, menu_ids, context=context)
53         menu_names = Menus.name_get(cr, uid, menu_ids, context=context)
54         return [dict(id=m[0], name=m[1]) for m in menu_names]
55
56     def _clear_list_cache(self):
57         self.list.clear_cache(self)
58
59     def create(self, cr, user, vals, context=None):
60         return 0
61
62     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
63         """
64         Overrides orm field_view_get.
65         @return: Dictionary of Fields, arch and toolbar.
66         """
67
68         res = {}
69         res = super(board_board, self).fields_view_get(cr, user, view_id, view_type,
70                                                        context, toolbar=toolbar, submenu=submenu)
71
72         CustView = self.pool.get('ir.ui.view.custom')
73         vids = CustView.search(cr, user, [('user_id', '=', user), ('ref_id', '=', view_id)], context=context)
74         if vids:
75             view_id = vids[0]
76             arch = CustView.browse(cr, user, view_id, context=context)
77             res['custom_view_id'] = view_id
78             res['arch'] = arch.arch
79         res['arch'] = self._arch_preprocessing(cr, user, res['arch'], context=context)
80         res['toolbar'] = {'print': [], 'action': [], 'relate': []}
81         return res
82
83     def _arch_preprocessing(self, cr, user, arch, context=None):
84         from lxml import etree
85         def remove_unauthorized_children(node):
86             for child in node.iterchildren():
87                 if child.tag == 'action' and child.get('invisible'):
88                     node.remove(child)
89                 else:
90                     child = remove_unauthorized_children(child)
91             return node
92
93         def encode(s):
94             if isinstance(s, unicode):
95                 return s.encode('utf8')
96             return s
97
98         archnode = etree.fromstring(encode(arch))
99         return etree.tostring(remove_unauthorized_children(archnode), pretty_print=True)
100
101
102 class board_create(osv.osv_memory):
103
104     def board_create(self, cr, uid, ids, context=None):
105         assert len(ids) == 1
106         this = self.browse(cr, uid, ids[0], context=context)
107
108         view_arch = dedent("""<?xml version="1.0"?>
109             <form string="%s" version="7.0">
110             <board style="2-1">
111                 <column/>
112                 <column/>
113             </board>
114             </form>
115         """.strip() % (this.name,))
116
117         view_id = self.pool.get('ir.ui.view').create(cr, uid, {
118             'name': this.name,
119             'model': 'board.board',
120             'priority': 16,
121             'type': 'form',
122             'arch': view_arch,
123         }, context=context)
124
125         action_id = self.pool.get('ir.actions.act_window').create(cr, uid, {
126             'name': this.name,
127             'view_type': 'form',
128             'view_mode': 'form',
129             'res_model': 'board.board',
130             'usage': 'menu',
131             'view_id': view_id,
132             'help': dedent('''<div class="oe_empty_custom_dashboard">
133               <p>
134                 <b>This dashboard is empty.</b>
135               </p><p>
136                 To add the first report into this dashboard, go to any
137                 menu, switch to list or graph view, and click <i>'Add to
138                 Dashboard'</i> in the extended search options.
139               </p><p>
140                 You can filter and group data before inserting into the
141                 dashboard using the search options.
142               </p>
143           </div>
144             ''')
145         }, context=context)
146
147         menu_id = self.pool.get('ir.ui.menu').create(cr, uid, {
148             'name': this.name,
149             'parent_id': this.menu_parent_id.id,
150             'action': 'ir.actions.act_window,%s' % (action_id,)
151         }, context=context)
152
153         self.pool.get('board.board')._clear_list_cache()
154
155         return {
156             'type': 'ir.actions.client',
157             'tag': 'reload',
158             'params': {
159                 'menu_id': menu_id
160             },
161         }
162
163     def _default_menu_parent_id(self, cr, uid, context=None):
164         _, menu_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'menu_reporting_dashboard')
165         return menu_id
166
167     _name = "board.create"
168     _description = "Board Creation"
169
170     _columns = {
171         'name': fields.char('Board Name', size=64, required=True),
172         'menu_parent_id': fields.many2one('ir.ui.menu', 'Parent Menu', required=True),
173     }
174
175     _defaults = {
176         'menu_parent_id': _default_menu_parent_id,
177     }
178
179 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: