From 709f2506ff40d799aada58df74dd78522d965868 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 12:39:32 +0100 Subject: [PATCH] [REM] server-side eval_domain_and_context bzr revid: xmo@openerp.com-20121123113932-siz0u2vp2u7z6wk2 --- addons/web/controllers/main.py | 53 --------------------------- addons/web/doc/rpc.rst | 13 ++++--- addons/web/static/src/js/corelib.js | 11 +----- addons/web/static/src/js/pyeval.js | 8 ++-- addons/web/static/src/js/search.js | 2 +- addons/web/static/src/js/view_form.js | 4 +- addons/web/static/src/js/view_tree.js | 2 +- addons/web/static/src/js/views.js | 6 +-- addons/web/static/test/evals.js | 18 +++++++-- 9 files changed, 34 insertions(+), 83 deletions(-) diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 8b043dc2cb0..228da888767 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -919,59 +919,6 @@ class Session(openerpweb.Controller): # return all installed modules. Web client is smart enough to not load a module twice return module_installed(req) - @openerpweb.jsonrequest - def eval_domain_and_context(self, req, contexts, domains, - group_by_seq=None): - """ Evaluates sequences of domains and contexts, composing them into - a single context, domain or group_by sequence. - - :param list contexts: list of contexts to merge together. Contexts are - evaluated in sequence, all previous contexts - are part of their own evaluation context - (starting at the session context). - :param list domains: list of domains to merge together. Domains are - evaluated in sequence and appended to one another - (implicit AND), their evaluation domain is the - result of merging all contexts. - :param list group_by_seq: list of domains (which may be in a different - order than the ``contexts`` parameter), - evaluated in sequence, their ``'group_by'`` - key is extracted if they have one. - :returns: - a 3-dict of: - - context (``dict``) - the global context created by merging all of - ``contexts`` - - domain (``list``) - the concatenation of all domains - - group_by (``list``) - a list of fields to group by, potentially empty (in which case - no group by should be performed) - """ - context, domain = eval_context_and_domain(req.session, - nonliterals.CompoundContext(*(contexts or [])), - nonliterals.CompoundDomain(*(domains or []))) - - group_by_sequence = [] - for candidate in (group_by_seq or []): - ctx = req.session.eval_context(candidate, context) - group_by = ctx.get('group_by') - if not group_by: - continue - elif isinstance(group_by, basestring): - group_by_sequence.append(group_by) - else: - group_by_sequence.extend(group_by) - - return { - 'context': context, - 'domain': domain, - 'group_by': group_by_sequence - } - @openerpweb.jsonrequest def save_session_action(self, req, the_action): """ diff --git a/addons/web/doc/rpc.rst b/addons/web/doc/rpc.rst index db6fcb4fa4b..8c48a2f6eaa 100644 --- a/addons/web/doc/rpc.rst +++ b/addons/web/doc/rpc.rst @@ -257,16 +257,17 @@ method you want to call) and a mapping of attributes to values (applied as keyword arguments on the Python method [#]_). This function fetches the return value of the Python methods, converted to JSON. -For instance, to call the ``eval_domain_and_context`` of the -:class:`~web.controllers.main.Session` controller: +For instance, to call the ``resequence`` of the +:class:`~web.controllers.main.DataSet` controller: .. code-block:: javascript - openerp.connection.rpc('/web/session/eval_domain_and_context', { - domains: ds, - contexts: cs + openerp.connection.rpc('/web/dataset/resequence', { + model: some_model, + ids: array_of_ids, + offset: 42 }).then(function (result) { - // handle result + // resequenced on server }); .. [#] with a small twist: SQLAlchemy's ``orm.query.Query.group_by`` diff --git a/addons/web/static/src/js/corelib.js b/addons/web/static/src/js/corelib.js index 54eb6ce3e81..d05a42061de 100644 --- a/addons/web/static/src/js/corelib.js +++ b/addons/web/static/src/js/corelib.js @@ -965,15 +965,8 @@ instance.web.JsonRPC = instance.web.Class.extend(instance.web.PropertiesMixin, { var deferred = $.Deferred(); if (! options.shadow) this.trigger('request', url, payload); - var request; -// if (url.url === '/web/session/eval_domain_and_context') { -// // intercept eval_domain_and_context -// request = instance.web.pyeval.eval_domains_and_contexts( -// params) -// } else { - request = this.rpc_function(url, payload); -// } - request.then( + + this.rpc_function(url, payload).then( function (response, textStatus, jqXHR) { if (! options.shadow) self.trigger('response', response); diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index d00654fc8ef..e16880889d2 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -739,13 +739,13 @@ openerp.web.pyeval = function (instance) { instance.web.pyeval.eval_domains_and_contexts = function (source) { return new $.Deferred(function (d) {setTimeout(function () { try { - var contexts = ([instance.session.context] || []).concat(source.contexts); + var contexts = ([instance.session.user_context] || []).concat(source.contexts); // see Session.eval_context in Python - d.resolve({result: { + d.resolve({ context: instance.web.pyeval.eval('contexts', contexts), domain: instance.web.pyeval.eval('domains', source.domains), - group_by: instance.web.pyeval.eval('groupbys', source.group_by_seq) - }}, 'success', {}); + group_by: instance.web.pyeval.eval('groupbys', source.group_by_seq || []) + }); } catch (e) { d.resolve({ error: { code: 400, diff --git a/addons/web/static/src/js/search.js b/addons/web/static/src/js/search.js index 0f25b2484c3..e0a95df37ce 100644 --- a/addons/web/static/src/js/search.js +++ b/addons/web/static/src/js/search.js @@ -1614,7 +1614,7 @@ instance.web.search.CustomFilters = instance.web.search.Input.extend({ var set_as_default = this.$('#oe_searchview_custom_default').prop('checked'); var search = this.view.build_search_data(); - this.rpc('/web/session/eval_domain_and_context', { + instance.web.pyeval.eval_domains_and_contexts({ domains: search.domains, contexts: search.contexts, group_by_seq: search.groupbys || [] diff --git a/addons/web/static/src/js/view_form.js b/addons/web/static/src/js/view_form.js index c784b169d12..a2768e196c7 100644 --- a/addons/web/static/src/js/view_form.js +++ b/addons/web/static/src/js/view_form.js @@ -4588,7 +4588,7 @@ instance.web.form.SelectCreatePopup = instance.web.form.AbstractFormPopup.extend var self = this; this.init_dataset(); if (this.options.initial_view == "search") { - self.rpc('/web/session/eval_domain_and_context', { + instance.web.pyeval.eval_domains_and_contexts({ domains: [], contexts: [this.context] }).done(function (results) { @@ -4659,7 +4659,7 @@ instance.web.form.SelectCreatePopup = instance.web.form.AbstractFormPopup.extend }, do_search: function(domains, contexts, groupbys) { var self = this; - this.rpc('/web/session/eval_domain_and_context', { + instance.web.pyeval.eval_domains_and_contexts({ domains: domains || [], contexts: contexts || [], group_by_seq: groupbys || [] diff --git a/addons/web/static/src/js/view_tree.js b/addons/web/static/src/js/view_tree.js index 8265be77ba1..8fd3b25ade5 100644 --- a/addons/web/static/src/js/view_tree.js +++ b/addons/web/static/src/js/view_tree.js @@ -236,7 +236,7 @@ instance.web.TreeView = instance.web.View.extend(/** @lends instance.web.TreeVie if (action.context) { c.add(action.context); } - return self.rpc('/web/session/eval_domain_and_context', { + return instance.web.pyeval.eval_domains_and_contexts({ contexts: [c], domains: [] }).then(function (res) { action.context = res.context; diff --git a/addons/web/static/src/js/views.js b/addons/web/static/src/js/views.js index 6fbbd3199ad..4c6b422cfbb 100644 --- a/addons/web/static/src/js/views.js +++ b/addons/web/static/src/js/views.js @@ -396,7 +396,7 @@ instance.web.ActionManager = instance.web.Widget.extend({ ir_actions_report_xml: function(action, options) { var self = this; instance.web.blockUI(); - self.rpc("/web/session/eval_domain_and_context", { + instance.web.pyeval.eval_domains_and_contexts({ contexts: [action.context], domains: [] }).done(function(res) { @@ -667,7 +667,7 @@ instance.web.ViewManager = instance.web.Widget.extend({ var self = this, controller = this.views[this.active_view].controller, action_context = this.action.context || {}; - this.rpc('/web/session/eval_domain_and_context', { + instance.web.pyeval.eval_domains_and_contexts({ domains: [this.action.domain || []].concat(domains || []), contexts: [action_context].concat(contexts || []), group_by_seq: groupbys || [] @@ -1227,7 +1227,7 @@ instance.web.View = instance.web.Widget.extend({ }); } ncontext.add(action.context || {}); - return self.rpc('/web/session/eval_domain_and_context', { + return instance.web.pyeval.eval_domains_and_contexts({ contexts: [ncontext], domains: [] }).then(function (results) { diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 994c122f1e6..86da3ae2876 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -247,8 +247,13 @@ openerp.testing.section('eval.edc', { dependencies: ['web.data'], rpc: 'rpc', setup: function (instance) { + instance.session.user_context = { + lang: 'en_US', + tz: false, + uid: 1 + }; instance.edc = function (domains, contexts) { - return this.session.rpc('/web/session/eval_domain_and_context', { + return instance.web.pyeval.eval_domains_and_contexts({ contexts: contexts || [], domains: domains || [] }); @@ -382,9 +387,14 @@ openerp.testing.section('eval.edc.nonliterals', { dependencies: ['web.data'], rpc: 'rpc', setup: function (instance) { + instance.session.user_context = { + lang: 'en_US', + tz: false, + uid: 1 + }; _.extend(instance, { edc: function (domains, contexts) { - return this.session.rpc('/web/session/eval_domain_and_context', { + return instance.web.pyeval.eval_domains_and_contexts({ contexts: contexts || [], domains: domains || [] }); @@ -398,7 +408,7 @@ openerp.testing.section('eval.edc.nonliterals', { }); } }, function (test) { - test('domain with time', {asserts: 2}, function (instance) { + test('domain with time', {asserts: 1}, function (instance) { return instance.load_domain(0).then(function (d) { strictEqual(d.__ref, 'domain'); return instance.edc([ @@ -409,7 +419,7 @@ openerp.testing.section('eval.edc.nonliterals', { }, d, [['user_id', '=', 1]] - ]) + ]); }).then(function (result) { var d = new Date(); var today = _.str.sprintf("%04d-%02d-%02d",