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

Commit 26aeef4

Browse files
committed
Add history support atomx.models.AtomxModel.history
1 parent 66847d5 commit 26aeef4

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

CHANGES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
- Add new models:
88

99
* :class:`atomx.models.AccountManager` (alias for `User`)
10-
* :class:`atomx.models.BanReason`
1110
* :class:`atomx.models.PlacementType`
1211
* :class:`atomx.models.CampaignDebugReason`
1312
* :class:`atomx.models.Visibility`
@@ -18,6 +17,7 @@
1817
E.g.: ``atomx_api.get(atomx.models.Advertiser)`` or ``atomx_api.get(atomx.models.Advertiser(42))``
1918
- Add :mod:`pickle` support for :mod:`atomx.models`.
2019
- Save HTTP headers in :attr:`atomx.Atomx.last_response`.
20+
- Add history support. :meth:`atomx.models.AtomxModel.history`.
2121

2222

2323
1.4

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Example Usage
6464
publisher = s['publisher'][0] # get the first publisher..
6565
publisher.reload() # .. and load all the data
6666
print(publisher) # now all publisher data is there
67+
publisher.history() # gets all changes made to this publisher
6768
6869
6970
# reporting example

atomx/models.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
NoPandasInstalledError,
1818
)
1919

20-
__all__ = ['AccountManager', 'Advertiser', 'BanReason', 'Bidder', 'Browser', 'CampaignDebugReason',
20+
__all__ = ['AccountManager', 'Advertiser', 'Bidder', 'Browser', 'CampaignDebugReason',
2121
'Campaign', 'Category', 'ConnectionType', 'ConversionPixel', 'Country', 'Creative',
2222
'Datacenter', 'DeviceType', 'Domain', 'Fallback', 'Isp', 'Languages', 'Network',
2323
'OperatingSystem', 'Placement', 'PlacementType', 'Profile', 'Publisher', 'Reason',
@@ -81,9 +81,9 @@ def __setattr__(self, key, value):
8181
def __delattr__(self, item):
8282
if item in self._dirty:
8383
self._dirty.remove(item)
84-
else:
85-
self._attributes[item] = None
86-
self._dirty.add(item)
84+
85+
self._attributes[item] = [] if isinstance(self._attributes[item], list) else None
86+
self._dirty.add(item)
8787

8888
def __dir__(self):
8989
"""Manually add dynamic attributes for autocomplete"""
@@ -190,6 +190,27 @@ def reload(self, session=None):
190190
self.__init__(session=session, **res.json)
191191
return self
192192

193+
def history(self, session=None, offset=0, limit=100, sort='date.asc'):
194+
"""Show the changelog of the model.
195+
196+
:param session: The :class:`atomx.Atomx` session to use for the api call.
197+
(Optional if you specified a `session` at initialization)
198+
:param int offset: Skip first ``offset`` history entries. (default: 0)
199+
:param int limit: Only return ``limit`` history entries. (default: 100)
200+
:param str sort: Sort by `date.asc` or `date.desc`. (default: 'date.asc')
201+
:return: `list` of `dict`s with `date`, `user` and the attributes that changed (`history`).
202+
:rtype: list
203+
"""
204+
session = session or self.session
205+
if not session:
206+
raise NoSessionError
207+
if not hasattr(self, 'id'):
208+
raise ModelNotFoundError("Can't reload without 'id' parameter. "
209+
"Forgot to save() first?")
210+
res = session.get('history', self._resource_name, self.id,
211+
offset=offset, limit=limit, sort=sort)
212+
return res
213+
193214

194215
for m in __all__:
195216
locals()[m] = type(m, (AtomxModel,),

docs/usage.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ Or to get the advertiser for a profile, just:
9595
advertiser = profiles[0].advertiser
9696
9797
98+
You can get a list of all changes with :meth:`atomx.models.AtomxModel.history`.
99+
100+
.. code-block:: python
101+
102+
advertiser.history()
103+
104+
98105
Updating models
99106
---------------
100107

0 commit comments

Comments
 (0)