Skip to content

Commit fad2841

Browse files
committed
Use the existing UID if not authenticated in Model.get_model
1 parent 3656f05 commit fad2841

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

pyodoo/v12/model.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,26 @@ def authenticate(self) -> int:
9292

9393
def get_model(self,
9494
model_name: str,
95-
authenticate: bool = False) -> 'Model':
95+
authenticate: bool = False,
96+
use_existing_uid: bool = False) -> 'Model':
9697
"""
9798
Get a Model object for another model name
9899
:param model_name: Model name
99100
:param authenticate: Automatically authenticate user
101+
:param use_existing_uid: Use the existing UID if not authenticated
100102
:return: Model object
101103
"""
102-
return Model(model_name=model_name,
103-
endpoint=self.api.endpoint,
104-
database=self.api.database,
105-
username=self.api.username,
106-
password=self.api.password,
107-
language=self.api.language,
108-
authenticate=authenticate)
104+
model = Model(model_name=model_name,
105+
endpoint=self.api.endpoint,
106+
database=self.api.database,
107+
username=self.api.username,
108+
password=self.api.password,
109+
language=self.api.language,
110+
authenticate=authenticate)
111+
if not authenticate and use_existing_uid:
112+
# Use the existing UID
113+
model.api.uid = self.api.uid
114+
return model
109115

110116
def get_model_data_reference(self,
111117
module_name: str,

tests/test_contacts.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,13 +784,31 @@ def test_40_get_fields_attributes(self) -> None:
784784
self.assertIn('string', field_name)
785785
self.assertIn('type', field_name)
786786

787-
def test_41_get_model(self) -> None:
787+
def test_41_get_model_authenticate(self) -> None:
788788
"""
789789
Get a new Model object
790790
"""
791791
model_name = 'res.users'
792792
model = self.model.get_model(model_name=model_name,
793-
authenticate=True)
793+
authenticate=True,
794+
use_existing_uid=False)
795+
results = model.count(filters=[])
796+
# Check if we have results
797+
self.assertIsNotNone(model)
798+
self.assertIsInstance(model, Model)
799+
self.assertEqual(model.model_name, model_name)
800+
self.assertIsNotNone(results)
801+
self.assertGreater(results, 0)
802+
803+
def test_42_get_model_no_authenticate(self) -> None:
804+
"""
805+
Get a new Model object using the existing UID
806+
"""
807+
self.model.authenticate()
808+
model_name = 'res.users'
809+
model = self.model.get_model(model_name=model_name,
810+
authenticate=False,
811+
use_existing_uid=True)
794812
results = model.count(filters=[])
795813
# Check if we have results
796814
self.assertIsNotNone(model)

0 commit comments

Comments
 (0)