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

Commit b04568e

Browse files
committed
add reload
1 parent 85dcc40 commit b04568e

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

atomx/models.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
from .exceptions import NoSessionError
4-
3+
from .exceptions import NoSessionError, ModelNotFoundError
54

65
__all__ = ['Advertiser', 'Campaign', 'Creative', 'Fallback', 'Network', 'Placement', 'Profile',
76
'Publisher', 'Segment', 'Site', 'User']
@@ -44,14 +43,26 @@ def update(self, session=None):
4443
session = session or self.session
4544
if not session:
4645
raise NoSessionError
47-
res = self.session.put(self.__class__.__name__, self.id, json=self._dirty_json)
46+
res = session.put(self.__class__.__name__, self.id, json=self._dirty_json)
4847
self.__init__(session=session, **res)
4948
return self
5049

5150
def delete(self, session=None):
52-
if not session and not self.session:
51+
session = session or self.session
52+
if not session:
53+
raise NoSessionError
54+
return session.delete(self.__class__.__name__, self.id, json=self._dirty_json)
55+
56+
def reload(self, session=None):
57+
session = session or self.session
58+
if not session:
5359
raise NoSessionError
54-
return self.session.delete(self.__class__.__name__, self.id, json=self._dirty_json)
60+
if not hasattr(self, 'id'):
61+
raise ModelNotFoundError("Can't reload without 'id' parameter. "
62+
"Forgot to save() first?")
63+
res = session.get(self.__class__.__name__ + '/' + str(self.id))
64+
self.__init__(session=session, **res.json)
65+
return self
5566

5667

5768
for m in __all__:

0 commit comments

Comments
 (0)