From 8df6b16784b64d4ebb375290f96a7c8e2fd4f949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 10:04:47 +0100 Subject: [PATCH 01/11] [ADD] website: added support for invoking a server action through controller website: added website/action/ 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 --- addons/website/controllers/main.py | 31 ++++++++++++++++++++++++++-- addons/website/data/website_data.xml | 18 ++++++++++++++++ addons/website/models/__init__.py | 1 + addons/website/models/ir_actions.py | 26 +++++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 addons/website/models/ir_actions.py diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index b9fc68bb667..9cd22a76e6e 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -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/'], 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('/') diff --git a/addons/website/data/website_data.xml b/addons/website/data/website_data.xml index 50f2d8e381e..dc50a494a49 100644 --- a/addons/website/data/website_data.xml +++ b/addons/website/data/website_data.xml @@ -1,5 +1,23 @@ + + + + + Website Test + True + + +values = {} +template = request.website.render("website.test0", values) + + code + ir.actions.server + + + www.openerp.com diff --git a/addons/website/models/__init__.py b/addons/website/models/__init__.py index e17fba63e39..da3c232a24e 100644 --- a/addons/website/models/__init__.py +++ b/addons/website/models/__init__.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import ir_actions import ir_ui_view import website import ir_qweb diff --git a/addons/website/models/ir_actions.py b/addons/website/models/ir_actions.py new file mode 100644 index 00000000000..c6b71e51e1e --- /dev/null +++ b/addons/website/models/ir_actions.py @@ -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 From 1e6f81abc2ae0d1c19fbdad333bc1df2208daea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 10:45:30 +0100 Subject: [PATCH 02/11] [IMP] website: ir_actions_server: only published server action can be invoked from the front-end bzr revid: tde@openerp.com-20140205094530-k058ealuxq9hbrdw --- addons/website/__openerp__.py | 3 ++- addons/website/controllers/main.py | 12 +++++++++--- addons/website/data/website_data.xml | 1 + addons/website/models/ir_actions.py | 10 +++++++--- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/addons/website/__openerp__.py b/addons/website/__openerp__.py index 7779686c041..4cfabd942ca 100644 --- a/addons/website/__openerp__.py +++ b/addons/website/__openerp__.py @@ -20,12 +20,13 @@ OpenERP Website CMS 'views/snippets.xml', 'views/themes.xml', 'views/res_config.xml', + 'views/ir_actions.xml', ], 'demo': [ 'data/website_demo.xml', ], 'js': ['static/src/js/website.backend.js'], - 'qweb' : ['static/src/xml/website.backend.xml'], + 'qweb': ['static/src/xml/website.backend.xml'], 'css': ['static/src/css/website.backend.css'], 'application': True, } diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index 9cd22a76e6e..29d2652ba94 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -374,6 +374,10 @@ class Website(openerp.addons.web.controllers.main.Home): cr, uid, context = request.cr, request.uid, request.context res, action_id, action = None, None, None ServerActions = request.registry['ir.actions.server'] + # add the post values in the context, to be able to handle it + if context is None: + context = {} + context.get('post', {}).update(post) # 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: @@ -389,9 +393,11 @@ class Website(openerp.addons.web.controllers.main.Home): 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 + action = ServerActions.browse(cr, uid, action_id, context=context) + if action.state == 'code' and action.website_published: + 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('/') diff --git a/addons/website/data/website_data.xml b/addons/website/data/website_data.xml index dc50a494a49..85476c8f7fe 100644 --- a/addons/website/data/website_data.xml +++ b/addons/website/data/website_data.xml @@ -8,6 +8,7 @@ Website Test True + values = {} diff --git a/addons/website/models/ir_actions.py b/addons/website/models/ir_actions.py index c6b71e51e1e..e47c3496f31 100644 --- a/addons/website/models/ir_actions.py +++ b/addons/website/models/ir_actions.py @@ -9,9 +9,13 @@ class actions_server(osv.Model): _name = 'ir.actions.server' _inherit = ['ir.actions.server'] - # _columns = { - # 'website': fields.boolean('Website Stuff'), - # } + _columns = { + 'website_published': fields.boolean( + 'Available on the Website', + help='A Code server action can be run using a dedicated controller. Set' + 'this field as True to allow users to run this action. If it is False' + 'the action cannot be run through the dedicated controller.'), + } def _get_eval_context(self, cr, uid, action, context=None): eval_context = super(actions_server, self)._get_eval_context(cr, uid, action, context=context) From 5ec4259a34be606c9041db64068d54436e10415b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 11:10:51 +0100 Subject: [PATCH 03/11] [IMP] website: removed dummy data used for testing, added a first demo template + server action callable through the front-end, working on partners. Some king of simple partner directory. bzr revid: tde@openerp.com-20140205101051-d9ysg4fel30g2cgy --- addons/website/data/website_data.xml | 19 ----------- addons/website/data/website_demo.xml | 47 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/addons/website/data/website_data.xml b/addons/website/data/website_data.xml index 85476c8f7fe..50f2d8e381e 100644 --- a/addons/website/data/website_data.xml +++ b/addons/website/data/website_data.xml @@ -1,24 +1,5 @@ - - - - - Website Test - True - - - -values = {} -template = request.website.render("website.test0", values) - - code - ir.actions.server - - - www.openerp.com diff --git a/addons/website/data/website_demo.xml b/addons/website/data/website_demo.xml index d23bc50c817..66919daf960 100644 --- a/addons/website/data/website_demo.xml +++ b/addons/website/data/website_demo.xml @@ -1,5 +1,52 @@ + + + + + + Website Test + True + + + +partner_ids = pool['res.partner'].search(cr, uid, [('customer', '=', True)], context=context) +partners = pool['res.partner'].browse(cr, uid, partner_ids, context=context) +values = { + 'partners': partners, +} +template = request.website.render("website.template_partner_demo", values) + + code + ir.actions.server + + + From 1c608569ca96fad950781cb330a122e7a3b83ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 11:55:55 +0100 Subject: [PATCH 04/11] [IMP] website: server action in front-end: updated demo data to show something vaguely complex that 'everyone' can do using 2 server actions and 2 templates: a basic partner directory. bzr revid: tde@openerp.com-20140205105555-hzpwwqgefdiy12jg --- addons/website/controllers/main.py | 2 +- addons/website/data/website_demo.xml | 114 ++++++++++++++++++++++----- 2 files changed, 97 insertions(+), 19 deletions(-) diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index 29d2652ba94..c043ff82458 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -377,7 +377,7 @@ class Website(openerp.addons.web.controllers.main.Home): # add the post values in the context, to be able to handle it if context is None: context = {} - context.get('post', {}).update(post) + context['post'] = post or {} # 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: diff --git a/addons/website/data/website_demo.xml b/addons/website/data/website_demo.xml index 66919daf960..57480eedd92 100644 --- a/addons/website/data/website_demo.xml +++ b/addons/website/data/website_demo.xml @@ -1,10 +1,100 @@ - + + + + https://twitter.com/openerp + https://www.facebook.com/OpenERP + https://plus.google.com/+openerp + http://www.linkedin.com/company/openerp + + + + Great Product for Great People + + + + + + + + + + + Website Partner Detail Demo + True + + + +partner_id, partner = False, None +post_partner_id = context.get('post', {}).get('partner_id') +if post_partner_id: + try: + post_partner_id = int(post_partner_id) + except ValueError: + post_partner_id = False +if post_partner_id: + partner_ids = pool['res.partner'].search(cr, uid, [('id', '=', post_partner_id)], context=context) + partner_id = partner_ids and partner_ids[0] or False +if partner_id: + partner = pool['res.partner'].browse(cr, uid, partner_id, context=context) +values = { + 'partner': partner, +} +template = request.website.render("website.template_partner_detail_demo", values) + + code + ir.actions.server + - Website Test + Website Partners Demo True @@ -46,18 +138,4 @@ template = request.website.render("website.template_partner_demo", values) ir.actions.server - - - - - https://twitter.com/openerp - https://www.facebook.com/OpenERP - https://plus.google.com/+openerp - http://www.linkedin.com/company/openerp - - - - Great Product for Great People - - From 8d1713170aa4d247b4496aeaeaab64df737642db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 12:01:18 +0100 Subject: [PATCH 05/11] [FIX] website: server action demo data: ValueError is not defined, do something generic bzr revid: tde@openerp.com-20140205110118-6c5mq24x8e806vvn --- addons/website/data/website_demo.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/website/data/website_demo.xml b/addons/website/data/website_demo.xml index 57480eedd92..364012f89c8 100644 --- a/addons/website/data/website_demo.xml +++ b/addons/website/data/website_demo.xml @@ -76,7 +76,7 @@ post_partner_id = context.get('post', {}).get('partner_id') if post_partner_id: try: post_partner_id = int(post_partner_id) - except ValueError: + except: post_partner_id = False if post_partner_id: partner_ids = pool['res.partner'].search(cr, uid, [('id', '=', post_partner_id)], context=context) From ba962f9b1c811f16fc8683e8541befdb1bfc4517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 13:37:37 +0100 Subject: [PATCH 06/11] [ADD] website: added forgottent view file for ir_actions bzr revid: tde@openerp.com-20140205123737-o7btwj3xryosnayo --- addons/website/views/ir_actions.xml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 addons/website/views/ir_actions.xml diff --git a/addons/website/views/ir_actions.xml b/addons/website/views/ir_actions.xml new file mode 100644 index 00000000000..0b017a43192 --- /dev/null +++ b/addons/website/views/ir_actions.xml @@ -0,0 +1,18 @@ + + + + + + ir.actions.server.form.website + ir.actions.server + + + + + + + + + + From 79d6a1a39aa1f5a45ae7371f00068023bfd8952d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 15:02:16 +0100 Subject: [PATCH 07/11] [IMP] website: server action controller: no need to add post, everything is on request bzr revid: tde@openerp.com-20140205140216-r6oxlbbmypd3u5vf --- addons/website/controllers/main.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index c043ff82458..e97a3be9d97 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -374,10 +374,6 @@ class Website(openerp.addons.web.controllers.main.Home): cr, uid, context = request.cr, request.uid, request.context res, action_id, action = None, None, None ServerActions = request.registry['ir.actions.server'] - # add the post values in the context, to be able to handle it - if context is None: - context = {} - context['post'] = post or {} # 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: From 89d053111ff6c2f45795cfc8bc71b963d6232994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 15:02:38 +0100 Subject: [PATCH 08/11] [IMP] website: server action: response is a better keyworkd than template bzr revid: tde@openerp.com-20140205140238-liili9v4l0a074zj --- addons/website/models/ir_actions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/website/models/ir_actions.py b/addons/website/models/ir_actions.py index e47c3496f31..47f5124f887 100644 --- a/addons/website/models/ir_actions.py +++ b/addons/website/models/ir_actions.py @@ -25,6 +25,6 @@ class actions_server(osv.Model): 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'] + if 'response' in eval_context: + return eval_context['response'] return res From 9487dafd12877aea2960faabbd5c84b57f78f0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 5 Feb 2014 15:15:15 +0100 Subject: [PATCH 09/11] [IMP] website: server action in front-end: improved demo data, a mini posting mechanism bzr revid: tde@openerp.com-20140205141515-z0yfu11iufq0qqdk --- addons/website/data/website_demo.xml | 83 +++++++++++++--------------- 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/addons/website/data/website_demo.xml b/addons/website/data/website_demo.xml index 364012f89c8..52fb7dd2238 100644 --- a/addons/website/data/website_demo.xml +++ b/addons/website/data/website_demo.xml @@ -20,7 +20,7 @@ Some demo data for front-end server action generating templates --> -