29e8a3310b4a2ba614fb110c0ee705a0cd18817a
[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 import time
23 from osv import fields,osv
24
25 class board_board(osv.osv):
26     _name = 'board.board'
27
28     def create_view(self, cr, uid, ids, context):
29
30         board = self.pool.get('board.board').browse(cr, uid, ids, context)
31         left = []
32         right = []
33         for line in board.line_ids:
34             linestr = '<action string="%s" name="%d" colspan="4"' % (line.name, line.action_id.id)
35             if line.height:
36                 linestr+=(' height="%d"' % (line.height,))
37             if line.width:
38                 linestr+=(' width="%d"' % (line.width,))
39             linestr += '/>'
40             if line.position=='left':
41                 left.append(linestr)
42             else:
43                 right.append(linestr)
44         arch = """<?xml version="1.0"?>
45             <form string="My Board">
46             <hpaned>
47                 <child1>
48                     %s
49                 </child1>
50                 <child2>
51                     %s
52                 </child2>
53             </hpaned>
54             </form>""" % ('\n'.join(left), '\n'.join(right))
55
56         return arch
57
58     def write(self, cr, uid, ids, vals, context={}):
59         result = super(board_board, self).write(cr, uid, ids, vals, context)
60         cr.commit()
61
62         board = self.pool.get('board.board').browse(cr, uid, ids[0])
63
64         view = self.create_view(cr, uid, ids[0], context)
65         id = board.view_id.id
66         cr.execute("update ir_ui_view set arch=%s where id=%s" , (view, id))
67         cr.commit()
68
69         return result
70
71     def create(self, cr, user, vals, context=None):
72         if not 'name' in vals:
73             return False
74         id = super(board_board, self).create(cr, user, vals, context)
75         view_id = self.pool.get('ir.ui.view').create(cr, user, {
76             'name': vals['name'],
77             'model':'board.board',
78             'priority':16,
79             'type': 'form',
80             'arch': self.create_view(cr, user, id, context),
81         })
82
83         super(board_board, self).write(cr, user, [id], {'view_id': view_id}, context)
84
85         return id
86
87     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
88         res = {}
89         res = super(board_board, self).fields_view_get(cr, user, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
90
91         vids = self.pool.get('ir.ui.view.custom').search(cr, user, [('user_id','=',user), ('ref_id','=',view_id)])
92         if vids:
93             view_id = vids[0]
94             arch = self.pool.get('ir.ui.view.custom').browse(cr, user, view_id)
95             res['arch'] = arch.arch
96
97         res['toolbar'] = {'print':[],'action':[],'relate':[]}
98         return res
99
100     _columns = {
101         'name': fields.char('Dashboard', size=64, required=True),
102         'view_id': fields.many2one('ir.ui.view', 'Board View'),
103         'line_ids': fields.one2many('board.board.line', 'board_id', 'Action Views')
104     }
105
106     # the following lines added to let the button on dashboard work.
107     _defaults = {
108         'name': lambda *args: 'Dashboard'
109     }
110
111 board_board()
112
113 class board_line(osv.osv):
114     _name = 'board.board.line'
115     _order = 'position,sequence'
116     _columns = {
117         'name': fields.char('Title', size=64, required=True),
118         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of board lines."),
119         'height': fields.integer('Height'),
120         'width': fields.integer('Width'),
121         'board_id': fields.many2one('board.board', 'Dashboard', required=True, ondelete='cascade'),
122         'action_id': fields.many2one('ir.actions.act_window', 'Action', required=True),
123         'position': fields.selection([('left','Left'),('right','Right')], 'Position', required=True)
124     }
125     _defaults = {
126         'position': lambda *args: 'left'
127     }
128 board_line()
129
130 class board_note_type(osv.osv):
131     _name = 'board.note.type'
132     _columns = {
133         'name': fields.char('Note Type', size=64, required=True),
134     }
135 board_note_type()
136
137 def _type_get(self, cr, uid, context={}):
138     obj = self.pool.get('board.note.type')
139     ids = obj.search(cr, uid, [])
140     res = obj.read(cr, uid, ids, ['name'], context)
141     res = [(r['name'], r['name']) for r in res]
142     return res
143
144 class board_note(osv.osv):
145     _name = 'board.note'
146     _columns = {
147         'name': fields.char('Subject', size=128, required=True),
148         'note': fields.text('Note'),
149         'user_id': fields.many2one('res.users', 'Author', size=64),
150         'date': fields.date('Date', size=64, required=True),
151         'type': fields.selection(_type_get, 'Note type', size=64),
152     }
153     _defaults = {
154         'user_id': lambda object,cr,uid,context: uid,
155         'date': lambda object,cr,uid,context: time.strftime('%Y-%m-%d'),
156     }
157 board_note()
158
159 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
160