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

Commit cf91996

Browse files
committed
Add support to remove models from the API
Add :meth:`atomx.models.AtomxModel.delete`. Add :meth:`atomx.Atomx.remove`.
1 parent 7fc1f3e commit cf91996

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

CHANGES.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
1.5.1 (unreleased)
2-
------------------
1+
1.6 (unreleased)
2+
----------------
33

44
- :meth:`atomx.Atomx.login` takes ``totp`` parameter for users that have 2-factor auth enabled.
55
- :meth:`atomx.Atomx.search` takes ``index`` parameter to only search specific models.
6+
- Add support to remove models from the API. See :meth:`atomx.models.AtomxModel.delete`.
7+
- Add :meth:`atomx.Atomx.remove`.
68

79
1.5
810
---

atomx/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,7 @@ def save(self, model):
429429
def create(self, model):
430430
"""Alias for :meth:`.models.AtomxModel.create` with `session` argument."""
431431
return model.create(self)
432+
433+
def remove(self, model):
434+
"""Alias for :meth:`.models.AtomxModel.delete` with `session` argument."""
435+
return model.delete(self)

atomx/models.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def __getattr__(self, item):
5454
from atomx.utils import get_attribute_model_name
5555
model_name = get_attribute_model_name(item)
5656
attr = self._attributes.get(item)
57+
58+
# loading of extra data is only possible if model ID is known
59+
if 'id' not in self._attributes:
60+
raise AttributeError('Model needs at least an `id` value to load more attributes.')
5761
# if requested attribute item is a valid model name and and int or
5862
# a list of integers, just delete the attribute so it gets
5963
# fetched from the api
@@ -161,11 +165,28 @@ def save(self, session=None):
161165
return self
162166

163167
def delete(self, session=None):
164-
"""Delete is currently not supported by the api.
165-
Set `state` to `INACTIVE` to deactivate it.
168+
"""`DELETE` the model in the api.
169+
A `deleted` attribute is to ``True`` on ``self`` so you can check if a
170+
:class:`.AtomxModel` is deleted or not.
171+
172+
.. warning::
173+
174+
Calling this method will permanently remove this model from the API.
175+
176+
:param session: The :class:`atomx.Atomx` session to use for the api call.
177+
(Optional if you specified a `session` at initialization)
178+
:return: A ``dict`` of all models that the API removed.
179+
Keys are the model names and values are a list of IDs.
180+
:rtype: :class:`dict`
166181
"""
167-
raise NotImplementedError("Delete is currently not supported by the api."
168-
"Set `state` to `INACTIVE` to deactivate it.")
182+
session = session or self.session
183+
if not session:
184+
raise NoSessionError
185+
res = session.delete(self._resource_name, self.id)
186+
187+
# set a deleted attribute
188+
self._attributes['deleted'] = True
189+
return res
169190

170191
def reload(self, session=None):
171192
"""Reload the model from the api and update attributes with the response.

atomx/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
VERSION = '1.5.1b'
1+
VERSION = '1.6b'
22
API_VERSION = 'v3'

0 commit comments

Comments
 (0)