diff options
| author | Raphael Collet <rco@odoo.com> | 2019-05-31 14:35:21 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2019-06-03 12:37:39 +0000 |
| commit | b415da5daacd51e1fec39b29a91ce2a715b0f193 (patch) | |
| tree | aad23cc373f353249733c99632a356ec2e3785fc | |
| parent | 0d88571faae0627e316c96cfb0e8f58f9aebd3c5 (diff) | |
[IMP] fields: add method to get a usable domain from a field
closes odoo/odoo#33816
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
| -rw-r--r-- | addons/portal/controllers/mail.py | 6 | ||||
| -rw-r--r-- | odoo/fields.py | 19 | ||||
| -rw-r--r-- | odoo/osv/expression.py | 10 |
3 files changed, 16 insertions, 19 deletions
diff --git a/addons/portal/controllers/mail.py b/addons/portal/controllers/mail.py index e9603edbaa9..39e83703a3c 100644 --- a/addons/portal/controllers/mail.py +++ b/addons/portal/controllers/mail.py @@ -126,9 +126,9 @@ class PortalChatter(http.Controller): domain = [] # Only search into website_message_ids, so apply the same domain to perform only one search # extract domain from the 'website_message_ids' field - field_domain = request.env[res_model]._fields['website_message_ids'].domain - if callable(field_domain): - field_domain = field_domain(request.env[res_model]) + model = request.env[res_model] + field = model._fields['website_message_ids'] + field_domain = field.get_domain_list(model) domain = expression.AND([domain, field_domain, [('res_id', '=', res_id)]]) # Check access diff --git a/odoo/fields.py b/odoo/fields.py index 8baf2fa2edd..222d42a3cda 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -2070,6 +2070,13 @@ class _Relational(Field): _logger.warning("Field %s with unknown comodel_name %r", self, self.comodel_name) self.comodel_name = '_unknown' + def get_domain_list(self, model): + """ Return a list domain from the domain parameter. """ + domain = self.domain + if callable(domain): + domain = domain(model) + return domain if isinstance(domain, list) else [] + @property def _related_domain(self): if callable(self.domain): @@ -2493,8 +2500,7 @@ class One2many(_RelationalMulti): comodel = records.env[self.comodel_name].with_context(**self.context) inverse = self.inverse_name get_id = (lambda rec: rec.id) if comodel._fields[inverse].type == 'many2one' else int - domain = self.domain(records) if callable(self.domain) else self.domain - domain = domain + [(inverse, 'in', records.ids)] + domain = self.get_domain_list(records) + [(inverse, 'in', records.ids)] lines = comodel.search(domain, limit=self.limit) # group lines by inverse field (without prefetching other fields) @@ -2568,8 +2574,7 @@ class One2many(_RelationalMulti): elif act[0] in (5, 6): flush() ids = act[2] if act[0] == 6 else [] - domain = self.domain(model) if callable(self.domain) else self.domain - domain = domain + [(inverse, 'in', records.ids)] + domain = self.get_domain_list(model) + [(inverse, 'in', records.ids)] if ids: domain = domain + [('id', 'not in', ids)] unlink(comodel.search(domain)._ids) @@ -2726,11 +2731,7 @@ class Many2many(_RelationalMulti): def read(self, records): comodel = records.env[self.comodel_name].with_context(**self.context) - - # String domains are supposed to be dynamic and evaluated on client-side - # only (thus ignored here). - domain = self.domain if isinstance(self.domain, list) else [] - + domain = self.get_domain_list(records) wquery = comodel._where_calc(domain) comodel._apply_ir_rules(wquery, 'read') order_by = comodel._generate_order_by(None, wquery) diff --git a/odoo/osv/expression.py b/odoo/osv/expression.py index b6638a5b3c8..f8a1c702758 100644 --- a/odoo/osv/expression.py +++ b/odoo/osv/expression.py @@ -889,7 +889,7 @@ class expression(object): elif len(path) > 1 and field.store and field.type == 'one2many' and field.auto_join: # res_partner.id = res_partner__bank_ids.partner_id leaf.add_join_context(comodel, 'id', field.inverse_name, path[0]) - domain = field.domain(model) if callable(field.domain) else field.domain + domain = field.get_domain_list(model) push(create_substitution_leaf(leaf, (path[1], operator, right), comodel)) if domain: domain = normalize_domain(domain) @@ -949,9 +949,7 @@ class expression(object): push(create_substitution_leaf(leaf, dom_leaf, model)) elif field.type == 'one2many': - domain = field.domain - if callable(domain): - domain = domain(model) + domain = field.get_domain_list(model) inverse_is_int = comodel._fields[field.inverse_name].type == 'integer' unwrap_inverse = (lambda ids: ids) if inverse_is_int else (lambda recs: recs.ids) @@ -1015,9 +1013,7 @@ class expression(object): elif right is not False: # determine ids2 in comodel if isinstance(right, str): - domain = field.domain - if callable(domain): - domain = domain(model) + domain = field.get_domain_list(model) op2 = (TERM_OPERATORS_NEGATION[operator] if operator in NEGATIVE_TERM_OPERATORS else operator) ids2 = [x[0] for x in comodel.name_search(right, domain or [], op2, limit=None)] |
