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