From d8f788aa5d8e711e0ba7f85f8991b55de8a389e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markku=20Leini=C3=B6?= Date: Mon, 18 Nov 2019 22:37:33 +0200 Subject: [PATCH 1/3] Add App.custom_choices() --- pynetbox/api.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pynetbox/api.py b/pynetbox/api.py index a9fce2c4..54176fd0 100644 --- a/pynetbox/api.py +++ b/pynetbox/api.py @@ -78,6 +78,24 @@ def choices(self): return self._choices + def custom_choices(self): + """ Returns _custom_field_choices response from app + + :Returns: Raw response from NetBox's _custom_field_choices endpoint. + :Raises: :py:class:`.RequestError` if called for an invalid endpoint. + """ + custom_field_choices = Request( + base="{}/{}/_custom_field_choices/".format( + self.api.base_url, + self.name, + ), + token=self.api.token, + private_key=self.api.private_key, + ssl_verify=self.api.ssl_verify, + http_session=self.api.http_session, + ).get() + return custom_field_choices + class Api(object): """ The API object is the point of entry to pynetbox. From 8d1b5a11413f03a72128d1e4adc1b7ab002a960d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markku=20Leini=C3=B6?= Date: Mon, 18 Nov 2019 22:46:17 +0200 Subject: [PATCH 2/3] Add usage example in App.custom_choices() --- pynetbox/api.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pynetbox/api.py b/pynetbox/api.py index 54176fd0..b6584444 100644 --- a/pynetbox/api.py +++ b/pynetbox/api.py @@ -83,6 +83,11 @@ def custom_choices(self): :Returns: Raw response from NetBox's _custom_field_choices endpoint. :Raises: :py:class:`.RequestError` if called for an invalid endpoint. + :Example: + + >>> nb.extras.custom_choices() + {'Testfield1': {'Testvalue2': 2, 'Testvalue1': 1}, + 'Testfield2': {'Othervalue2': 4, 'Othervalue1': 3}} """ custom_field_choices = Request( base="{}/{}/_custom_field_choices/".format( From 4e4dd35502bad2b9006cbb8529251b831e74bd8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markku=20Leini=C3=B6?= Date: Wed, 20 Nov 2019 20:06:58 +0200 Subject: [PATCH 3/3] Add unit test for App.custom_choices() --- tests/test_app.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/test_app.py diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 00000000..237ad38a --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,35 @@ +import unittest + +import six + +import pynetbox + +if six.PY3: + from unittest.mock import patch +else: + from mock import patch + +host = "http://localhost:8000" + +def_kwargs = { + 'token': 'abc123', +} + + +class AppCustomChoicesTestCase(unittest.TestCase): + + @patch( + 'pynetbox.core.query.Request.get', + return_value={ + "Testfield1": {"TF1_1": 1, "TF1_2": 2}, + "Testfield2": {"TF2_1": 3, "TF2_2": 4}, + } + ) + def test_custom_choices(self, *_): + api = pynetbox.api( + host, + **def_kwargs + ) + choices = api.extras.custom_choices() + self.assertEqual(len(choices), 2) + self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])