From 6db2a81ea2578615ccc17199dfc488745b2fc596 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 19 May 2020 10:29:16 +0200 Subject: [PATCH] [IMP] *: use specific ir.property methods Do not make direct CRUD to ir.property records, use appropriate methods for that --- addons/account/models/chart_template.py | 40 +++++++------------ .../mrp_subcontracting/models/res_company.py | 16 +++----- addons/point_of_sale/tests/test_frontend.py | 18 +++------ addons/stock/models/res_company.py | 31 +++++++------- .../models/account_chart_template.py | 37 +++-------------- .../models/stock_landed_cost.py | 7 +--- .../tests/test_website_sale_pricelist.py | 16 ++------ odoo/addons/base/tests/test_orm.py | 15 +++---- .../tests/test_performance.py | 7 +--- 9 files changed, 57 insertions(+), 130 deletions(-) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index ebe926d3fc7..6f12b955724 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -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', diff --git a/addons/mrp_subcontracting/models/res_company.py b/addons/mrp_subcontracting/models/res_company.py index 14a7955e98f..583d2413916 100644 --- a/addons/mrp_subcontracting/models/res_company.py +++ b/addons/mrp_subcontracting/models/res_company.py @@ -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 diff --git a/addons/point_of_sale/tests/test_frontend.py b/addons/point_of_sale/tests/test_frontend.py index 69fff92983e..9fe59733ba1 100644 --- a/addons/point_of_sale/tests/test_frontend.py +++ b/addons/point_of_sale/tests/test_frontend.py @@ -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') diff --git a/addons/stock/models/res_company.py b/addons/stock/models/res_company.py index fea6cbb88c8..4ae579ebaeb 100644 --- a/addons/stock/models/res_company.py +++ b/addons/stock/models/res_company.py @@ -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() diff --git a/addons/stock_account/models/account_chart_template.py b/addons/stock_account/models/account_chart_template.py index 16796b947c5..8f6105d09bb 100644 --- a/addons/stock_account/models/account_chart_template.py +++ b/addons/stock_account/models/account_chart_template.py @@ -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 diff --git a/addons/stock_landed_costs/models/stock_landed_cost.py b/addons/stock_landed_costs/models/stock_landed_cost.py index 66f5a6e9a45..63222bc1385 100644 --- a/addons/stock_landed_costs/models/stock_landed_cost.py +++ b/addons/stock_landed_costs/models/stock_landed_cost.py @@ -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( diff --git a/addons/website_sale/tests/test_website_sale_pricelist.py b/addons/website_sale/tests/test_website_sale_pricelist.py index 04306b9b263..01d5e58049d 100644 --- a/addons/website_sale/tests/test_website_sale_pricelist.py +++ b/addons/website_sale/tests/test_website_sale_pricelist.py @@ -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 diff --git a/odoo/addons/base/tests/test_orm.py b/odoo/addons/base/tests/test_orm.py index 2e0406e6b5b..285e4b67290 100644 --- a/odoo/addons/base/tests/test_orm.py +++ b/odoo/addons/base/tests/test_orm.py @@ -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 """ diff --git a/odoo/addons/test_performance/tests/test_performance.py b/odoo/addons/test_performance/tests/test_performance.py index 104fce93195..01bab7187e2 100644 --- a/odoo/addons/test_performance/tests/test_performance.py +++ b/odoo/addons/test_performance/tests/test_performance.py @@ -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'))