From 7b711fd117a52ad073e06da34cd812ad790f7428 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 28 Mar 2011 18:39:17 +0200 Subject: [PATCH] [ADD] group_by support in search views (not handled yet by the view manager and list view, so they have no actual effect) bzr revid: xmo@openerp.com-20110328163917-5deq43o7jwotfpym --- addons/base/controllers/main.py | 47 ++++++++++++++++++++- addons/base/static/openerp/js/base_views.js | 23 +++++++--- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/addons/base/controllers/main.py b/addons/base/controllers/main.py index d38dce05fe1..097b9ab79e9 100644 --- a/addons/base/controllers/main.py +++ b/addons/base/controllers/main.py @@ -120,12 +120,55 @@ class Session(openerpweb.Controller): js.exposed = True @openerpweb.jsonrequest - def eval_domain_and_context(self, req, contexts, domains): + 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 = req.session.eval_contexts(contexts) domain = req.session.eval_domains(domains, context) + + 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 + 'domain': domain, + 'group_by': group_by_sequence } class Menu(openerpweb.Controller): diff --git a/addons/base/static/openerp/js/base_views.js b/addons/base/static/openerp/js/base_views.js index c1ac7897302..591b6857806 100644 --- a/addons/base/static/openerp/js/base_views.js +++ b/addons/base/static/openerp/js/base_views.js @@ -115,12 +115,14 @@ openerp.base.ViewManager = openerp.base.Controller.extend({ }, on_edit: function() { }, - do_search: function (domains, contexts) { + do_search: function (domains, contexts, groupbys) { var self = this; this.rpc('/base/session/eval_domain_and_context', { domains: domains, - contexts: contexts + contexts: contexts, + group_by_seq: groupbys }, function (results) { + // TODO: handle non-empty results.group_by with read_group self.dataset.set({ context: results.context, domain: results.domain @@ -539,7 +541,15 @@ openerp.base.SearchView = openerp.base.Controller.extend({ map(function (input) { return input.get_context(); }). compact(). value(); - this.on_search(domains, contexts); + + // TODO: do we need to handle *fields* with group_by in their context? + var groupbys = _(this.enabled_filters) + .chain() + .map(function (filter) { return filter.get_context();}) + .compact() + .value(); + + this.on_search(domains, contexts, groupbys); }, /** * Event hook for searches: triggers after the SearchView has collected @@ -549,10 +559,11 @@ openerp.base.SearchView = openerp.base.Controller.extend({ * may or may not be evaluated (each item can be either a valid domain or * context, or a string to evaluate in order in the sequence) * - * @param {Array} domains an array of string or literal domains - * @param {Array} contexts an array of string or literal contexts + * @param {Array} domains an array of literal domains or domain references + * @param {Array} contexts an array of literal contexts or context refs + * @param {Array} groupbys ordered contexts which may or may not have group_by keys */ - on_search: function (domains, contexts) { }, + on_search: function (domains, contexts, groupbys) { }, do_clear: function (e) { if (e && e.preventDefault) { e.preventDefault(); } this.on_clear();