[MRG] merge with lp:openobjet-addons
authorTurkesh Patel (Open ERP) <tpa@tinyerp.com>
Wed, 11 Dec 2013 13:23:26 +0000 (18:53 +0530)
committerTurkesh Patel (Open ERP) <tpa@tinyerp.com>
Wed, 11 Dec 2013 13:23:26 +0000 (18:53 +0530)
bzr revid: tpa@tinyerp.com-20131211132326-2hya21cd9td46m3b

1  2 
addons/mail/mail_thread.py

@@@ -113,11 -113,10 +113,11 @@@ class mail_thread(osv.AbstractModel)
                      object_id.alias_id.alias_model_id.model == self._name and \
                      object_id.alias_id.alias_force_thread_id == 0:
                  alias = object_id.alias_id
 -        elif catchall_domain and model:  # no specific res_id given -> generic help message, take an example alias (i.e. alias of some section_id)
 +
 +        if catchall_domain and model and not alias:  #check for example alias if res_id not given or given res_id dose not contain alias-> generic help message, take an example alias (i.e. alias of some section_id)
              model_id = self.pool.get('ir.model').search(cr, uid, [("model", "=", self._name)], context=context)[0]
              alias_obj = self.pool.get('mail.alias')
 -            alias_ids = alias_obj.search(cr, uid, [("alias_model_id", "=", model_id), ("alias_name", "!=", False), ('alias_force_thread_id', '=', 0)], context=context, order='id ASC')
 +            alias_ids = alias_obj.search(cr, uid, [("alias_model_id", "=", model_id), ("alias_name", "!=", False), ('alias_force_thread_id', '=', False), ('alias_parent_thread_id', '=', False)], context=context, order='id ASC')
              if alias_ids and len(alias_ids) == 1:  # if several aliases -> incoherent to propose one guessed from nowhere, therefore avoid if several aliases
                  alias = alias_obj.browse(cr, uid, alias_ids[0], context=context)
  
                  new = set(command[2])
  
          # remove partners that are no longer followers
-         fol_ids = fol_obj.search(cr, SUPERUSER_ID,
-             [('res_model', '=', self._name), ('res_id', '=', id), ('partner_id', 'not in', list(new))])
-         fol_obj.unlink(cr, SUPERUSER_ID, fol_ids)
+         self.message_unsubscribe(cr, uid, [id], list(old-new))
  
          # add new followers
-         for partner_id in new - old:
-             fol_obj.create(cr, SUPERUSER_ID, {'res_model': self._name, 'res_id': id, 'partner_id': partner_id})
+         self.message_subscribe(cr, uid, [id], list(new-old))
  
      def _search_followers(self, cr, uid, obj, name, args, context):
          """Search function for message_follower_ids
          """
          if context is None:
              context = {}
          thread_id = super(mail_thread, self).create(cr, uid, values, context=context)
  
          # automatic logging unless asked not to (mainly for various testing purpose)
          # subscribe uid unless asked not to
          if not context.get('mail_create_nosubscribe'):
              self.message_subscribe_users(cr, uid, [thread_id], [uid], context=context)
          # auto_subscribe: take values and defaults into account
          create_values = dict(values)
          for key, val in context.iteritems():
          else:
              self.check_access_rights(cr, uid, 'write')
  
-         for record in self.browse(cr, SUPERUSER_ID, ids, context=context):
-             existing_pids = set([f.id for f in record.message_follower_ids
-                                             if f.id in partner_ids])
+         existing_pids_dict = {}
+         fol_ids = mail_followers_obj.search(cr, SUPERUSER_ID, [('res_model', '=', self._name), ('res_id', 'in', ids)])
+         for fol in mail_followers_obj.browse(cr, SUPERUSER_ID, fol_ids, context=context):
+             existing_pids_dict.setdefault(fol.res_id, set()).add(fol.partner_id.id)
+         # subtype_ids specified: update already subscribed partners
+         if subtype_ids and fol_ids:
+             mail_followers_obj.write(cr, SUPERUSER_ID, fol_ids, {'subtype_ids': [(6, 0, subtype_ids)]}, context=context)
+         # subtype_ids not specified: do not update already subscribed partner, fetch default subtypes for new partners
+         if subtype_ids is None:
+             subtype_ids = subtype_obj.search(
+                 cr, uid, [
+                     ('default', '=', True), '|', ('res_model', '=', self._name), ('res_model', '=', False)], context=context)
+         for id in ids:
+             existing_pids = existing_pids_dict.get(id, set())
              new_pids = set(partner_ids) - existing_pids
  
-             # subtype_ids specified: update already subscribed partners
-             if subtype_ids and existing_pids:
-                 fol_ids = mail_followers_obj.search(cr, SUPERUSER_ID, [
-                                                         ('res_model', '=', self._name),
-                                                         ('res_id', '=', record.id),
-                                                         ('partner_id', 'in', list(existing_pids)),
-                                                     ], context=context)
-                 mail_followers_obj.write(cr, SUPERUSER_ID, fol_ids, {'subtype_ids': [(6, 0, subtype_ids)]}, context=context)
-             # subtype_ids not specified: do not update already subscribed partner, fetch default subtypes for new partners
-             elif subtype_ids is None:
-                 subtype_ids = subtype_obj.search(cr, uid, [
-                                                         ('default', '=', True),
-                                                         '|',
-                                                         ('res_model', '=', self._name),
-                                                         ('res_model', '=', False)
-                                                     ], context=context)
              # subscribe new followers
              for new_pid in new_pids:
-                 mail_followers_obj.create(cr, SUPERUSER_ID, {
-                                                 'res_model': self._name,
-                                                 'res_id': record.id,
-                                                 'partner_id': new_pid,
-                                                 'subtype_ids': [(6, 0, subtype_ids)],
-                                             }, context=context)
+                 mail_followers_obj.create(
+                     cr, SUPERUSER_ID, {
+                         'res_model': self._name,
+                         'res_id': id,
+                         'partner_id': new_pid,
+                         'subtype_ids': [(6, 0, subtype_ids)],
+                     }, context=context)
  
          return True
  
              self.check_access_rights(cr, uid, 'read')
          else:
              self.check_access_rights(cr, uid, 'write')
-         return self.write(cr, SUPERUSER_ID, ids, {'message_follower_ids': [(3, pid) for pid in partner_ids]}, context=context)
+         fol_obj = self.pool['mail.followers']
+         fol_ids = fol_obj.search(
+             cr, SUPERUSER_ID, [
+                 ('res_model', '=', self._name),
+                 ('res_id', 'in', ids),
+                 ('partner_id', 'in', partner_ids)
+             ], context=context)
+         return fol_obj.unlink(cr, SUPERUSER_ID, fol_ids, context=context)
  
      def _message_get_auto_subscribe_fields(self, cr, uid, updated_fields, auto_follow_fields=['user_id'], context=None):
          """ Returns the list of relational fields linking to res.users that should