[MERGE] changes required for the "import hook" commit in the server.
[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" colspan="4"' % (line.name, line.action_id.id)
47             if line.height:
48                 linestr += (' height="%d"' % (line.height, ))
49             if line.width:
50                 linestr += (' width="%d"' % (line.width, ))
51             linestr += '/>'
52             if line.position == 'left':
53                 left.append(linestr)
54             else:
55                 right.append(linestr)
56         #End Loop
57         arch = """<?xml version="1.0"?>
58             <form string="My Board">
59             <board style="1-1">
60                 <column>
61                     %s
62                 </column>
63                 <column>
64                     %s
65                 </column>
66             </board>
67             </form>""" % ('\n'.join(left), '\n'.join(right))
68
69         return arch
70
71     def write(self, cr, uid, ids, vals, context=None):
72
73         """
74         Writes values in one or several fields.
75         @param cr: the current row, from the database cursor,
76         @param uid: the current user’s ID for security checks,
77         @param ids: List of Board's IDs
78         @param vals: dictionary with values to update.
79                      dictionary must be with the form: {‘name_of_the_field’: value, ...}.
80         @return: True
81         """
82         result = super(board_board, self).write(cr, uid, ids, vals, context=context)
83
84         board = self.pool.get('board.board').browse(cr, uid, ids[0], context=context)
85         view = self.create_view(cr, uid, ids[0], context=context)
86         id = board.view_id.id
87         cr.execute("update ir_ui_view set arch=%s where id=%s", (view, id))
88         return result
89
90     def create(self, cr, user, vals, context=None):
91         """
92         create new record.
93         @param cr: the current row, from the database cursor,
94         @param uid: the current user’s ID for security checks,
95         @param vals: dictionary of values for every field.
96                       dictionary must use this form: {‘name_of_the_field’: value, ...}
97         @return: id of new created record of board.board.
98         """
99
100
101         if not 'name' in vals:
102             return False
103         id = super(board_board, self).create(cr, user, vals, context=context)
104         view_id = self.pool.get('ir.ui.view').create(cr, user, {
105             'name': vals['name'],
106             'model': 'board.board',
107             'priority': 16,
108             'type': 'form',
109             'arch': self.create_view(cr, user, id, context=context),
110         })
111
112         super(board_board, self).write(cr, user, [id], {'view_id': view_id}, context)
113
114         return id
115
116     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None,\
117                          toolbar=False, submenu=False):
118         """
119         Overrides orm field_view_get.
120         @return: Dictionary of Fields, arch and toolbar.
121         """
122
123         res = {}
124         res = super(board_board, self).fields_view_get(cr, user, view_id, view_type,\
125                                  context, toolbar=toolbar, submenu=submenu)
126
127         vids = self.pool.get('ir.ui.view.custom').search(cr, user,\
128                      [('user_id', '=', user), ('ref_id' ,'=', view_id)])
129         if vids:
130             view_id = vids[0]
131             arch = self.pool.get('ir.ui.view.custom').browse(cr, user, view_id, context=context)
132             res['custom_view_id'] = view_id
133             res['arch'] = arch.arch
134         res['arch'] = self._arch_preprocessing(cr, user, res['arch'], context=context)
135         res['toolbar'] = {'print': [], 'action': [], 'relate': []}
136         return res
137
138
139     def _arch_preprocessing(self, cr, user, arch, context=None):
140         from lxml import etree
141         def remove_unauthorized_children(node):
142             for child in node.iterchildren():
143                 if child.tag=='action' and child.get('invisible'):
144                     node.remove(child)
145                 else:
146                     child=remove_unauthorized_children(child)
147             return node
148
149         def encode(s):
150             if isinstance(s, unicode):
151                 return s.encode('utf8')
152             return s
153
154         archnode = etree.fromstring(encode(arch))
155         return etree.tostring(remove_unauthorized_children(archnode),pretty_print=True)
156
157
158
159
160     _columns = {
161         'name': fields.char('Dashboard', size=64, required=True),
162         'view_id': fields.many2one('ir.ui.view', 'Board View'),
163         'line_ids': fields.one2many('board.board.line', 'board_id', 'Action Views')
164     }
165
166     # the following lines added to let the button on dashboard work.
167     _defaults = {
168         'name':lambda *args:  'Dashboard'
169     }
170
171 class board_line(osv.osv):
172     """
173     Board Line
174     """
175     _name = 'board.board.line'
176     _description = "Board Line"
177     _order = 'position,sequence'
178     _columns = {
179         'name': fields.char('Title', size=64, required=True),
180         'sequence': fields.integer('Sequence', help="Gives the sequence order\
181                          when displaying a list of board lines.", select=True),
182         'height': fields.integer('Height'),
183         'width': fields.integer('Width'),
184         'board_id': fields.many2one('board.board', 'Dashboard', required=True, ondelete='cascade'),
185         'action_id': fields.many2one('ir.actions.act_window', 'Action', required=True),
186         'position': fields.selection([('left','Left'),
187                                       ('right','Right')], 'Position', required=True, select=True)
188     }
189     _defaults = {
190         'position': lambda *args: 'left'
191     }
192
193 class res_log_report(osv.osv):
194     """ Log Report """
195     _name = "res.log.report"
196     _auto = False
197     _description = "Log Report"
198     _columns = {
199         'name': fields.char('Year', size=64, required=False, readonly=True),
200         'month':fields.selection([('01', 'January'), ('02', 'February'), \
201                                   ('03', 'March'), ('04', 'April'),\
202                                   ('05', 'May'), ('06', 'June'), \
203                                   ('07', 'July'), ('08', 'August'),\
204                                   ('09', 'September'), ('10', 'October'),\
205                                   ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
206         'day': fields.char('Day', size=128, readonly=True),
207         'creation_date': fields.date('Creation Date', readonly=True),
208         'res_model': fields.char('Object', size=128),
209         'nbr': fields.integer('# of Entries', readonly=True)
210      }
211
212     def init(self, cr):
213         """
214             Log Report
215             @param cr: the current row, from the database cursor
216         """
217         tools.drop_view_if_exists(cr,'res_log_report')
218         cr.execute("""
219             CREATE OR REPLACE VIEW res_log_report AS (
220                 SELECT
221                     l.id as id,
222                     1 as nbr,
223                     to_char(l.create_date, 'YYYY') as name,
224                     to_char(l.create_date, 'MM') as month,
225                     to_char(l.create_date, 'YYYY-MM-DD') as day,
226                     to_char(l.create_date, 'YYYY-MM-DD') as creation_date,
227                     l.res_model as res_model,
228                     date_trunc('day',l.create_date) as create_date
229                 FROM
230                     res_log l
231             )""")
232 res_log_report()
233
234 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: