[ADD] website: added support for invoking a server action through controller
website: added website/action/<id_or_xml_id> route, that runs the server action designed by its id or xml_id. Only code server action returning a template give a result. The template is displayed. Other or not existing server actions redirect to the homepage. website: added an override of ir_actions_server: evaluation context gets request for evaluation, to enable request.render(template_name); also returns template the same way action is returned for code server action; bzr revid: tde@openerp.com-20140205090447-2hppg7818nx1wfzh
This commit is contained in:
parent
6400ffb5e5
commit
8df6b16784
|
@ -9,7 +9,6 @@ import datetime
|
|||
|
||||
from sys import maxint
|
||||
|
||||
import psycopg2
|
||||
import werkzeug
|
||||
import werkzeug.exceptions
|
||||
import werkzeug.utils
|
||||
|
@ -27,6 +26,7 @@ logger = logging.getLogger(__name__)
|
|||
# Completely arbitrary limits
|
||||
MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT = IMAGE_LIMITS = (1024, 768)
|
||||
|
||||
|
||||
class Website(openerp.addons.web.controllers.main.Home):
|
||||
#------------------------------------------------------
|
||||
# View
|
||||
|
@ -366,5 +366,32 @@ class Website(openerp.addons.web.controllers.main.Home):
|
|||
|
||||
return response
|
||||
|
||||
#------------------------------------------------------
|
||||
# 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):
|
||||
cr, uid, context = request.cr, request.uid, request.context
|
||||
res, action_id, action = None, None, None
|
||||
ServerActions = request.registry['ir.actions.server']
|
||||
|
||||
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
# 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:
|
||||
try:
|
||||
action_id = int(id_or_xml_id)
|
||||
except ValueError:
|
||||
pass
|
||||
# check it effectively exists
|
||||
if action_id:
|
||||
action_ids = ServerActions.exists(cr, uid, [action_id], context=context)
|
||||
action_id = action_ids and action_ids[0] or None
|
||||
# run it, return only LazyResponse that are templates to be rendered
|
||||
if action_id:
|
||||
action_res = ServerActions.run(cr, uid, [action_id], context=context)
|
||||
if isinstance(action_res, LazyResponse):
|
||||
res = action_res
|
||||
if res:
|
||||
return res
|
||||
return request.redirect('/')
|
||||
|
|
|
@ -1,5 +1,23 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<data noupdate="0">
|
||||
<template id="website.test0" name="Test0">
|
||||
<p>Super cacaprout</p>
|
||||
</template>
|
||||
|
||||
<record id="action_website_test" model="ir.actions.server">
|
||||
<field name="name">Website Test</field>
|
||||
<field name="condition">True</field>
|
||||
<field name="model_id" ref="base.model_res_partner"/>
|
||||
<field name="code">
|
||||
values = {}
|
||||
template = request.website.render("website.test0", values)
|
||||
</field>
|
||||
<field name="state">code</field>
|
||||
<field name="type">ir.actions.server</field>
|
||||
</record>
|
||||
</data>
|
||||
|
||||
<data noupdate="1">
|
||||
<record id="default_website" model="website">
|
||||
<field name="name">www.openerp.com</field>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import ir_actions
|
||||
import ir_ui_view
|
||||
import website
|
||||
import ir_qweb
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from openerp.addons.web.http import request
|
||||
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']
|
||||
|
||||
# _columns = {
|
||||
# 'website': fields.boolean('Website Stuff'),
|
||||
# }
|
||||
|
||||
def _get_eval_context(self, cr, uid, action, context=None):
|
||||
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):
|
||||
res = super(actions_server, self).run_action_code_multi(cr, uid, action, eval_context, context)
|
||||
if 'template' in eval_context:
|
||||
return eval_context['template']
|
||||
return res
|
Loading…
Reference in New Issue