[MERGE] Sync with trunk
[odoo/odoo.git] / addons / board / controllers.py
1 # -*- coding: utf-8 -*-
2 from xml.etree import ElementTree
3
4 import openerp
5 from openerp.addons.web.controllers.main import load_actions_from_ir_values
6
7 class Board(openerp.addons.web.http.Controller):
8     _cp_path = '/board'
9
10     @openerp.addons.web.http.jsonrequest
11     def add_to_dashboard(self, req, menu_id, action_id, context_to_save, domain, view_mode, name=''):
12         # FIXME move this method to board.board model
13         dashboard_action = load_actions_from_ir_values(
14             req, 'action', 'tree_but_open', [('ir.ui.menu', menu_id)], False)
15
16         if dashboard_action:
17             action = dashboard_action[0][2]
18             if action['res_model'] == 'board.board' and action['views'][0][1] == 'form':
19                 # Maybe should check the content instead of model board.board ?
20                 view_id = action['views'][0][0]
21                 board = req.session.model(action['res_model']).fields_view_get(view_id, 'form')
22                 if board and 'arch' in board:
23                     xml = ElementTree.fromstring(board['arch'])
24                     column = xml.find('./board/column')
25                     if column is not None:
26                         new_action = ElementTree.Element('action', {
27                             'name': str(action_id),
28                             'string': name,
29                             'view_mode': view_mode,
30                             'context': str(context_to_save),
31                             'domain': str(domain)
32                         })
33                         column.insert(0, new_action)
34                         arch = ElementTree.tostring(xml, 'utf-8')
35                         return req.session.model('ir.ui.view.custom').create({
36                             'user_id': req.session._uid,
37                             'ref_id': view_id,
38                             'arch': arch
39                         }, req.context)
40
41         return False