From d6f375df61b91761d92c0448572178a93ce74ca7 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Fri, 22 Aug 2014 16:14:58 +0200 Subject: [PATCH] [IMP] models: "X in self" is now equivalent to any(X == rec for rec in self) Fix modules with code like "record.id in other.stuff_ids". --- addons/portal_sale/portal_sale.py | 8 ++++---- addons/product_email_template/models/invoice.py | 2 +- openerp/models.py | 17 ++++++++++------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/addons/portal_sale/portal_sale.py b/addons/portal_sale/portal_sale.py index 31a2e1fb61c..6bc44b5dc02 100644 --- a/addons/portal_sale/portal_sale.py +++ b/addons/portal_sale/portal_sale.py @@ -62,7 +62,7 @@ class sale_order(osv.Model): assert len(ids) == 1 document = self.browse(cr, uid, ids[0], context=context) partner = document.partner_id - if partner.id not in document.message_follower_ids: + if partner not in document.message_follower_ids: self.message_subscribe(cr, uid, ids, [partner.id], context=context) return super(sale_order, self).action_button_confirm(cr, uid, ids, context=context) @@ -121,7 +121,7 @@ class account_invoice(osv.Model): # fetch the partner's id and subscribe the partner to the invoice for invoice in self.browse(cr, uid, ids, context=context): partner = invoice.partner_id - if partner.id not in invoice.message_follower_ids: + if partner not in invoice.message_follower_ids: self.message_subscribe(cr, uid, [invoice.id], [partner.id], context=context) return super(account_invoice, self).invoice_validate(cr, uid, ids, context=context) @@ -150,10 +150,10 @@ class mail_mail(osv.osv): order = so_obj.browse(cr, uid, mail.res_id, context=context) partner = order.partner_id # Add the customer in the SO as follower - if partner.id not in order.message_follower_ids: + if partner not in order.message_follower_ids: so_obj.message_subscribe(cr, uid, [mail.res_id], [partner.id], context=context) # Add all recipients of the email as followers for p in mail.partner_ids: - if p.id not in order.message_follower_ids: + if p not in order.message_follower_ids: so_obj.message_subscribe(cr, uid, [mail.res_id], [p.id], context=context) return super(mail_mail, self)._postprocess_sent_message(cr, uid, mail=mail, context=context, mail_sent=mail_sent) diff --git a/addons/product_email_template/models/invoice.py b/addons/product_email_template/models/invoice.py index d5876b510f1..60d7c4c61ef 100644 --- a/addons/product_email_template/models/invoice.py +++ b/addons/product_email_template/models/invoice.py @@ -13,7 +13,7 @@ class account_invoice(osv.Model): if invoice.type != 'out_invoice': continue # subscribe the partner to the invoice - if invoice.partner_id.id not in invoice.message_follower_ids: + if invoice.partner_id not in invoice.message_follower_ids: self.message_subscribe(cr, uid, [invoice.id], [invoice.partner_id.id], context=context) for line in invoice.invoice_line: if line.product_id.email_template_id: diff --git a/openerp/models.py b/openerp/models.py index 121186cada4..33bf9ba734e 100644 --- a/openerp/models.py +++ b/openerp/models.py @@ -5308,14 +5308,17 @@ class BaseModel(object): yield self._browse(self.env, (id,)) def __contains__(self, item): - """ Test whether `item` is a subset of `self` or a field name. """ - if isinstance(item, BaseModel): - if self._name == item._name: - return set(item._ids) <= set(self._ids) - raise except_orm("ValueError", "Mixing apples and oranges: %s in %s" % (item, self)) - if isinstance(item, basestring): + """ Test whether `item` (record or field name) is an element of `self`. + In the first case, the test is fully equivalent to:: + + any(item == record for record in self) + """ + if isinstance(item, BaseModel) and self._name == item._name: + return len(item) == 1 and item.id in self._ids + elif isinstance(item, basestring): return item in self._fields - return item in self.ids + else: + raise except_orm("ValueError", "Mixing apples and oranges: %s in %s" % (item, self)) def __add__(self, other): """ Return the concatenation of two recordsets. """