Repository: digitalocean/pynetbox
Pull request #76 (2018-07-18 18:59:37)

Circuit Repr
Adds custom __str__ method for Record from Circuits endpoint so that .cid is returned.


Patch:
diff --git a/pynetbox/api.py b/pynetbox/api.py
index fceff1e..44d554e 100644
--- a/pynetbox/api.py
+++ b/pynetbox/api.py
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
 limitations under the License.
 '''
 from pynetbox.lib import Endpoint, Request
-from pynetbox import dcim, ipam, virtualization
+from pynetbox import dcim, ipam, virtualization, circuits
 
 
 class App(object):
@@ -119,7 +119,7 @@ class Api(object):
 
         self.dcim = App(dcim, api_kwargs=self.api_kwargs)
         self.ipam = App(ipam, api_kwargs=self.api_kwargs)
-        self.circuits = App('circuits', api_kwargs=self.api_kwargs)
+        self.circuits = App(circuits, api_kwargs=self.api_kwargs)
         self.secrets = App('secrets', api_kwargs=self.api_kwargs)
         self.tenancy = App('tenancy', api_kwargs=self.api_kwargs)
         self.extras = App('extras', api_kwargs=self.api_kwargs)
diff --git a/pynetbox/circuits.py b/pynetbox/circuits.py
new file mode 100644
index 0000000..7785cad
--- /dev/null
+++ b/pynetbox/circuits.py
@@ -0,0 +1,22 @@
+'''
+(c) 2017 DigitalOcean
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+from pynetbox.lib.response import Record
+
+
+class Circuits(Record):
+
+    def __str__(self):
+        return self.cid
diff --git a/pynetbox/lib/response.py b/pynetbox/lib/response.py
index 19ffa35..e03d926 100644
--- a/pynetbox/lib/response.py
+++ b/pynetbox/lib/response.py
@@ -20,22 +20,6 @@ from pynetbox.lib.query import Request
 
 
 def get_return(lookup, return_fields=None):
-    '''Returns simple representations for items passed to lookup.
-
-    Used to return a "simple" representation of objects and collections
-    sent to it via lookup. If lookup is an IPNetwork object immediately
-    return the string representation. Otherwise, we look to see if
-    lookup is a "choices" field (dict with only 'id' and 'value')
-    or a nested_return. Finally, we check if it's a Record, if
-    so simply return a string. Order is important due to nested_return
-    being self-referential.
-
-    :arg list,optional return_fields: A list of fields to reference when
-        calling values on lookup.
-    '''
-
-    if isinstance(lookup, netaddr.IPNetwork):
-        return str(lookup)
 
     for i in return_fields or ['id', 'value', 'nested_return']:
         if isinstance(lookup, dict) and lookup.get(i):
@@ -43,7 +27,6 @@ def get_return(lookup, return_fields=None):
         else:
             if hasattr(lookup, i):
                 return getattr(lookup, i)
-
     if isinstance(lookup, Record):
         return str(lookup)
     else:


Test patch:
diff --git a/tests/test_circuits.py b/tests/test_circuits.py
index 6d03692..b95cc71 100644
--- a/tests/test_circuits.py
+++ b/tests/test_circuits.py
@@ -92,6 +92,14 @@ class GenericTest(object):
 class CircuitsTestCase(unittest.TestCase, GenericTest):
     name = 'circuits'
 
+    @patch(
+        'pynetbox.lib.query.requests.get',
+        return_value=Response(fixture='circuits/circuit.json')
+    )
+    def test_repr(self, _):
+        test = nb.circuits.get(1)
+        self.assertEqual(str(test), '123456')
+
 
 class ProviderTestCase(unittest.TestCase, GenericTest):
     name = 'providers'
diff --git a/tests/test_ipam.py b/tests/test_ipam.py
index b97aa75..a3689e9 100644
--- a/tests/test_ipam.py
+++ b/tests/test_ipam.py
@@ -126,19 +126,6 @@ class PrefixTestCase(unittest.TestCase, GenericTest):
         self.assertEqual(ret_serialized['prefix'], '10.1.2.0/24')
         self.assertTrue(netaddr.IPNetwork(ret_serialized['prefix']))
 
-    @patch(
-        'pynetbox.lib.query.requests.put',
-        return_value=Response(fixture='ipam/prefix.json')
-    )
-    @patch(
-        'pynetbox.lib.query.requests.get',
-        return_value=Response(fixture='ipam/prefix.json')
-    )
-    def test_idempotence(self, *_):
-        ret = nb.prefixes.get(1)
-        test = ret.save()
-        self.assertFalse(test)
-
     @patch(
         'pynetbox.lib.query.requests.get',
         side_effect=[