mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
[IMP] *_online_paypal: use hash for unique_import_id
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user