[MERGE] lp:~openerp-commiter/openobject-addons/trunk-review-dashboards-fix-aja
[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 #
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 osv import fields, osv
23 import time
24 import tools
25
26 class board_board(osv.osv):
27     """
28     Board
29     """
30     _name = 'board.board'
31     _description = "Board"
32
33     def create_view(self, cr, uid, ids, context=None):
34         """
35         Create  view
36         @param cr: the current row, from the database cursor,
37         @param uid: the current user’s ID for security checks,
38         @param ids: List of Board's IDs
39         @return: arch of xml view.
40         """
41         board = self.pool.get('board.board').browse(cr, uid, ids, context=context)
42         left = []
43         right = []
44         #start Loop
45         for line in board.line_ids:
46             linestr = '<action string="%s" name="%d"' % (line.name, line.action_id.id)
47             linestr += '/>'
48             if line.position == 'left':
49                 left.append(linestr)
50             else:
51                 right.append(linestr)
52         #End Loop
53         arch = """<?xml version="1.0"?>
54             <form string="My Board">
55             <board style="1-1">
56                 <column>
57                     %s
58                 </column>
59                 <column>
60                     %s
61                 </column>
62             </board>
63             </form>""" % ('\n'.join(left), '\n'.join(right))
64
65         return arch
66
67     def write(self, cr, uid, ids, vals, context=None):
68
69         """
70         Writes values in one or several fields.
71         @param cr: the current row, from the database cursor,
72         @param uid: the current user’s ID for security checks,
73         @param ids: List of Board's IDs
74         @param vals: dictionary with values to update.
75                      dictionary must be with the form: {‘name_of_the_field’: value, ...}.
76         @return: True
77         """
78         result = super(board_board, self).write(cr, uid, ids, vals, context=context)
79
80         board = self.pool.get('board.board').browse(cr, uid, ids[0], context=context)
81         view = self.create_view(cr, uid, ids[0], context=context)
82         id = board.view_id.id
83         cr.execute("update ir_ui_view set arch=%s where id=%s", (view, id))
84         return result
85
86     def create(self, cr, user, vals, context=None):
87         """
88         create new record.
89         @param cr: the current row, from the database cursor,
90         @param uid: the current user’s ID for security checks,
91         @param vals: dictionary of values for every field.
92                       dictionary must use this form: {‘name_of_the_field’: value, ...}
93         @return: id of new created record of board.board.
94         """
95
96
97         if not 'name' in vals:
98             return False
99         id = super(board_board, self).create(cr, user, vals, context=context)
100         view_id = self.pool.get('ir.ui.view').create(cr, user, {
101             'name': vals['name'],
102             'model': 'board.board',
103             'priority': 16,
104             'type': 'form',
105             'arch': self.create_view(cr, user, id, context=context),
106         })
107
108         super(board_board, self).write(cr, user, [id], {'view_id': view_id}, context)
109
110         return id
111
112     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None,\
113                          toolbar=False, submenu=False):
114         """
115         Overrides orm field_view_get.
116         @return: Dictionary of Fields, arch and toolbar.
117         """
118
119         res = {}
120         res = super(board_board, self).fields_view_get(cr, user, view_id, view_type,\
121                                  context, toolbar=toolbar, submenu=submenu)
122
123         vids = self.pool.get('ir.ui.view.custom').search(cr, user,\
124                      [('user_id', '=', user), ('ref_id' ,'=', view_id)])
125         if vids:
126             view_id = vids[0]
127             arch = self.pool.get('ir.ui.view.custom').browse(cr, user, view_id, context=context)
128             res['custom_view_id'] = view_id
129             res['arch'] = arch.arch
130         res['arch'] = self._arch_preprocessing(cr, user, res['arch'], context=context)
131         res['toolbar'] = {'print': [], 'action': [], 'relate': []}
132         return res
133
134
135     def _arch_preprocessing(self, cr, user, arch, context=None):
136         from lxml import etree
137         def remove_unauthorized_children(node):
138             for child in node.iterchildren():
139                 if child.tag=='action' and child.get('invisible'):
140                     node.remove(child)
141                 else:
142                     child=remove_unauthorized_children(child)
143             return node
144
145         def encode(s):
146             if isinstance(s, unicode):
147                 return s.encode('utf8')
148             return s
149
150         archnode = etree.fromstring(encode(arch))
151         return etree.tostring(remove_unauthorized_children(archnode),pretty_print=True)
152
153
154
155
156     _columns = {
157         'name': fields.char('Dashboard', size=64, required=True),
158         'view_id': fields.many2one('ir.ui.view', 'Board View'),
159         'line_ids': fields.one2many('board.board.line', 'board_id', 'Action Views')
160     }
161
162     # the following lines added to let the button on dashboard work.
163     _defaults = {
164         'name':lambda *args:  'Dashboard'
165     }
166
167 class board_line(osv.osv):
168     """
169     Board Line
170     """
171     _name = 'board.board.line'
172     _description = "Board Line"
173     _order = 'position,sequence'
174     _columns = {
175         'name': fields.char('Title', size=64, required=True),
176         'sequence': fields.integer('Sequence', help="Gives the sequence order\
177                          when displaying a list of board lines.", select=True),
178         'height': fields.integer('Height'),
179         'width': fields.integer('Width'),
180         'board_id': fields.many2one('board.board', 'Dashboard', required=True, ondelete='cascade'),
181         'action_id': fields.many2one('ir.actions.act_window', 'Action', required=True),
182         'position': fields.selection([('left','Left'),
183                                       ('right','Right')], 'Position', required=True, select=True)
184     }
185     _defaults = {
186         'position': lambda *args: 'left'
187     }
188
189 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: