diff --git a/base_partner_merge/base_partner_merge.py b/base_partner_merge/models/base_partner_merge.py similarity index 100% rename from base_partner_merge/base_partner_merge.py rename to base_partner_merge/models/base_partner_merge.py diff --git a/base_partner_merge/models/base_partner_merge_automatic_wizard.py b/base_partner_merge/models/base_partner_merge_automatic_wizard.py index a211f02c4..0b01e7585 100644 --- a/base_partner_merge/models/base_partner_merge_automatic_wizard.py +++ b/base_partner_merge/models/base_partner_merge_automatic_wizard.py @@ -17,13 +17,14 @@ class MergePartnerAutomatic(models.TransientModel): ret = super(MergePartnerAutomatic, self)._process_query(query) # If 'extra_domain', deduplicate only the records matching the domain - extra_domain = self.env.context.get('extra_domain', []) + extra_domain = self.env.context.get('partner_merge_domain', []) if extra_domain: for line in self.line_ids: - aggr_ids = literal_eval(line.aggr_ids) - domain = [('id', 'in', aggr_ids)] + domain = [('id', 'in', literal_eval(line.aggr_ids))] domain.extend(extra_domain) - records = self.env['res.partner'].search(domain) - if len(records) < len(aggr_ids): + aggr_ids = self.env['res.partner'].search(domain).ids + if len(aggr_ids) > 1: + line.aggr_ids = str(aggr_ids) + else: line.unlink() return ret diff --git a/base_partner_merge/models/res_partner.py b/base_partner_merge/models/res_partner.py index cca006ab4..badcc6e60 100644 --- a/base_partner_merge/models/res_partner.py +++ b/base_partner_merge/models/res_partner.py @@ -5,17 +5,17 @@ from openerp import models, api -class ResPartner(models.Model): +class ResPartnerChanges(models.Model): _inherit = 'res.partner' @api.model - def deduplicate_on_field(self, field, domain=[]): - """ Merge contacts""" - self.merge_wizard = \ - self.env['base.partner.merge.automatic.wizard'] - wizard_id = self.merge_wizard.with_context( - extra_domain=domain).create({ - 'group_by_%s' % (field,): True, - 'state': 'option' - }) - wizard_id.automatic_process_cb() + def deduplicate_on_fields(self, fields_list, domain=None): + """ Merge contacts """ + wizard_obj = self.env['base.partner.merge.automatic.wizard'] + if domain: + wizard_obj = wizard_obj.with_context(partner_merge_domain=domain) + params = {'state': 'option'} + for field in fields_list: + params['group_by_%s' % (field,)] = True + wizard = wizard_obj.create(params) + wizard.automatic_process_cb() diff --git a/base_partner_merge/tests/test_merge.py b/base_partner_merge/tests/test_merge.py index ebb86223b..063b2ca41 100644 --- a/base_partner_merge/tests/test_merge.py +++ b/base_partner_merge/tests/test_merge.py @@ -33,32 +33,38 @@ class PartnerMergeTestCase(TransactionCase): field2: value2, }) - def test_10_all_functionality(self): - """ All functionality """ - - # Create users with duplicate names + def test_10_name_merge(self): + """ Merge users with duplicate names """ self._unlink_all() - self._create_duplicates('name', 'Donald Duck', 'email', - ['donald@therp.nl', 'donald@sunflowerweb.nl']) - self._create_duplicates('name', 'Mickey Mouse', 'email', - ['mickey@therp.nl', 'mickey@sunflowerweb.nl']) - # Test if there are two Donald Ducks and Mickey Mouses + self._create_duplicates('name', 'Donald Duck', + 'email', ['donald@therp.nl', 'donald@sunflowerweb.nl']) + self._create_duplicates('name', 'Mickey Mouse', + 'email', ['mickey@therp.nl', 'mickey@sunflowerweb.nl']) self._count_donalds_mickeys(2, 2) - # Merge all names that start with 'D', - self.partner.deduplicate_on_field('name', - domain=[('name', 'like', 'D%')]) - # Test if there is one Donald but still two Mickeys + self.partner.deduplicate_on_fields(['name'], + domain=[('name', 'like', 'D%')]) self._count_donalds_mickeys(1, 2) - # Create users with duplicate references + def test_20_ref_merge(self): + """ Merge users with duplicate references """ self._unlink_all() self._create_duplicates('ref', 'DD123', - 'name', ['Donald Duck', 'Mickey Mouse']) - + 'name', ['Donald Duck', 'Mickey Mouse']) # Merge on reference, leaving out guys that have no ref - self.partner.deduplicate_on_field('ref', - domain=[('ref', '!=', False)]) + self.partner.deduplicate_on_fields(['ref'], + domain=[('ref', '!=', False)]) # Test if only one remains after - self.assertEquals(len(self.partner.search([ - ('ref', '=', 'DD123')])), 1) + partners = self.partner.search([('ref', '=', 'DD123')]) + self.assertEquals(len(partners), 1) + + def test_30_ref_merge(self): + """ Fringe case: three guys, two to merge """ + self._unlink_all() + self._create_duplicates('ref', 'DD123', + 'name', ['Donald Duck', 'Donald Duck', 'Mickey Mouse']) + self.partner.deduplicate_on_fields(['ref'], + domain=[('name', '=', 'Donald Duck')]) + self._count_donalds_mickeys(1, 1) + +