[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>
This commit is contained in:
Rémi Rahir
2020-03-23 13:41:18 +00:00
parent 94fee8a8dd
commit 5094cdc9f7
+17 -14
View File
@@ -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]