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

Commit 476a416

Browse files
committed
bugfix: post/put for models with a CamelCase name
1 parent a4fb796 commit 476a416

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

atomx/models.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def __dir__(self):
7171
def __repr__(self):
7272
return '{}({})'.format(self.__class__.__name__, pprint.pformat(self.json))
7373

74+
@property
75+
def _resource_name(self):
76+
from atomx.utils import model_name_to_rest
77+
return model_name_to_rest(self.__class__.__name__)
78+
7479
@property
7580
def _dirty_json(self):
7681
return {k: self._attributes[k] for k in self._dirty}
@@ -91,7 +96,7 @@ def create(self, session=None):
9196
session = session or self.session
9297
if not session:
9398
raise NoSessionError
94-
res = session.post(self.__class__.__name__, json=self.json)
99+
res = session.post(self._resource_name, json=self.json)
95100
self.__init__(session=session, **res)
96101
return self
97102

@@ -110,7 +115,7 @@ def save(self, session=None):
110115
session = session or self.session
111116
if not session:
112117
raise NoSessionError
113-
res = session.put(self.__class__.__name__, self.id, json=self._dirty_json)
118+
res = session.put(self._resource_name, self.id, json=self._dirty_json)
114119
self.__init__(session=session, **res)
115120
return self
116121

@@ -138,7 +143,7 @@ def reload(self, session=None):
138143
if not hasattr(self, 'id'):
139144
raise ModelNotFoundError("Can't reload without 'id' parameter. "
140145
"Forgot to save() first?")
141-
res = session.get(self.__class__.__name__ + '/' + str(self.id))
146+
res = session.get(self._resource_name + '/' + str(self.id))
142147
self.__init__(session=session, **res.json)
143148
return self
144149

atomx/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,23 @@ def get_attribute_model_name(attribute):
4646
:return: name of the model or False
4747
"""
4848
return get_model_name(re.sub('(_filter|_include|_exclude)$', '', attribute))
49+
50+
51+
def model_name_to_rest(name):
52+
"""Gets a name of a :mod:`atomx.models` and transforms it in the
53+
resource name for the atomx api.
54+
E.g.::
55+
56+
>>> assert model_name_to_rest('ConversionPixels') == 'conversion-pixels'
57+
>>> assert model_name_to_rest('OperatingSystem') == 'operating-system'
58+
>>> assert model_name_to_rest('Advertiser') == 'Advertiser'
59+
60+
:param str name: Name of the model to convert
61+
:return: :class:`str` with the transformed name.
62+
"""
63+
r = name[0]
64+
for i in range(1, len(name)):
65+
if name[i].isupper():
66+
r += '-'
67+
r += name[i]
68+
return r.lower()

0 commit comments

Comments
 (0)