← Back to team overview

openerp-expert-framework team mailing list archive

[Merge] lp:~numerigraphe/openobject-server/trunk-read_group-having into lp:openobject-server

 

Numérigraphe has proposed merging lp:~numerigraphe/openobject-server/trunk-read_group-having into lp:openobject-server.

Requested reviews:
  OpenERP Framework Experts (openerp-expert-framework)
  OpenERP Core Team (openerp)

For more details, see:
https://code.launchpad.net/~numerigraphe/openobject-server/trunk-read_group-having/+merge/104579

This branch adds a new parameter named "having" to orm.read_group(), that lets clients pass the information to filter the grouped records based on the sum (or avg, etc.) of a value.
This parameter should contain a domain-like list of tuples, that the ORM will use to add a "HAVING" clause to the SQL query.

For an example of how this feature is useful,  please see lp:~numerigraphe/openobject-addons/trunk-stock-analysis-filter-zero, where we use the "having" parameter to filter out results with quantity = 0 in stock analysis. This can only be done after the records have been grouped (the goods may have gone in then out of a location).

This is only really useful if clients support it too. Support for the GTK client has been added in lp:~numerigraphe/openobject-client/trunk-read_group-having.

Lionel Sausin & Philippe Rossi.
-- 
https://code.launchpad.net/~numerigraphe/openobject-server/trunk-read_group-having/+merge/104579
Your team OpenERP Framework Experts is requested to review the proposed merge of lp:~numerigraphe/openobject-server/trunk-read_group-having into lp:openobject-server.
=== modified file 'openerp/osv/orm.py'
--- openerp/osv/orm.py	2012-04-18 09:07:35 +0000
+++ openerp/osv/orm.py	2012-05-03 15:58:22 +0000
@@ -2458,7 +2458,7 @@
                 append_right(all_groups.pop(0))
         return result
 
-    def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
+    def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, having=False):
         """
         Get the list of records in list view grouped by the given ``groupby`` fields
 
@@ -2545,7 +2545,15 @@
         offset_str = offset and ' offset %d' % offset or ''
         if len(groupby_list) < 2 and context.get('group_by_no_leaf'):
             group_count = '_'
-        cr.execute('SELECT min(%s.id) AS id, count(%s.id) AS %s_count' % (self._table, self._table, group_count) + (flist and ',') + flist + ' FROM ' + from_clause + where_clause + gb + limit_str + offset_str, where_clause_params)
+
+        having_conditions = []
+        for field, operator, value in having:
+            group_operator = fget[field].get('group_operator', 'sum')
+            having_conditions.append('%s(%s) %s' % (group_operator, field, operator) + ' %s')
+            where_clause_params.append(value)
+        having_clause = having_conditions and (' HAVING ' + ' AND '.join(having_conditions)) or ''
+                   
+        cr.execute('SELECT min(%s.id) AS id, count(%s.id) AS %s_count' % (self._table, self._table, group_count) + (flist and ',') + flist + ' FROM ' + from_clause + where_clause + gb + having_clause + limit_str + offset_str, where_clause_params)
         alldata = {}
         groupby = group_by
         for r in cr.dictfetchall():


Follow ups