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