Skip to content

Commit

Permalink
Use the existing UID if not authenticated in Model.get_model
Browse files Browse the repository at this point in the history
  • Loading branch information
muflone committed Jun 2, 2023
1 parent 3656f05 commit fad2841
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
22 changes: 14 additions & 8 deletions pyodoo/v12/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,26 @@ def authenticate(self) -> int:

def get_model(self,
model_name: str,
authenticate: bool = False) -> 'Model':
authenticate: bool = False,
use_existing_uid: bool = False) -> 'Model':
"""
Get a Model object for another model name
:param model_name: Model name
:param authenticate: Automatically authenticate user
:param use_existing_uid: Use the existing UID if not authenticated
:return: Model object
"""
return Model(model_name=model_name,
endpoint=self.api.endpoint,
database=self.api.database,
username=self.api.username,
password=self.api.password,
language=self.api.language,
authenticate=authenticate)
model = Model(model_name=model_name,
endpoint=self.api.endpoint,
database=self.api.database,
username=self.api.username,
password=self.api.password,
language=self.api.language,
authenticate=authenticate)
if not authenticate and use_existing_uid:
# Use the existing UID
model.api.uid = self.api.uid
return model

def get_model_data_reference(self,
module_name: str,
Expand Down
22 changes: 20 additions & 2 deletions tests/test_contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,13 +784,31 @@ def test_40_get_fields_attributes(self) -> None:
self.assertIn('string', field_name)
self.assertIn('type', field_name)

def test_41_get_model(self) -> None:
def test_41_get_model_authenticate(self) -> None:
"""
Get a new Model object
"""
model_name = 'res.users'
model = self.model.get_model(model_name=model_name,
authenticate=True)
authenticate=True,
use_existing_uid=False)
results = model.count(filters=[])
# Check if we have results
self.assertIsNotNone(model)
self.assertIsInstance(model, Model)
self.assertEqual(model.model_name, model_name)
self.assertIsNotNone(results)
self.assertGreater(results, 0)

def test_42_get_model_no_authenticate(self) -> None:
"""
Get a new Model object using the existing UID
"""
self.model.authenticate()
model_name = 'res.users'
model = self.model.get_model(model_name=model_name,
authenticate=False,
use_existing_uid=True)
results = model.count(filters=[])
# Check if we have results
self.assertIsNotNone(model)
Expand Down

0 comments on commit fad2841

Please sign in to comment.