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

Commit 62445e3

Browse files
committed
lazy load model attributes
1 parent 56df33f commit 62445e3

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

atomx/models.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
22

3-
from .exceptions import NoSessionError, ModelNotFoundError
3+
from .exceptions import NoSessionError, ModelNotFoundError, APIError
44

5-
__all__ = ['Advertiser', 'Campaign', 'Creative', 'Fallback', 'Network', 'Placement', 'Profile',
6-
'Publisher', 'Segment', 'Site', 'User']
5+
__all__ = ['Advertiser', 'Campaign', 'Creative', 'Fallback', 'Network', 'Placement',
6+
'Profile', 'Publisher', 'Segment', 'Site', 'User']
77

88

99
class AtomxModel(object):
@@ -13,6 +13,15 @@ def __init__(self, session=None, **attributes):
1313
super(AtomxModel, self).__setattr__('_dirty', set()) # list of changed attributes
1414

1515
def __getattr__(self, item):
16+
# if item not in model and session exists,
17+
# try to load model attribute from server if possible
18+
if not item.startswith('_') and item not in self._attributes and self.session:
19+
try:
20+
v = self.session.get(self.__class__.__name__ + '/' +
21+
str(self.id) + '/' + str(item))
22+
self._attributes[item] = v
23+
except APIError as e:
24+
raise AttributeError(e)
1625
return self._attributes.get(item)
1726

1827
def __setattr__(self, key, value):

0 commit comments

Comments
 (0)