[IMP] *: use specific ir.property methods

Do not make direct CRUD to ir.property records, use appropriate
methods for that
This commit is contained in:
Martin Trigaux
2020-05-26 15:50:11 +02:00
parent 2f26eee981
commit 6db2a81ea2
9 changed files with 57 additions and 130 deletions
+14 -26
View File
@@ -463,34 +463,22 @@ class AccountChartTemplate(models.Model):
self.ensure_one()
PropertyObj = self.env['ir.property']
todo_list = [
('property_account_receivable_id', 'res.partner', 'account.account'),
('property_account_payable_id', 'res.partner', 'account.account'),
('property_account_expense_categ_id', 'product.category', 'account.account'),
('property_account_income_categ_id', 'product.category', 'account.account'),
('property_account_expense_id', 'product.template', 'account.account'),
('property_account_income_id', 'product.template', 'account.account'),
('property_tax_payable_account_id', 'account.tax.group', 'account.account'),
('property_tax_receivable_account_id', 'account.tax.group', 'account.account'),
('property_advance_tax_payment_account_id', 'account.tax.group', 'account.account'),
('property_account_receivable_id', 'res.partner'),
('property_account_payable_id', 'res.partner'),
('property_account_expense_categ_id', 'product.category'),
('property_account_income_categ_id', 'product.category'),
('property_account_expense_id', 'product.template'),
('property_account_income_id', 'product.template'),
('property_tax_payable_account_id', 'account.tax.group'),
('property_tax_receivable_account_id', 'account.tax.group'),
('property_advance_tax_payment_account_id', 'account.tax.group'),
]
for record in todo_list:
account = getattr(self, record[0])
value = account and 'account.account,' + str(acc_template_ref[account.id]) or False
for field, model in todo_list:
account = self[field]
value = acc_template_ref[account.id] if account else False
if value:
field = self.env['ir.model.fields'].search([('name', '=', record[0]), ('model', '=', record[1]), ('relation', '=', record[2])], limit=1)
vals = {
'name': record[0],
'company_id': company.id,
'fields_id': field.id,
'value': value,
}
properties = PropertyObj.search([('name', '=', record[0]), ('company_id', '=', company.id)])
if properties:
#the property exist: modify it
properties.write(vals)
else:
#create the property
PropertyObj.create(vals)
PropertyObj._set_default(field, model, value, company=company)
stock_properties = [
'property_stock_account_input_categ_id',
'property_stock_account_output_categ_id',
@@ -21,10 +21,6 @@ class ResCompany(models.Model):
def _create_subcontracting_location(self):
parent_location = self.env.ref('stock.stock_location_locations', raise_if_not_found=False)
property_stock_subcontractor_res_partner_field = self.env['ir.model.fields'].search([
('model', '=', 'res.partner'),
('name', '=', 'property_stock_subcontractor')
])
for company in self:
subcontracting_location = self.env['stock.location'].create({
'name': _('Subcontracting Location'),
@@ -32,10 +28,10 @@ class ResCompany(models.Model):
'location_id': parent_location.id,
'company_id': company.id,
})
self.env['ir.property'].create({
'name': 'property_stock_subcontractor_%s' % company.name,
'fields_id': property_stock_subcontractor_res_partner_field.id,
'company_id': company.id,
'value': 'stock.location,%d' % subcontracting_location.id,
})
self.env['ir.property']._set_default(
"property_stock_subcontractor",
"res.partner",
subcontracting_location,
company,
)
company.subcontracting_location_id = subcontracting_location
+6 -12
View File
@@ -30,12 +30,7 @@ class TestPointOfSaleHttpCommon(odoo.tests.HttpCase):
'reconcile': True})
self.env.company.account_default_pos_receivable_account_id = account_receivable
self.env['ir.property']._set_default(
'property_account_receivable_id',
'res.partner',
account_receivable,
main_company,
)
self.env['ir.property']._set_default('property_account_receivable_id', 'res.partner', account_receivable, main_company)
cash_journal = journal_obj.create({
'name': 'Cash Test',
@@ -400,12 +395,11 @@ class TestPointOfSaleHttpCommon(odoo.tests.HttpCase):
# Change the default sale pricelist of customers,
# so the js tests can expect deterministically this pricelist when selecting a customer.
field = env['ir.model.fields']._get('res.partner', 'property_product_pricelist')
env['ir.property'].search([
('name', '=', 'property_product_pricelist'),
('fields_id', '=', field.id),
('res_id', '=', False)
]).write({'value_reference': 'product.pricelist,%s' % public_pricelist.id})
env['ir.property']._set_default(
"property_product_pricelist",
"res.partner",
public_pricelist,
)
@odoo.tests.tagged('post_install', '-at_install')
+15 -16
View File
@@ -47,7 +47,6 @@ class Company(models.Model):
def _create_inventory_loss_location(self):
parent_location = self.env.ref('stock.stock_location_locations_virtual', raise_if_not_found=False)
inventory_loss_product_template_field = self.env['ir.model.fields'].search([('model','=','product.template'),('name','=','property_stock_inventory')])
for company in self:
inventory_loss_location = self.env['stock.location'].create({
'name': 'Inventory adjustment',
@@ -55,16 +54,15 @@ class Company(models.Model):
'location_id': parent_location.id,
'company_id': company.id,
})
self.env['ir.property'].create({
'name': 'property_stock_inventory_%s' % company.name,
'fields_id': inventory_loss_product_template_field.id,
'company_id': company.id,
'value': 'stock.location,%d' % inventory_loss_location.id,
})
self.env['ir.property']._set_default(
"property_stock_inventory",
"product.template",
inventory_loss_location,
company.id,
)
def _create_production_location(self):
parent_location = self.env.ref('stock.stock_location_locations_virtual', raise_if_not_found=False)
production_product_template_field = self.env['ir.model.fields'].search([('model','=','product.template'),('name','=','property_stock_production')])
for company in self:
production_location = self.env['stock.location'].create({
'name': 'Production',
@@ -72,12 +70,13 @@ class Company(models.Model):
'location_id': parent_location.id,
'company_id': company.id,
})
self.env['ir.property'].create({
'name': 'property_stock_inventory_%s' % company.name,
'fields_id': production_product_template_field.id,
'company_id': company.id,
'value': 'stock.location,%d' % production_location.id,
})
self.env['ir.property']._set_default(
"property_stock_production",
"product.template",
production_location,
company.id,
)
def _create_scrap_location(self):
parent_location = self.env.ref('stock.stock_location_locations_virtual', raise_if_not_found=False)
@@ -129,7 +128,7 @@ class Company(models.Model):
@api.model
def create_missing_inventory_loss_location(self):
company_ids = self.env['res.company'].search([])
inventory_loss_product_template_field = self.env['ir.model.fields'].search([('model','=','product.template'),('name','=','property_stock_inventory')])
inventory_loss_product_template_field = self.env['ir.model.fields']._get('product.template', 'property_stock_inventory')
companies_having_property = self.env['ir.property'].sudo().search([('fields_id', '=', inventory_loss_product_template_field.id)]).mapped('company_id')
company_without_property = company_ids - companies_having_property
company_without_property._create_inventory_loss_location()
@@ -137,7 +136,7 @@ class Company(models.Model):
@api.model
def create_missing_production_location(self):
company_ids = self.env['res.company'].search([])
production_product_template_field = self.env['ir.model.fields'].search([('model','=','product.template'),('name','=','property_stock_production')])
production_product_template_field = self.env['ir.model.fields']._get('product.template', 'property_stock_production')
companies_having_property = self.env['ir.property'].sudo().search([('fields_id', '=', production_product_template_field.id)]).mapped('company_id')
company_without_property = company_ids - companies_having_property
company_without_property._create_production_location()
@@ -20,43 +20,16 @@ class AccountChartTemplate(models.Model):
PropertyObj = self.env['ir.property'] # Property Stock Journal
value = self.env['account.journal'].search([('company_id', '=', company.id), ('code', '=', 'STJ'), ('type', '=', 'general')], limit=1)
if value:
field = self.env['ir.model.fields'].search([('name', '=', 'property_stock_journal'), ('model', '=', 'product.category'), ('relation', '=', 'account.journal')], limit=1)
vals = {
'name': 'property_stock_journal',
'company_id': company.id,
'fields_id': field.id,
'value': 'account.journal,%s' % value.id,
}
properties = PropertyObj.search([('name', '=', 'property_stock_journal'), ('company_id', '=', company.id)])
if properties:
# the property exist: modify it
properties.write(vals)
else:
# create the property
PropertyObj.create(vals)
PropertyObj._set_default("property_stock_journal", "product.category", value, company)
todo_list = [ # Property Stock Accounts
'property_stock_account_input_categ_id',
'property_stock_account_output_categ_id',
'property_stock_valuation_account_id',
]
for record in todo_list:
account = getattr(self, record)
value = account and 'account.account,' + str(acc_template_ref[account.id]) or False
if value:
field = self.env['ir.model.fields'].search([('name', '=', record), ('model', '=', 'product.category'), ('relation', '=', 'account.account')], limit=1)
vals = {
'name': record,
'company_id': company.id,
'fields_id': field.id,
'value': value,
}
properties = PropertyObj.search([('name', '=', record), ('company_id', '=', company.id)], limit=1)
if not properties:
# create the property
PropertyObj.create(vals)
elif not properties.value_reference:
# update the property if False
properties.write(vals)
for field in todo_list:
account = self[field]
value = acc_template_ref[account.id] if account else False
PropertyObj._set_default(field, "product.category", value, company)
return res
@@ -28,12 +28,7 @@ class StockLandedCost(models.Model):
if self.env.company.lc_journal_id:
lc_journal = self.env.company.lc_journal_id
else:
ir_property = self.env['ir.property'].search([
('name', '=', 'property_stock_journal'),
('company_id', '=', self.env.company.id)
], limit=1)
if ir_property:
lc_journal = ir_property.get_by_record()
lc_journal = self.env['ir.property']._get("property_stock_journal", "product.category")
return lc_journal
name = fields.Char(
@@ -432,19 +432,9 @@ class TestWebsitePriceListMultiCompany(TransactionCaseWithUserDemo):
# Ensure everything was done correctly
self.assertEqual(self.demo_user.partner_id.with_company(self.company1.id).property_product_pricelist, self.c1_pl)
self.assertEqual(self.demo_user.partner_id.with_company(self.company2.id).property_product_pricelist, self.c2_pl)
irp1 = self.env['ir.property'].search([
('name', '=', 'property_product_pricelist'),
('company_id', '=', self.company1.id),
('res_id', '=', 'res.partner,%s' % self.demo_user.partner_id.id),
('value_reference', '=', 'product.pricelist,%s' % self.c1_pl.id),
])
irp2 = self.env['ir.property'].search([
('name', '=', 'property_product_pricelist'),
('company_id', '=', self.company2.id),
('res_id', '=', 'res.partner,%s' % self.demo_user.partner_id.id),
('value_reference', '=', 'product.pricelist,%s' % self.c2_pl.id),
])
self.assertEqual(len(irp1 + irp2), 2, "Ensure there is an `ir.property` for demo partner for every company, and that the pricelist is the company specific one.")
irp1 = self.env['ir.property'].with_company(self.company1)._get("property_product_pricelist", "res.partner", self.demo_user.partner_id.id)
irp2 = self.env['ir.property'].with_company(self.company2)._get("property_product_pricelist", "res.partner", self.demo_user.partner_id.id)
self.assertEqual((irp1, irp2), (self.c1_pl, self.c2_pl), "Ensure there is an `ir.property` for demo partner for every company, and that the pricelist is the company specific one.")
simulate_frontend_context(self)
# ---------------------------------- IR.PROPERTY -------------------------------------
# id | name | res_id | company_id | value_reference
+6 -9
View File
@@ -235,21 +235,18 @@ class TestORM(TransactionCase):
'groups_id': [(6, 0, [self.ref('base.group_partner_manager')])],
})
p1 = self.env['res.partner'].with_user(user).create({'name': 'Zorro'})
p1_prop = self.env['ir.property'].with_user(user).create({
'name': 'Slip en laine',
'res_id': 'res.partner,{}'.format(p1.id),
'fields_id': self.env['ir.model.fields'].search([
('model', '=', 'res.partner'), ('name', '=', 'ref')], limit=1).id,
'value_text': 'Nain poilu',
'type': 'char',
})
self.env['ir.property'].with_user(user)._set_multi("ref", "res.partner", {p1.id: "Nain poilu"})
p1_prop = self.env['ir.property'].with_user(user)._get("ref", "res.partner", res_id=p1.id)
self.assertEqual(
p1_prop, "Nain poilu", 'p1_prop should have been created')
# Unlink with unprivileged user
p1.unlink()
# ir.property is deleted
p1_prop = self.env['ir.property'].with_user(user)._get("ref", "res.partner", res_id=p1.id)
self.assertEqual(
p1_prop.exists(), self.env['ir.property'], 'p1_prop should have been deleted')
p1_prop, False, 'p1_prop should have been deleted')
def test_create_multi(self):
""" create for multiple records """
@@ -485,12 +485,7 @@ class TestIrPropertyOptimizations(TransactionCase):
def test_with_truthy_default(self):
eggs = self.Eggs.create({})
field = self.env['ir.model.fields']._get('test_performance.bacon', 'property_eggs')
self.env['ir.property'].create({
'name': 'property_eggs_with_bacon',
'fields_id': field.id,
'value': eggs,
})
self.env['ir.property']._set_default("property_eggs", "test_performance.bacon", eggs)
self.assertEqual(eggs, self.env['ir.property']._get('property_eggs', 'test_performance.bacon'))