示例#1
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if settings.NETBOX_API:
            self.fields["netbox_device_id"] = forms.ChoiceField(
                label="NetBox Device",
                choices=[(0, "--------")] +
                [(device["id"], device["display_name"])
                 for device in NetBox().get_devices()],
            )
            self.fields["netbox_device_id"].widget.attrs["class"] = " ".join([
                self.fields["netbox_device_id"].widget.attrs.get("class", ""),
                "form-control",
            ]).strip()
        else:
            self.fields["netbox_device_id"].widget = forms.HiddenInput()
示例#2
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if settings.NETBOX_API:
            choices = []
            for device in NetBox().get_devices():
                try:
                    choices.append((device.id, device.display))
                except AttributeError:
                    # Fallback to hold API attribute
                    choices.append((device.id, device.display_name))

            self.fields["netbox_device_id"] = forms.ChoiceField(
                label="NetBox device",
                choices=[(0, "---------")] + choices,
                widget=StaticSelect,
            )
            self.fields["netbox_device_id"].widget.attrs["class"] = " ".join([
                self.fields["netbox_device_id"].widget.attrs.get("class", ""),
                "form-control",
            ]).strip()
        else:
            self.fields["netbox_device_id"].widget = forms.HiddenInput()
    def setUp(self):
        super().setUp()

        self.netbox = NetBox()
        self.netbox.api = pynetbox.api("http://netbox.example.net", token="test")