Allow to change user company with context
review30004
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
* Allow to change user company with context
|
||||
|
||||
Version 2.0.0 - 2011-04-27
|
||||
* Bug fixes (see mercurial logs for details)
|
||||
|
||||
|
||||
+27
@@ -167,6 +167,33 @@ class User(ModelSQL, ModelView):
|
||||
res = copy.deepcopy(res)
|
||||
return res
|
||||
|
||||
def read(self, ids, fields_names=None):
|
||||
user_id = Transaction().user
|
||||
if user_id == 0 and 'user' in Transaction().context:
|
||||
user_id = Transaction().context['user']
|
||||
result = super(User, self).read(ids, fields_names=fields_names)
|
||||
if (fields_names
|
||||
and 'company' in fields_names
|
||||
and 'company' in Transaction().context):
|
||||
values = None
|
||||
if isinstance(ids, (int, long)):
|
||||
if int(user_id) == ids:
|
||||
values = result
|
||||
else:
|
||||
if int(user_id) in ids:
|
||||
for vals in result:
|
||||
if vals['id'] == int(user_id):
|
||||
values = vals
|
||||
break
|
||||
if values:
|
||||
with Transaction().reset_context():
|
||||
companies = self.read(user_id, ['companies'])['companies']
|
||||
company_id = Transaction().context['company']
|
||||
if ((company_id and company_id in companies)
|
||||
or not company_id):
|
||||
values['company'] = company_id
|
||||
return result
|
||||
|
||||
User()
|
||||
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ class CompanyTestCase(unittest.TestCase):
|
||||
self.company = POOL.get('company.company')
|
||||
self.employee = POOL.get('company.employee')
|
||||
self.currency = POOL.get('currency.currency')
|
||||
self.user = POOL.get('res.user')
|
||||
|
||||
def test0005views(self):
|
||||
'''
|
||||
@@ -87,6 +88,63 @@ class CompanyTestCase(unittest.TestCase):
|
||||
})
|
||||
transaction.cursor.commit()
|
||||
|
||||
def test0040user(self):
|
||||
'''
|
||||
Test user company
|
||||
'''
|
||||
with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
|
||||
currency1_id = self.currency.search([
|
||||
('code', '=', 'cu1'),
|
||||
], 0, 1, None)[0]
|
||||
|
||||
company1_id = self.company.search([
|
||||
('name', '=', 'B2CK'),
|
||||
], 0, 1, None)[0]
|
||||
|
||||
company2_id = self.company.create({
|
||||
'name': 'B2CK Branch',
|
||||
'parent': company1_id,
|
||||
'currency': currency1_id,
|
||||
})
|
||||
user1_id = self.user.create({
|
||||
'name': 'Test 1',
|
||||
'login': 'test1',
|
||||
'main_company': company1_id,
|
||||
'company': company1_id,
|
||||
})
|
||||
user2_id = self.user.create({
|
||||
'name': 'Test 2',
|
||||
'login': 'test2',
|
||||
'main_company': company2_id,
|
||||
'company': company2_id,
|
||||
})
|
||||
self.assert_(user1_id)
|
||||
|
||||
with transaction.set_user(user1_id):
|
||||
company_id = self.user.read(user1_id, ['company'])['company']
|
||||
self.assertEqual(company_id, company1_id)
|
||||
|
||||
user1, user2 = self.user.browse([user1_id, user2_id])
|
||||
self.assertEqual(user1.company.id, company1_id)
|
||||
self.assertEqual(user2.company.id, company2_id)
|
||||
|
||||
with transaction.set_context({'company': company2_id}):
|
||||
company_id = self.user.read(user1_id, ['company'])['company']
|
||||
self.assertEqual(company_id, company2_id)
|
||||
|
||||
user1, user2 = self.user.browse([user1_id, user2_id])
|
||||
self.assertEqual(user1.company.id, company2_id)
|
||||
self.assertEqual(user2.company.id, company2_id)
|
||||
|
||||
with transaction.set_context({'company': False}):
|
||||
company_id = self.user.read(user1_id, ['company'])['company']
|
||||
self.assertEqual(company_id, False)
|
||||
|
||||
user1, user2 = self.user.browse([user1_id, user2_id])
|
||||
self.assertEqual(user1.company.id, False)
|
||||
self.assertEqual(user2.company.id, company2_id)
|
||||
|
||||
|
||||
def suite():
|
||||
suite = trytond.tests.test_tryton.suite()
|
||||
from trytond.modules.currency.tests import test_currency
|
||||
|
||||
Reference in New Issue
Block a user