[IMP] base: remove force_company
From now on, if one wants to force following operations to happen in a given company, use with_company(company) or with_company(cid) to update the environment.
This commit is contained in:
committed by
Raphael Collet
parent
ef20d818ea
commit
3455d02189
@@ -182,7 +182,7 @@ class Property(models.Model):
|
||||
|
||||
# only cache Property.get(res_id=False) as that's
|
||||
# sub-optimally.
|
||||
COMPANY_KEY = "self.env.context.get('force_company') or self.env.company.id"
|
||||
COMPANY_KEY = "self.env.company.id"
|
||||
@ormcache(COMPANY_KEY, 'name', 'model')
|
||||
def _get_default_property(self, name, model):
|
||||
prop = self._get_property(name, model, res_id=False)
|
||||
@@ -206,7 +206,7 @@ class Property(models.Model):
|
||||
res = self._cr.fetchone()
|
||||
if not res:
|
||||
return None
|
||||
company_id = self._context.get('force_company') or self.env.company.id
|
||||
company_id = self.env.company.id
|
||||
return [('fields_id', '=', res[0]), ('company_id', 'in', [company_id, False])]
|
||||
|
||||
@api.model
|
||||
@@ -220,10 +220,7 @@ class Property(models.Model):
|
||||
|
||||
field = self.env[model]._fields[name]
|
||||
field_id = self.env['ir.model.fields']._get(model, name).id
|
||||
company_id = (
|
||||
self._context.get('force_company')
|
||||
or self.env.company.id
|
||||
)
|
||||
company_id = self.env.company.id
|
||||
|
||||
if field.type == 'many2one':
|
||||
comodel = self.env[field.comodel_name]
|
||||
@@ -302,7 +299,7 @@ class Property(models.Model):
|
||||
# retrieve the properties corresponding to the given record ids
|
||||
self._cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", (name, model))
|
||||
field_id = self._cr.fetchone()[0]
|
||||
company_id = self.env.context.get('force_company') or self.env.company.id
|
||||
company_id = self.env.company.id
|
||||
refs = {('%s,%s' % (model, id)): id for id in values}
|
||||
props = self.search([
|
||||
('fields_id', '=', field_id),
|
||||
|
||||
@@ -265,18 +265,10 @@ class IrSequence(models.Model):
|
||||
If several sequences with the correct code are available to the user
|
||||
(multi-company cases), the one from the user's current company will
|
||||
be used.
|
||||
|
||||
:param dict context: context dictionary may contain a
|
||||
``force_company`` key with the ID of the company to
|
||||
use instead of the user's current company for the
|
||||
sequence selection. A matching sequence for that
|
||||
specific company will get higher priority.
|
||||
"""
|
||||
self.check_access_rights('read')
|
||||
force_company = self._context.get('force_company')
|
||||
if not force_company:
|
||||
force_company = self.env.company.id
|
||||
seq_ids = self.search([('code', '=', sequence_code), ('company_id', 'in', [force_company, False])], order='company_id')
|
||||
company_id = self.env.company.id
|
||||
seq_ids = self.search([('code', '=', sequence_code), ('company_id', 'in', [company_id, False])], order='company_id')
|
||||
if not seq_ids:
|
||||
_logger.debug("No ir.sequence has been found for code '%s'. Please make sure a sequence is set for current company." % sequence_code)
|
||||
return False
|
||||
|
||||
@@ -935,12 +935,15 @@ class TestFields(TransactionCaseWithUserDemo):
|
||||
company2 = self.env['res.company'].create({'name': 'B'})
|
||||
|
||||
# create one user per company
|
||||
user0 = self.env['res.users'].create({'name': 'Foo', 'login': 'foo',
|
||||
'company_id': company0.id, 'company_ids': []})
|
||||
user1 = self.env['res.users'].create({'name': 'Bar', 'login': 'bar',
|
||||
'company_id': company1.id, 'company_ids': []})
|
||||
user2 = self.env['res.users'].create({'name': 'Baz', 'login': 'baz',
|
||||
'company_id': company2.id, 'company_ids': []})
|
||||
user0 = self.env['res.users'].create({
|
||||
'name': 'Foo', 'login': 'foo', 'company_id': company0.id,
|
||||
'company_ids': [(6, 0, [company0.id, company1.id, company2.id])]})
|
||||
user1 = self.env['res.users'].create({
|
||||
'name': 'Bar', 'login': 'bar', 'company_id': company1.id,
|
||||
'company_ids': [(6, 0, [company0.id, company1.id, company2.id])]})
|
||||
user2 = self.env['res.users'].create({
|
||||
'name': 'Baz', 'login': 'baz', 'company_id': company2.id,
|
||||
'company_ids': [(6, 0, [company0.id, company1.id, company2.id])]})
|
||||
|
||||
# create values for many2one field
|
||||
tag0 = self.env['test_new_api.multi.tag'].create({'name': 'Qux'})
|
||||
@@ -1009,8 +1012,7 @@ class TestFields(TransactionCaseWithUserDemo):
|
||||
self.assertEqual(record.with_user(user1).foo, False)
|
||||
self.assertEqual(record.with_user(user2).foo, 'default')
|
||||
|
||||
# set field with 'force_company' in context
|
||||
record.with_user(user0).with_context(force_company=company1.id).foo = 'beta'
|
||||
record.with_user(user0).with_company(company1).foo = 'beta'
|
||||
record.invalidate_cache()
|
||||
self.assertEqual(record.with_user(user0).foo, 'main')
|
||||
self.assertEqual(record.with_user(user1).foo, 'beta')
|
||||
|
||||
+1
-1
@@ -225,7 +225,7 @@ def depends_context(*args):
|
||||
All dependencies must be hashable. The following keys have special
|
||||
support:
|
||||
|
||||
* `force_company` (value in context or current company id),
|
||||
* `company` (value in context or current company id),
|
||||
* `uid` (current user id and superuser flag),
|
||||
* `active_test` (value in env.context or value in field.context).
|
||||
"""
|
||||
|
||||
+5
-13
@@ -360,7 +360,7 @@ class Field(MetaField('DummyField', (object,), {})):
|
||||
if not attrs.get('readonly'):
|
||||
attrs['inverse'] = self._inverse_company_dependent
|
||||
attrs['search'] = self._search_company_dependent
|
||||
attrs['depends_context'] = attrs.get('depends_context', ()) + ('force_company',)
|
||||
attrs['depends_context'] = attrs.get('depends_context', ()) + ('company',)
|
||||
if attrs.get('translate'):
|
||||
# by default, translatable fields are context-dependent
|
||||
attrs['depends_context'] = attrs.get('depends_context', ()) + ('lang',)
|
||||
@@ -599,22 +599,14 @@ class Field(MetaField('DummyField', (object,), {})):
|
||||
|
||||
def _compute_company_dependent(self, records):
|
||||
# read property as superuser, as the current user may not have access
|
||||
context = records.env.context
|
||||
if 'force_company' not in context:
|
||||
company = records.env.company
|
||||
context = dict(context, force_company=company.id)
|
||||
Property = records.env(context=context, su=True)['ir.property']
|
||||
Property = records.env['ir.property'].sudo()
|
||||
values = Property.get_multi(self.name, self.model_name, records.ids)
|
||||
for record in records:
|
||||
record[self.name] = values.get(record.id)
|
||||
|
||||
def _inverse_company_dependent(self, records):
|
||||
# update property as superuser, as the current user may not have access
|
||||
context = records.env.context
|
||||
if 'force_company' not in context:
|
||||
company = records.env.company
|
||||
context = dict(context, force_company=company.id)
|
||||
Property = records.env(context=context, su=True)['ir.property']
|
||||
Property = records.env['ir.property'].sudo()
|
||||
values = {
|
||||
record.id: self.convert_to_write(record[self.name], record)
|
||||
for record in records
|
||||
@@ -634,8 +626,8 @@ class Field(MetaField('DummyField', (object,), {})):
|
||||
get_context = env.context.get
|
||||
|
||||
def get(key):
|
||||
if key == 'force_company':
|
||||
return get_context('force_company') or env.company.id
|
||||
if key == 'company':
|
||||
return env.company.id
|
||||
elif key == 'uid':
|
||||
return (env.uid, env.su)
|
||||
elif key == 'active_test':
|
||||
|
||||
+2
-5
@@ -3142,11 +3142,8 @@ Fields:
|
||||
# The second part of the check (for property / company-dependent fields) verifies that the records
|
||||
# linked via those relation fields are compatible with the company that owns the property value, i.e.
|
||||
# the company for which the value is being assigned, i.e:
|
||||
# `self.property_account_payable_id.company_id == self.env.context['force_company']`
|
||||
if self.env.context.get('force_company'):
|
||||
company = self.env['res.company'].browse(self.env.context['force_company'])
|
||||
else:
|
||||
company = self.env.company
|
||||
# `self.property_account_payable_id.company_id == self.env.company
|
||||
company = self.env.company
|
||||
for name in property_fields:
|
||||
# Special case with `res.users` since an user can belong to multiple companies.
|
||||
if record[name]._name == 'res.users' and record[name].company_ids:
|
||||
|
||||
Reference in New Issue
Block a user