[REF] removes useless comments (addon web_graph)
[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-2012 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_names = Menus.name_get(cr, uid, menu_ids, context=context)
53         return [dict(id=m[0], name=m[1]) for m in menu_names]
54
55     def _clear_list_cache(self):
56         self.list.clear_cache(self)
57
58     def create(self, cr, user, vals, context=None):
59         return 0
60
61     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
62         """
63         Overrides orm field_view_get.
64         @return: Dictionary of Fields, arch and toolbar.
65         """
66
67         res = {}
68         res = super(board_board, self).fields_view_get(cr, user, view_id, view_type,
69                                                        context, toolbar=toolbar, submenu=submenu)
70
71         CustView = self.pool.get('ir.ui.view.custom')
72         vids = CustView.search(cr, user, [('user_id', '=', user), ('ref_id', '=', view_id)], context=context)
73         if vids:
74             view_id = vids[0]
75             arch = CustView.browse(cr, user, view_id, context=context)
76             res['custom_view_id'] = view_id
77             res['arch'] = arch.arch
78         res['arch'] = self._arch_preprocessing(cr, user, res['arch'], context=context)
79         res['toolbar'] = {'print': [], 'action': [], 'relate': []}
80         return res
81
82     def _arch_preprocessing(self, cr, user, arch, context=None):
83         from lxml import etree
84         def remove_unauthorized_children(node):
85             for child in node.iterchildren():
86                 if child.tag == 'action' and child.get('invisible'):
87                     node.remove(child)
88                 else:
89                     child = remove_unauthorized_children(child)
90             return node
91
92         def encode(s):
93             if isinstance(s, unicode):
94                 return s.encode('utf8')
95             return s
96
97         archnode = etree.fromstring(encode(arch))
98         return etree.tostring(remove_unauthorized_children(archnode), pretty_print=True)
99
100
101 class board_create(osv.osv_memory):
102
103     def board_create(self, cr, uid, ids, context=None):
104         assert len(ids) == 1
105         this = self.browse(cr, uid, ids[0], context=context)
106
107         view_arch = dedent("""<?xml version="1.0"?>
108             <form string="%s" version="7.0">
109             <board style="2-1">
110                 <column/>
111                 <column/>
112             </board>
113             </form>
114         """.strip() % (this.name,))
115
116         view_id = self.pool.get('ir.ui.view').create(cr, uid, {
117             'name': this.name,
118             'model': 'board.board',
119             'priority': 16,
120             'type': 'form',
121             'arch': view_arch,
122         }, context=context)
123
124         action_id = self.pool.get('ir.actions.act_window').create(cr, uid, {
125             'name': this.name,
126             'view_type': 'form',
127             'view_mode': 'form',
128             'res_model': 'board.board',
129             'usage': 'menu',
130             'view_id': view_id,
131             'help': dedent('''<div class="oe_empty_custom_dashboard">
132               <p>
133                 <b>This dashboard is empty.</b>
134               </p><p>
135                 To add the first report into this dashboard, go to any
136                 menu, switch to list or graph view, and click <i>'Add to
137                 Dashboard'</i> in the extended search options.
138               </p><p>
139                 You can filter and group data before inserting into the
140                 dashboard using the search options.
141               </p>
142           </div>
143             ''')
144         }, context=context)
145
146         menu_id = self.pool.get('ir.ui.menu').create(cr, uid, {
147             'name': this.name,
148             'parent_id': this.menu_parent_id.id,
149             'action': 'ir.actions.act_window,%s' % (action_id,)
150         }, context=context)
151
152         self.pool.get('board.board')._clear_list_cache()
153
154         return {
155             'type': 'ir.actions.client',
156             'tag': 'reload',
157             'params': {
158                 'menu_id': menu_id
159             },
160         }
161
162     def _default_menu_parent_id(self, cr, uid, context=None):
163         _, menu_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'menu_reporting_dashboard')
164         return menu_id
165
166     _name = "board.create"
167     _description = "Board Creation"
168
169     _columns = {
170         'name': fields.char('Board Name', size=64, required=True),
171         'menu_parent_id': fields.many2one('ir.ui.menu', 'Parent Menu', required=True),
172     }
173
174     _defaults = {
175         'menu_parent_id': _default_menu_parent_id,
176     }
177
178 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: