From 2a16219ad0bd66a5fc7f0ae4852d4df3798cac8c Mon Sep 17 00:00:00 2001 From: alt-odoo Date: Wed, 24 Aug 2022 19:47:07 +0000 Subject: [PATCH] [IMP] iap: add automatic deletion of accounts without token Since the accounts without token are useless and could create errors afterwards, we should delete them and automatically create a new one with a new token in case force_create is set to True. closes odoo/odoo#99155 X-original-commit: 637df81a22bf4adbb685f78a59072b110c30a546 Signed-off-by: Florian Daloze (fda) Signed-off-by: Alex Thuyls (alt) --- addons/iap/models/iap_account.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/addons/iap/models/iap_account.py b/addons/iap/models/iap_account.py index aa920cb7933..c9450a6eb7d 100644 --- a/addons/iap/models/iap_account.py +++ b/addons/iap/models/iap_account.py @@ -31,6 +31,17 @@ class IapAccount(models.Model): ('company_ids', '=', False) ] accounts = self.search(domain, order='id desc') + accounts_without_token = accounts.filtered(lambda acc: not acc.account_token) + if accounts_without_token: + with self.pool.cursor() as cr: + # In case of a further error that will rollback the database, we should + # use a different SQL cursor to avoid undo the accounts deletion. + + # Flush the pending operations to avoid a deadlock. + self.flush() + IapAccount = self.with_env(self.env(cr=cr)) + IapAccount.search(domain + [('account_token', '=', False)]).unlink() + accounts = accounts - accounts_without_token if not accounts: with self.pool.cursor() as cr: # Since the account did not exist yet, we will encounter a NoCreditError,