diff --git a/account_bank_statement_import_online_paypal/models/online_bank_statement_provider_paypal.py b/account_bank_statement_import_online_paypal/models/online_bank_statement_provider_paypal.py index a0d54482..e64ff0c3 100644 --- a/account_bank_statement_import_online_paypal/models/online_bank_statement_provider_paypal.py +++ b/account_bank_statement_import_online_paypal/models/online_bank_statement_provider_paypal.py @@ -223,6 +223,9 @@ class OnlineBankStatementProviderPayPal(models.Model): map(lambda x: self._paypal_transaction_to_lines(x), transactions) ) ) + # Make sure there are no duplicate unique_import_id's within a download. + # This way we still guard against importing twice the same data. + self._paypal_ensure_unique_import_ids(lines) first_transaction = transactions[0] first_transaction_id = first_transaction["transaction_info"]["transaction_id"] @@ -319,6 +322,17 @@ class OnlineBankStatementProviderPayPal(models.Model): ] return lines + def _paypal_ensure_unique_import_ids(self, lines): + """Check id's are unique within a list of lines.""" + check_ids = {} + for line in lines: + unique_import_id = line["unique_import_id"] + counter = 0 + if unique_import_id in check_ids: + counter = check_ids[unique_import_id] + 1 + line["unique_import_id"] = "%s-%d" % (unique_import_id, counter) + check_ids[unique_import_id] = counter + def _paypal_get_token(self): self.ensure_one() data = self._paypal_retrieve( diff --git a/account_bank_statement_import_online_paypal/tests/test_account_bank_statement_import_online_paypal.py b/account_bank_statement_import_online_paypal/tests/test_account_bank_statement_import_online_paypal.py index 5adf7f48..a0ea5da3 100644 --- a/account_bank_statement_import_online_paypal/tests/test_account_bank_statement_import_online_paypal.py +++ b/account_bank_statement_import_online_paypal/tests/test_account_bank_statement_import_online_paypal.py @@ -805,3 +805,16 @@ class TestAccountBankAccountStatementImportOnlinePayPal(common.TransactionCase): "unique_import_id": "1234567890-%s" % (self.today_timestamp,), }, ) + + def test_ensure_unique(self): + lines = [ + {"unique_import_id": "A", "name": "0 A key"}, + {"unique_import_id": "A", "name": "1 A key 1"}, + {"unique_import_id": "B", "name": "2 B key"}, + {"unique_import_id": "A", "name": "3 A key 2"}, + ] + self.OnlineBankStatementProvider._paypal_ensure_unique_import_ids(lines) + self.assertEqual(lines[0]["unique_import_id"], "A") + self.assertEqual(lines[1]["unique_import_id"], "A-1") + self.assertEqual(lines[2]["unique_import_id"], "B") + self.assertEqual(lines[3]["unique_import_id"], "A-2")