1b469adfc05f6690185e4615d30373ef46093e53
[odoo/odoo.git] / addons / website / models / ir_actions.py
1 # -*- coding: utf-8 -*-
2
3 import urlparse
4
5 from openerp.http import request
6 from openerp.osv import fields, osv
7
8
9 class actions_server(osv.Model):
10     """ Add website option in server actions. """
11     _name = 'ir.actions.server'
12     _inherit = ['ir.actions.server']
13
14     def _compute_website_url(self, cr, uid, id, website_path, xml_id, context=None):
15         base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url', context=context)
16         link = website_path or xml_id or (id and '%d' % id) or ''
17         if base_url and link:
18             path = '%s/%s' % ('/website/action', link)
19             return '%s' % urlparse.urljoin(base_url, path)
20         return ''
21
22     def _get_website_url(self, cr, uid, ids, name, args, context=None):
23         res = dict.fromkeys(ids, False)
24         for action in self.browse(cr, uid, ids, context=context):
25             if action.state == 'code' and action.website_published:
26                 res[action.id] = self._compute_website_url(cr, uid, action.id, action.website_path, action.xml_id, context=context)
27         return res
28
29     _columns = {
30         'xml_id': fields.function(
31             osv.osv.get_xml_id, type='char', string="External ID",
32             help="ID of the action if defined in a XML file"),
33         'website_path': fields.char('Website Path'),
34         'website_url': fields.function(
35             _get_website_url, type='char', string='Website URL',
36             help='The full URL to access the server action through the website.'),
37         'website_published': fields.boolean(
38             'Available on the Website', copy=False,
39             help='A code server action can be executed from the website, using a dedicated'
40                  'controller. The address is <base>/website/action/<website_path>.'
41                  'Set this field as True to allow users to run this action. If it'
42                  'set to is False the action cannot be run through the website.'),
43     }
44
45     def on_change_website_path(self, cr, uid, ids, website_path, xml_id, context=None):
46         values = {
47             'website_url': self._compute_website_url(cr, uid, ids[0], website_path, xml_id, context=context)
48         }
49         return {'value': values}
50
51     def _get_eval_context(self, cr, uid, action, context=None):
52         """ Override to add the request object in eval_context. """
53         eval_context = super(actions_server, self)._get_eval_context(cr, uid, action, context=context)
54         if action.state == 'code':
55             eval_context['request'] = request
56         return eval_context
57
58     def run_action_code_multi(self, cr, uid, action, eval_context=None, context=None):
59         """ Override to allow returning response the same way action is already
60         returned by the basic server action behavior. Note that response has
61         priority over action, avoid using both. """
62         res = super(actions_server, self).run_action_code_multi(cr, uid, action, eval_context, context)
63         if 'response' in eval_context:
64             return eval_context['response']
65         return res