[IMP] website: server actions: usability improvements

- added website_path, the end of the path to access the server action, allowing more custom
and short URLs, like website/action/partners instead of website/action/website.action_partners;
- added website_url, computed file with the full address of the server action that is displayed
on the form view, to ease usability
- updated server action contorller: now takes xml_id, id, or website_path

bzr revid: tde@openerp.com-20140207121921-7kdpxrf30zlte47l
This commit is contained in:
Thibault Delavallée
2014-02-07 13:19:21 +01:00
parent fd008435e0
commit 7d696bc620
3 changed files with 67 additions and 9 deletions
+13 -7
View File
@@ -369,20 +369,26 @@ class Website(openerp.addons.web.controllers.main.Home):
#------------------------------------------------------
# Server actions
#------------------------------------------------------
@http.route(['/website/action/<id_or_xml_id>'], type='http', auth="public", website=True)
def actions_server(self, id_or_xml_id, **post):
_server_action_route = '/website/action'
@http.route('%s/<path_or_xml_id_or_id>' % _server_action_route, type='http', auth="public", website=True)
def actions_server(self, path_or_xml_id_or_id, **post):
cr, uid, context = request.cr, request.uid, request.context
res, action_id, action = None, None, None
ServerActions = request.registry['ir.actions.server']
# find the action_id, either an int, an int into a basestring, or an xml_id
if isinstance(id_or_xml_id, basestring) and '.' in id_or_xml_id:
action_id = request.registry['ir.model.data'].xmlid_to_res_id(request.cr, request.uid, id_or_xml_id, raise_if_not_found=False)
else:
# find the action_id: either an xml_id, the path, or an ID
if isinstance(path_or_xml_id_or_id, basestring) and '.' in path_or_xml_id_or_id:
action_id = request.registry['ir.model.data'].xmlid_to_res_id(request.cr, request.uid, path_or_xml_id_or_id, raise_if_not_found=False)
if not action_id:
action_ids = ServerActions.search(cr, uid, [('website_path', '=', path_or_xml_id_or_id), ('website_publihsed', '=', True)], context=context)
action_id = action_ids and action_ids[0] or None
if not action_id:
try:
action_id = int(id_or_xml_id)
action_id = int(path_or_xml_id_or_id)
except ValueError:
pass
# check it effectively exists
if action_id:
action_ids = ServerActions.exists(cr, uid, [action_id], context=context)
+30
View File
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-
import urlparse
from openerp.addons.web.http import request
# from openerp.addons.website.controllers.main import Website
from openerp.osv import fields, osv
@@ -9,7 +12,28 @@ class actions_server(osv.Model):
_name = 'ir.actions.server'
_inherit = ['ir.actions.server']
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):
res[action.id] = self._compute_website_url(cr, uid, action.id, action.website_path, action.xml_id, context=context)
return res
_columns = {
'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"),
'website_url': fields.function(
_get_website_url, type='char', string='Website URL',
help='The full URL to access the server action through the website.'),
'website_path': fields.char('Website Path'),
'website_published': fields.boolean(
'Available on the Website',
help='A code server action can be executed from the website, using a dedicated'
@@ -18,6 +42,12 @@ class actions_server(osv.Model):
'set to is False the action cannot be run through the website.'),
}
def on_change_website_path(self, cr, uid, ids, website_path, xml_id, context=None):
values = {
'website_url': self._compute_website_url(cr, uid, ids[0], website_path, xml_id, context=context)
}
return {'value': values}
def _get_eval_context(self, cr, uid, action, context=None):
""" Override to add the request object in eval_context. """
eval_context = super(actions_server, self)._get_eval_context(cr, uid, action, context=context)
+24 -2
View File
@@ -6,10 +6,32 @@
<field name="name">ir.actions.server.form.website</field>
<field name="model">ir.actions.server</field>
<field name="inherit_id" ref="base.view_server_action_form"/>
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='state']" position="after">
<field name="website_published"
attrs="{'invisible': [('state', '!=', 'code')]}"/>
</xpath>
<xpath expr="//field[@name='sequence']" position="after">
<field name="xml_id" invisible="1"/>
<field name="website_path"
on_change="on_change_website_path(website_path, xml_id, context)"
attrs="{'invisible': ['|', ('website_published', '!=', True), ('state', '!=', 'code')]}"/>
<field name="website_url" readonly="1"
attrs="{'invisible': ['|', ('website_published', '!=', True), ('state', '!=', 'code')]}"/>
</xpath>
</data>
</field>
</record>
<record model="ir.ui.view" id="view_server_action_search_website">
<field name="name">ir.actions.server.search.website</field>
<field name="model">ir.actions.server</field>
<field name="inherit_id" ref="base.view_server_action_search"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='state']" position="after">
<field name="website_published"
attrs="{'invisible': [('state', '!=', 'code')]}"/>
<filter string="Website" name="website"
domain="[('website_published', '=', True), ('state', '=', 'code')]"/>
</xpath>
</field>
</record>