diff options
| author | Rémi Rahir <rar@odoo.com> | 2020-03-12 14:27:36 +0000 |
|---|---|---|
| committer | Rémi Rahir <rar@odoo.com> | 2020-03-23 13:41:18 +0000 |
| commit | 5094cdc9f754f60cbd205193940be08053a5e84e (patch) | |
| tree | 2a8697b883dd4ee9eff60360ccfa6f215e27622e /addons/iap | |
| parent | 94fee8a8dd5d362594e3c3107e7475177ffed547 (diff) | |
[FIX] iap: limit force_create at last step
The modification introduced in 5307f47d975c3b3ebd87c7f7e2248802a1aa5be7
was not properly managing the 'force_create' argument. When it is set to
false, we only look for existing accounts with the current cursor and
end up returning an empty recordset when the account was created during
the same transaction. Ex:
// Do stuff
IapAcc = self.env['iap.account']
IapAcc.get('my_service')
IapAcc.get_credit('my_service')
// Other stuff
closes odoo/odoo#48201
X-original-commit: c78070dc22f99867b4b1f3a52cd11543da571fe4
Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
Diffstat (limited to 'addons/iap')
| -rw-r--r-- | addons/iap/models/iap.py | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/addons/iap/models/iap.py b/addons/iap/models/iap.py index fbecec856c6..4dfd08b88c0 100644 --- a/addons/iap/models/iap.py +++ b/addons/iap/models/iap.py @@ -171,20 +171,23 @@ class IapAccount(models.Model): ] accounts = self.search(domain, order='id desc') if not accounts: - if force_create: - with self.pool.cursor() as cr: - # Since the account did not exist yet, we will encounter a NoCreditError, - # which is going to rollback the database and undo the account creation, - # preventing the process to continue any further. - IapAccount = self.with_env(self.env(cr=cr)) - account = IapAccount.search(domain, order='id desc', limit=1) - if not account: - account = IapAccount.create({'service_name': service_name}) - # fetch 'account_token' into cache with this cursor, - # as self's cursor cannot see this account - account.account_token - return self.browse(account.id) - return accounts + with self.pool.cursor() as cr: + # Since the account did not exist yet, we will encounter a NoCreditError, + # which is going to rollback the database and undo the account creation, + # preventing the process to continue any further. + + # Flush the pending operations to avoid a deadlock. + self.flush() + IapAccount = self.with_env(self.env(cr=cr)) + account = IapAccount.search(domain, order='id desc', limit=1) + if not account: + if not force_create: + return account + account = IapAccount.create({'service_name': service_name}) + # fetch 'account_token' into cache with this cursor, + # as self's cursor cannot see this account + account.account_token + return self.browse(account.id) accounts_with_company = accounts.filtered(lambda acc: acc.company_ids) if accounts_with_company: return accounts_with_company[0] |
