|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -from .exceptions import NoSessionError |
4 | | - |
| 3 | +from .exceptions import NoSessionError, ModelNotFoundError |
5 | 4 |
|
6 | 5 | __all__ = ['Advertiser', 'Campaign', 'Creative', 'Fallback', 'Network', 'Placement', 'Profile', |
7 | 6 | 'Publisher', 'Segment', 'Site', 'User'] |
@@ -44,14 +43,26 @@ def update(self, session=None): |
44 | 43 | session = session or self.session |
45 | 44 | if not session: |
46 | 45 | 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) |
48 | 47 | self.__init__(session=session, **res) |
49 | 48 | return self |
50 | 49 |
|
51 | 50 | 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: |
53 | 59 | 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 |
55 | 66 |
|
56 | 67 |
|
57 | 68 | for m in __all__: |
|
0 commit comments