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 #
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             <hpaned>
60                 <child1>
61                     %s
62                 </child1>
63                 <child2>
64                     %s
65                 </child2>
66             </hpaned>
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['arch'] = arch.arch
133         res['arch'] = self._arch_preprocessing(cr, user, res['arch'], context=context)
134         res['toolbar'] = {'print': [], 'action': [], 'relate': []}
135         return res
136     
137     
138     def _arch_preprocessing(self, cr, user, arch, context=None): 
139         from lxml import etree                               
140         def remove_unauthorized_children(node):
141             for child in node.iterchildren():
142                 if child.tag=='action' and child.get('invisible'):
143                     node.remove(child)
144                 else:
145                     child=remove_unauthorized_children(child)
146             return node
147         
148         def encode(s):
149             if isinstance(s, unicode):
150                 return s.encode('utf8')
151             return s
152             
153         archnode = etree.fromstring(encode(arch))        
154         return etree.tostring(remove_unauthorized_children(archnode),pretty_print=True)
155         
156         
157     
158
159     _columns = {
160         'name': fields.char('Dashboard', size=64, required=True),
161         'view_id': fields.many2one('ir.ui.view', 'Board View'),
162         'line_ids': fields.one2many('board.board.line', 'board_id', 'Action Views')
163     }
164
165     # the following lines added to let the button on dashboard work.
166     _defaults = {
167         'name':lambda *args:  'Dashboard'
168     }
169
170 board_board()
171
172
173 class board_line(osv.osv):
174     """
175     Board Line
176     """
177     _name = 'board.board.line'
178     _description = "Board Line"
179     _order = 'position,sequence'
180     _columns = {
181         'name': fields.char('Title', size=64, required=True),
182         'sequence': fields.integer('Sequence', help="Gives the sequence order\
183                          when displaying a list of board lines."),
184         'height': fields.integer('Height'),
185         'width': fields.integer('Width'),
186         'board_id': fields.many2one('board.board', 'Dashboard', required=True, ondelete='cascade'),
187         'action_id': fields.many2one('ir.actions.act_window', 'Action', required=True),
188         'position': fields.selection([('left','Left'),
189                                       ('right','Right')], 'Position', required=True)
190     }
191     _defaults = {
192         'position': lambda *args: 'left'
193     }
194
195 board_line()
196
197
198 class board_note_type(osv.osv):
199     """
200     Board note Type
201     """
202     _name = 'board.note.type'
203     _description = "Note Type"
204
205     _columns = {
206         'name': fields.char('Note Type', size=64, required=True),
207     }
208
209 board_note_type()
210
211 def _type_get(self, cr, uid, context=None):
212     """
213     Get by default Note type.
214     """
215     obj = self.pool.get('board.note.type')
216     ids = obj.search(cr, uid, [])
217     res = obj.read(cr, uid, ids, ['name'], context=context)
218     res = [(r['name'], r['name']) for r in res]
219     return res
220
221
222 class board_note(osv.osv):
223     """
224     Board Note
225     """
226     _name = 'board.note'
227     _description = "Note"
228     _columns = {
229         'name': fields.char('Subject', size=128, required=True),
230         'note': fields.text('Note'),
231         'user_id': fields.many2one('res.users', 'Author', size=64),
232         'date': fields.date('Date', size=64, required=True),
233         'type': fields.selection(_type_get, 'Note type', size=64),
234     }
235     _defaults = {
236         'user_id': lambda object, cr, uid, context: uid,
237         'date': lambda object, cr, uid, context: time.strftime('%Y-%m-%d'),
238     }
239
240 board_note()
241
242 class res_log_report(osv.osv):
243     """ Log Report """
244     _name = "res.log.report"
245     _auto = False
246     _description = "Log Report"
247     _columns = {
248         'name': fields.char('Year', size=64, required=False, readonly=True),
249         'month':fields.selection([('01', 'January'), ('02', 'February'), \
250                                   ('03', 'March'), ('04', 'April'),\
251                                   ('05', 'May'), ('06', 'June'), \
252                                   ('07', 'July'), ('08', 'August'),\
253                                   ('09', 'September'), ('10', 'October'),\
254                                   ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
255         'day': fields.char('Day', size=128, readonly=True),
256         'creation_date': fields.date('Creation Date', readonly=True),
257         'res_model': fields.char('Object', size=128),
258         'nbr': fields.integer('# of Entries', readonly=True)
259      }
260
261     def init(self, cr):
262         """
263             Log Report
264             @param cr: the current row, from the database cursor
265         """
266         tools.drop_view_if_exists(cr,'res_log_report')
267         cr.execute("""
268             CREATE OR REPLACE VIEW res_log_report AS (
269                 SELECT
270                     l.id as id,
271                     1 as nbr,
272                     to_char(l.create_date, 'YYYY') as name,
273                     to_char(l.create_date, 'MM') as month,
274                     to_char(l.create_date, 'YYYY-MM-DD') as day,
275                     to_char(l.create_date, 'YYYY-MM-DD') as creation_date,
276                     l.res_model as res_model,
277                     date_trunc('day',l.create_date) as create_date
278                 FROM
279                     res_log l
280             )""")
281 res_log_report()
282
283 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: