Private methods for scripts

Use sudo() in scripts that require admin access
This commit is contained in:
Alexis de Lattre
2019-02-25 22:02:05 +01:00
parent 6ce749c70f
commit 6f0f0b0a0d
2 changed files with 11 additions and 10 deletions

View File

@@ -121,7 +121,8 @@ class AccountInvoice(models.Model):
lines.unlink()
return True
def fix_invoice_attachment_filename(self):
@api.model
def _fix_invoice_attachment_filename(self):
# This script is designed to fix attachment of invoices
# badly generated by Odoo v8. I found this problem in Nov 2018 at
# Encres Dubuit when investigating a bug where Odoo would create a
@@ -240,19 +241,19 @@ class AccountAccount(models.Model):
return super(AccountAccount, self).name_get()
# https://github.com/odoo/odoo/issues/23040
def fix_bank_account_types(self):
aao = self.env['account.account']
@api.model
def _fix_bank_account_types(self):
companies = self.env['res.company'].search([])
if len(companies) > 1 and self.env.user.id != SUPERUSER_ID:
raise UserError(
"In multi-company setups, you should run this "
"script as admin user")
logger.info(
"Multi-company setup detected, running script with sudo ")
self = self.sudo()
logger.info("START the script 'fix bank and cash account types'")
bank_type = self.env.ref('account.data_account_type_liquidity')
asset_type = self.env.ref('account.data_account_type_current_assets')
journals = self.env['account.journal'].search(
[('type', 'in', ('bank', 'cash'))], order='company_id')
journal_accounts_bank_type = aao
journal_accounts_bank_type = self
for journal in journals:
for account in [
journal.default_credit_account_id,
@@ -266,7 +267,7 @@ class AccountAccount(models.Model):
account.company_id.display_name, account.code)
if account not in journal_accounts_bank_type:
journal_accounts_bank_type += account
accounts = aao.search([
accounts = self.search([
('user_type_id', '=', bank_type.id)], order='company_id, code')
for account in accounts:
if account not in journal_accounts_bank_type:
@@ -278,7 +279,7 @@ class AccountAccount(models.Model):
return True
@api.model
def create_account_groups(self, level=2, name_prefix=u'Comptes '):
def _create_account_groups(self, level=2, name_prefix=u'Comptes '):
'''Should be launched by a script. Make sure the account_group module is installed
(the account_usability module doesn't depend on it currently'''
assert level >= 1

View File

@@ -25,7 +25,7 @@ class ResUsers(models.Model):
@api.model
def _script_partners_linked_to_users_no_company(self):
if self.env.user.id != SUPERUSER_ID:
raise UserError(_('You must run this script as admin user'))
self = self.sudo()
logger.info(
'START to set company_id=False on partners related to users')
users = self.search(