Skip to content

Commit

Permalink
[MERGE] forward port branch 10.0 up to 2343138
Browse files Browse the repository at this point in the history
  • Loading branch information
KangOl committed Apr 19, 2018
2 parents 67054b6 + 2343138 commit c638cb4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
19 changes: 19 additions & 0 deletions odoo/addons/base/tests/test_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ def read_group(interval):
['date:month', 'date:day'], lazy=False)
self.assertEqual(len(res), len(partner_ids))

# combine groupby and orderby
months = ['February 2013', 'January 2013', 'December 2012', 'November 2012']
res = partners.read_group([('id', 'in', partner_ids)], ['date'],
groupby=['date:month'], orderby='date:month DESC')
self.assertEqual([item['date:month'] for item in res], months)

# order by date should reorder by date:month
res = partners.read_group([('id', 'in', partner_ids)], ['date'],
groupby=['date:month'], orderby='date DESC')
self.assertEqual([item['date:month'] for item in res], months)

# order by date should reorder by date:day
days = ['11 Feb 2013', '28 Jan 2013', '14 Jan 2013', '07 Jan 2013',
'31 Dec 2012', '17 Dec 2012', '19 Nov 2012']
res = partners.read_group([('id', 'in', partner_ids)], ['date'],
groupby=['date:month', 'date:day'],
orderby='date DESC', lazy=False)
self.assertEqual([item['date:day'] for item in res], days)

def test_write_duplicate(self):
p1 = self.env['res.partner'].create({'name': 'W'})
(p1 + p1).write({'name': 'X'})
Expand Down
3 changes: 2 additions & 1 deletion odoo/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ class Second(models.Model):

'automatic': False, # whether the field is automatically created ("magic" field)
'inherited': False, # whether the field is inherited (_inherits)
'inherited_field': None, # the corresponding inherited field

'name': None, # name of the field
'model_name': None, # name of the model of this field
Expand Down Expand Up @@ -650,7 +651,7 @@ def _search_related(self, records, operator, value):
@property
def base_field(self):
""" Return the base field of an inherited field, or ``self``. """
return self.related_field.base_field if self.inherited else self
return self.inherited_field.base_field if self.inherited_field else self

#
# Company-dependent fields
Expand Down
20 changes: 14 additions & 6 deletions odoo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,31 +1744,38 @@ def _read_group_prepare(self, orderby, aggregated_fields, annotated_groupbys, qu
"""
orderby_terms = []
groupby_terms = [gb['qualified_field'] for gb in annotated_groupbys]
groupby_fields = [gb['groupby'] for gb in annotated_groupbys]
if not orderby:
return groupby_terms, orderby_terms

self._check_qorder(orderby)

# when a field is grouped as 'foo:bar', both orderby='foo' and
# orderby='foo:bar' generate the clause 'ORDER BY "foo:bar"'
groupby_fields = {
gb[key]: gb['groupby']
for gb in annotated_groupbys
for key in ('field', 'groupby')
}
for order_part in orderby.split(','):
order_split = order_part.split()
order_field = order_split[0]
if order_field == 'id' or order_field in groupby_fields:

if self._fields[order_field.split(':')[0]].type == 'many2one':
order_clause = self._generate_order_by(order_part, query).replace('ORDER BY ', '')
if order_clause:
orderby_terms.append(order_clause)
groupby_terms += [order_term.split()[0] for order_term in order_clause.split(',')]
else:
order = '"%s" %s' % (order_field, '' if len(order_split) == 1 else order_split[1])
orderby_terms.append(order)
order_split[0] = '"%s"' % groupby_fields.get(order_field, order_field)
orderby_terms.append(' '.join(order_split))
elif order_field in aggregated_fields:
order_split[0] = '"' + order_field + '"'
order_split[0] = '"%s"' % order_field
orderby_terms.append(' '.join(order_split))
else:
# Cannot order by a field that will not appear in the results (needs to be grouped or aggregated)
_logger.warn('%s: read_group order by `%s` ignored, cannot sort on empty columns (not grouped/aggregated)',
self._name, order_part)

return groupby_terms, orderby_terms

@api.model
Expand Down Expand Up @@ -2739,6 +2746,7 @@ def _add_inherited_fields(self):
# - copy inherited fields iff their original field is copied
fields[name] = field.new(
inherited=True,
inherited_field=field,
related=(parent_field, name),
related_sudo=False,
copy=field.copy,
Expand Down Expand Up @@ -2855,7 +2863,7 @@ def _setup_fields(self, partial):
try:
field.setup_full(self)
except Exception:
if partial and field.manual:
if partial and field.base_field.manual:
# Something goes wrong when setup a manual field.
# This can happen with related fields using another manual many2one field
# that hasn't been loaded because the comodel does not exist yet.
Expand Down

0 comments on commit c638cb4

Please sign in to comment.