2014-02-05 09:04:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-02-07 12:19:21 +00:00
|
|
|
import urlparse
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
from openerp.http import request
|
2014-02-05 09:04:47 +00:00
|
|
|
from openerp.osv import fields, osv
|
|
|
|
|
|
|
|
|
|
|
|
class actions_server(osv.Model):
|
|
|
|
""" Add website option in server actions. """
|
|
|
|
_name = 'ir.actions.server'
|
|
|
|
_inherit = ['ir.actions.server']
|
|
|
|
|
2014-02-07 12:19:21 +00:00
|
|
|
def _compute_website_url(self, cr, uid, id, website_path, xml_id, context=None):
|
|
|
|
base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url', context=context)
|
|
|
|
link = website_path or xml_id or (id and '%d' % id) or ''
|
|
|
|
if base_url and link:
|
|
|
|
path = '%s/%s' % ('/website/action', link)
|
|
|
|
return '%s' % urlparse.urljoin(base_url, path)
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def _get_website_url(self, cr, uid, ids, name, args, context=None):
|
|
|
|
res = dict.fromkeys(ids, False)
|
|
|
|
for action in self.browse(cr, uid, ids, context=context):
|
2014-02-07 12:32:49 +00:00
|
|
|
if action.state == 'code' and action.website_published:
|
|
|
|
res[action.id] = self._compute_website_url(cr, uid, action.id, action.website_path, action.xml_id, context=context)
|
2014-02-07 12:19:21 +00:00
|
|
|
return res
|
|
|
|
|
2014-02-05 09:45:30 +00:00
|
|
|
_columns = {
|
2014-02-07 12:19:21 +00:00
|
|
|
'xml_id': fields.function(
|
|
|
|
osv.osv.get_xml_id, type='char', string="External ID",
|
|
|
|
help="ID of the action if defined in a XML file"),
|
2014-02-12 10:42:27 +00:00
|
|
|
'website_path': fields.char('Website Path'),
|
2014-02-07 12:19:21 +00:00
|
|
|
'website_url': fields.function(
|
|
|
|
_get_website_url, type='char', string='Website URL',
|
|
|
|
help='The full URL to access the server action through the website.'),
|
2014-02-05 09:45:30 +00:00
|
|
|
'website_published': fields.boolean(
|
2014-07-06 14:44:26 +00:00
|
|
|
'Available on the Website', copy=False,
|
2014-02-06 08:19:21 +00:00
|
|
|
help='A code server action can be executed from the website, using a dedicated'
|
2014-02-12 10:42:27 +00:00
|
|
|
'controller. The address is <base>/website/action/<website_path>.'
|
2014-02-06 08:19:21 +00:00
|
|
|
'Set this field as True to allow users to run this action. If it'
|
|
|
|
'set to is False the action cannot be run through the website.'),
|
2014-02-05 09:45:30 +00:00
|
|
|
}
|
2014-02-05 09:04:47 +00:00
|
|
|
|
2014-02-07 12:19:21 +00:00
|
|
|
def on_change_website_path(self, cr, uid, ids, website_path, xml_id, context=None):
|
|
|
|
values = {
|
2014-08-08 07:43:43 +00:00
|
|
|
'website_url': self._compute_website_url(cr, uid, ids and ids[0] or None, website_path, xml_id, context=context)
|
2014-02-07 12:19:21 +00:00
|
|
|
}
|
|
|
|
return {'value': values}
|
|
|
|
|
2014-02-05 09:04:47 +00:00
|
|
|
def _get_eval_context(self, cr, uid, action, context=None):
|
2014-02-06 08:19:21 +00:00
|
|
|
""" Override to add the request object in eval_context. """
|
2014-02-05 09:04:47 +00:00
|
|
|
eval_context = super(actions_server, self)._get_eval_context(cr, uid, action, context=context)
|
|
|
|
if action.state == 'code':
|
|
|
|
eval_context['request'] = request
|
|
|
|
return eval_context
|
|
|
|
|
|
|
|
def run_action_code_multi(self, cr, uid, action, eval_context=None, context=None):
|
2014-02-06 08:19:21 +00:00
|
|
|
""" Override to allow returning response the same way action is already
|
|
|
|
returned by the basic server action behavior. Note that response has
|
|
|
|
priority over action, avoid using both. """
|
2014-02-05 09:04:47 +00:00
|
|
|
res = super(actions_server, self).run_action_code_multi(cr, uid, action, eval_context, context)
|
2014-02-05 14:02:38 +00:00
|
|
|
if 'response' in eval_context:
|
|
|
|
return eval_context['response']
|
2014-02-05 09:04:47 +00:00
|
|
|
return res
|