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

Commit

Permalink
Convert model attributes to an AtomxModel when accessing attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dakra committed Oct 26, 2017
1 parent 774e4fb commit dbcc371
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Add ``editable`` parameter to :meth:`atomx.Atomx.report`.
- Add ``dsp`` reporting scope.
- Add ``daterange`` parameter to :meth:`atomx.Atomx.report`.
- When accessing a model attribute, attributes that are atomx models will now
automatically returned as :class:`atomx.models.AtomxModel` instead of
leaving them as `dict`.
- :meth:`atomx.models.AtomxModel.reload` takes kwargs parameter
- :meth:`atomx.Atomx.delete` can take a `atomx.models` instance as argument
- Add new models:
Expand Down
8 changes: 8 additions & 0 deletions atomx/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AtomxModel(object):
:param attributes: model attributes
"""
def __init__(self, id=None, session=None, **attributes):
from atomx.utils import get_attribute_model_name
for k, v in attributes.items():
if k.endswith('_at'):
try:
Expand All @@ -44,6 +45,7 @@ def __init__(self, id=None, session=None, **attributes):
attributes[k] = datetime.strptime(v, '%Y-%m-%d')
except (ValueError, TypeError):
pass

if id is not None:
attributes['id'] = id

Expand All @@ -63,6 +65,12 @@ def __getattr__(self, item):
isinstance(attr, list) and len(attr) > 0 and
isinstance(attr[0], int)):
del self._attributes[item]
elif model_name:
Model = globals()[model_name]
if isinstance(attr, list) and len(attr) > 0 and isinstance(attr[0], dict):
return [Model(session=self.session, **a) for a in attr]
elif isinstance(attr, dict):
return Model(session=self.session, **attr)

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

0 comments on commit dbcc371

Please sign in to comment.