Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit dbcc371

Browse files
committed
Convert model attributes to an AtomxModel when accessing attributes
1 parent 774e4fb commit dbcc371

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
- Add ``editable`` parameter to :meth:`atomx.Atomx.report`.
55
- Add ``dsp`` reporting scope.
66
- Add ``daterange`` parameter to :meth:`atomx.Atomx.report`.
7+
- When accessing a model attribute, attributes that are atomx models will now
8+
automatically returned as :class:`atomx.models.AtomxModel` instead of
9+
leaving them as `dict`.
710
- :meth:`atomx.models.AtomxModel.reload` takes kwargs parameter
811
- :meth:`atomx.Atomx.delete` can take a `atomx.models` instance as argument
912
- Add new models:

atomx/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class AtomxModel(object):
3333
:param attributes: model attributes
3434
"""
3535
def __init__(self, id=None, session=None, **attributes):
36+
from atomx.utils import get_attribute_model_name
3637
for k, v in attributes.items():
3738
if k.endswith('_at'):
3839
try:
@@ -44,6 +45,7 @@ def __init__(self, id=None, session=None, **attributes):
4445
attributes[k] = datetime.strptime(v, '%Y-%m-%d')
4546
except (ValueError, TypeError):
4647
pass
48+
4749
if id is not None:
4850
attributes['id'] = id
4951

@@ -63,6 +65,12 @@ def __getattr__(self, item):
6365
isinstance(attr, list) and len(attr) > 0 and
6466
isinstance(attr[0], int)):
6567
del self._attributes[item]
68+
elif model_name:
69+
Model = globals()[model_name]
70+
if isinstance(attr, list) and len(attr) > 0 and isinstance(attr[0], dict):
71+
return [Model(session=self.session, **a) for a in attr]
72+
elif isinstance(attr, dict):
73+
return Model(session=self.session, **attr)
6674

6775
# if item not in model and session exists,
6876
# try to load model attribute from server if possible

0 commit comments

Comments
 (0)