2
0

[FIX] account_chart_update: Don't cache recordsets

When marking methods as ormcache, you can't return recordsets, as they keep
the cursor information, and that cursor can be closed (worker spawn for example)
when you need to access again data.

This is fixed returning IDs instead, and browsing when needed
This commit is contained in:
Pedro M. Baeza 2018-09-18 14:59:54 +02:00 committed by Luis J. Salvatierra
parent 45e6abae56
commit 7c52e09c8d

View File

@ -262,7 +262,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
("type_tax_use", "=", template.type_tax_use)], ("type_tax_use", "=", template.type_tax_use)],
limit=1) limit=1)
result |= single result |= single
return result return result[:1].id
@api.model @api.model
@tools.ormcache("code") @tools.ormcache("code")
@ -276,7 +276,8 @@ class WizardUpdateChartsAccounts(models.TransientModel):
"""Find an account that matches the template.""" """Find an account that matches the template."""
return self.env['account.account'].search( return self.env['account.account'].search(
[('code', 'in', map(self.padded_code, templates.mapped("code"))), [('code', 'in', map(self.padded_code, templates.mapped("code"))),
('company_id', '=', self.company_id.id)]) ('company_id', '=', self.company_id.id)],
).id
@api.multi @api.multi
@tools.ormcache("templates") @tools.ormcache("templates")
@ -284,27 +285,27 @@ class WizardUpdateChartsAccounts(models.TransientModel):
"""Find a real fiscal position from a template.""" """Find a real fiscal position from a template."""
return self.env['account.fiscal.position'].search( return self.env['account.fiscal.position'].search(
[('name', 'in', templates.mapped("name")), [('name', 'in', templates.mapped("name")),
('company_id', '=', self.company_id.id)], limit=1) ('company_id', '=', self.company_id.id)], limit=1).id
@api.multi @api.multi
@tools.ormcache("templates", "current_fp_accounts") @tools.ormcache("templates", "current_fp_accounts")
def find_fp_account_by_templates(self, templates, current_fp_accounts): def find_fp_account_by_templates(self, templates, current_fp_accounts):
result = [] result = []
for tpl in templates: for tpl in templates:
pos = self.find_fp_by_templates(tpl.position_id) pos_id = self.find_fp_by_templates(tpl.position_id)
src = self.find_account_by_templates(tpl.account_src_id) src_id = self.find_account_by_templates(tpl.account_src_id)
dest = self.find_account_by_templates(tpl.account_dest_id) dest_id = self.find_account_by_templates(tpl.account_dest_id)
mappings = self.env["account.fiscal.position.account"].search([ mappings = self.env["account.fiscal.position.account"].search([
("position_id", "=", pos.id), ("position_id", "=", pos_id),
("account_src_id", "=", src.id), ("account_src_id", "=", src_id),
]) ])
existing = mappings.filtered(lambda x: x.account_dest_id == dest) existing = mappings.filtered(lambda x: x.account_dest_id == dest)
if not existing: if not existing:
# create a new mapping # create a new mapping
result.append((0, 0, { result.append((0, 0, {
'position_id': pos.id, 'position_id': pos_id,
'account_src_id': src.id, 'account_src_id': src_id,
'account_dest_id': dest.id, 'account_dest_id': dest_id,
})) }))
else: else:
current_fp_accounts -= existing current_fp_accounts -= existing
@ -318,20 +319,20 @@ class WizardUpdateChartsAccounts(models.TransientModel):
def find_fp_tax_by_templates(self, templates, current_fp_taxes): def find_fp_tax_by_templates(self, templates, current_fp_taxes):
result = [] result = []
for tpl in templates: for tpl in templates:
pos = self.find_fp_by_templates(tpl.position_id) pos_id = self.find_fp_by_templates(tpl.position_id)
src = self.find_tax_by_templates(tpl.tax_src_id) src_id = self.find_tax_by_templates(tpl.tax_src_id)
dest = self.find_tax_by_templates(tpl.tax_dest_id) dest_id = self.find_tax_by_templates(tpl.tax_dest_id)
mappings = self.env["account.fiscal.position.tax"].search([ mappings = self.env["account.fiscal.position.tax"].search([
("position_id", "=", pos.id), ("position_id", "=", pos_id),
("tax_src_id", "=", src.id), ("tax_src_id", "=", src_id),
]) ])
existing = mappings.filtered(lambda x: x.tax_dest_id == dest) existing = mappings.filtered(lambda x: x.tax_dest_id.id == dest_id)
if not existing: if not existing:
# create a new mapping # create a new mapping
result.append((0, 0, { result.append((0, 0, {
'position_id': pos.id, 'position_id': pos_id,
'tax_src_id': src.id, 'tax_src_id': src_id,
'tax_dest_id': dest.id, 'tax_dest_id': dest_id,
})) }))
else: else:
current_fp_taxes -= existing current_fp_taxes -= existing
@ -461,15 +462,15 @@ class WizardUpdateChartsAccounts(models.TransientModel):
@api.multi @api.multi
def _find_taxes(self): def _find_taxes(self):
"""Search for, and load, tax templates to create/update/delete.""" """Search for, and load, tax templates to create/update/delete."""
found_taxes = self.env["account.tax"] found_taxes_ids = []
self.tax_ids.unlink() self.tax_ids.unlink()
# Search for changes between template and real tax # Search for changes between template and real tax
for template in self.chart_template_ids.mapped("tax_template_ids"): for template in self.chart_template_ids.mapped("tax_template_ids"):
# Check if the template matches a real tax # Check if the template matches a real tax
tax = self.find_tax_by_templates(template) tax_id = self.find_tax_by_templates(template)
if not tax: if not tax_id:
# Tax to be created # Tax to be created
self.tax_ids.create({ self.tax_ids.create({
'tax_id': template.id, 'tax_id': template.id,
@ -478,9 +479,10 @@ class WizardUpdateChartsAccounts(models.TransientModel):
'notes': _('Name or description not found.'), 'notes': _('Name or description not found.'),
}) })
else: else:
found_taxes |= tax found_taxes_ids.append(tax_id)
# Check the tax for changes # Check the tax for changes
tax = self.env['account.tax'].browse(tax_id)
notes = self.diff_notes(template, tax) notes = self.diff_notes(template, tax)
if notes: if notes:
# Tax to be updated # Tax to be updated
@ -488,7 +490,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
'tax_id': template.id, 'tax_id': template.id,
'update_chart_wizard_id': self.id, 'update_chart_wizard_id': self.id,
'type': 'updated', 'type': 'updated',
'update_tax_id': tax.id, 'update_tax_id': tax_id,
'notes': notes, 'notes': notes,
}) })
@ -496,7 +498,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
# deactivation # deactivation
taxes_to_delete = self.env['account.tax'].search( taxes_to_delete = self.env['account.tax'].search(
[('company_id', '=', self.company_id.id), [('company_id', '=', self.company_id.id),
("id", "not in", found_taxes.ids), ("id", "not in", found_taxes_ids),
("active", "=", True)]) ("active", "=", True)])
for tax in taxes_to_delete: for tax in taxes_to_delete:
self.tax_ids.create({ self.tax_ids.create({
@ -513,9 +515,9 @@ class WizardUpdateChartsAccounts(models.TransientModel):
for template in self.chart_template_ids.mapped("account_ids"): for template in self.chart_template_ids.mapped("account_ids"):
# Search for a real account that matches the template # Search for a real account that matches the template
account = self.find_account_by_templates(template) account_id = self.find_account_by_templates(template)
if not account: if not account_id:
# Account to be created # Account to be created
self.account_ids.create({ self.account_ids.create({
'account_id': template.id, 'account_id': template.id,
@ -525,6 +527,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
}) })
else: else:
# Check the account for changes # Check the account for changes
account = self.env['account.account'].browse(account_id)
notes = self.diff_notes(template, account) notes = self.diff_notes(template, account)
if notes: if notes:
# Account to be updated # Account to be updated
@ -532,7 +535,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
'account_id': template.id, 'account_id': template.id,
'update_chart_wizard_id': self.id, 'update_chart_wizard_id': self.id,
'type': 'updated', 'type': 'updated',
'update_account_id': account.id, 'update_account_id': account_id,
'notes': notes, 'notes': notes,
}) })
@ -547,8 +550,8 @@ class WizardUpdateChartsAccounts(models.TransientModel):
[('chart_template_id', 'in', self.chart_template_ids.ids)]) [('chart_template_id', 'in', self.chart_template_ids.ids)])
for template in templates: for template in templates:
# Search for a real fiscal position that matches the template # Search for a real fiscal position that matches the template
fp = self.find_fp_by_templates(template) fp_id = self.find_fp_by_templates(template)
if not fp: if not fp_id:
# Fiscal position to be created # Fiscal position to be created
wiz_fp.create({ wiz_fp.create({
'fiscal_position_id': template.id, 'fiscal_position_id': template.id,
@ -558,6 +561,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
}) })
else: else:
# Check the fiscal position for changes # Check the fiscal position for changes
fp = self.env['account.fiscal.position'].browse(fp_id)
notes = self.diff_notes(template, fp) notes = self.diff_notes(template, fp)
if notes: if notes:
# Fiscal position template to be updated # Fiscal position template to be updated
@ -565,7 +569,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
'fiscal_position_id': template.id, 'fiscal_position_id': template.id,
'update_chart_wizard_id': self.id, 'update_chart_wizard_id': self.id,
'type': 'updated', 'type': 'updated',
'update_fiscal_position_id': fp.id, 'update_fiscal_position_id': fp_id,
'notes': notes, 'notes': notes,
}) })
@ -602,8 +606,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
'reconcile': account_template.reconcile, 'reconcile': account_template.reconcile,
'note': account_template.note, 'note': account_template.note,
'tax_ids': [ 'tax_ids': [
(6, 0, (6, 0, [self.find_tax_by_templates(account_template.tax_ids)]),
self.find_tax_by_templates(account_template.tax_ids).ids),
], ],
'company_id': self.company_id.id, 'company_id': self.company_id.id,
}) })
@ -665,10 +668,8 @@ class WizardUpdateChartsAccounts(models.TransientModel):
for fp_tax in fp_template.tax_ids: for fp_tax in fp_template.tax_ids:
# Create the fp tax mapping # Create the fp tax mapping
tax_mapping.append({ tax_mapping.append({
'tax_src_id': self.find_tax_by_templates( 'tax_src_id': self.find_tax_by_templates(fp_tax.tax_src_id),
fp_tax.tax_src_id).id, 'tax_dest_id': self.find_tax_by_templates(fp_tax.tax_dest_id),
'tax_dest_id': self.find_tax_by_templates(
fp_tax.tax_dest_id).id,
}) })
# Account mappings # Account mappings
account_mapping = [] account_mapping = []
@ -676,11 +677,11 @@ class WizardUpdateChartsAccounts(models.TransientModel):
# Create the fp account mapping # Create the fp account mapping
account_mapping.append({ account_mapping.append({
'account_src_id': ( 'account_src_id': (
self.find_account_by_templates( self.find_account_by_templates(fp_account.account_src_id)
fp_account.account_src_id).id), ),
'account_dest_id': ( 'account_dest_id': (
self.find_account_by_templates( self.find_account_by_templates(fp_account.account_dest_id)
fp_account.account_dest_id).id), ),
}) })
return { return {
'company_id': self.company_id.id, 'company_id': self.company_id.id,