Reports error: cannot import name 'DEVICE_STATUS_ACTIVE' #3347

Closed
opened 2025-12-29 18:28:05 +01:00 by adam · 2 comments
Owner

Originally created by @keliansb on GitHub (Feb 19, 2020).

Environment

  • Python version: 3.6.8
  • NetBox version: 2.7.6

Steps to Reproduce

Create a report with given example from the documentation in /opt/netbox/netbox/reports then visit the uri /extras/reports.

from dcim.constants import CONNECTION_STATUS_PLANNED, DEVICE_STATUS_ACTIVE
from dcim.models import ConsolePort, Device, PowerPort
from extras.reports import Report


class DeviceConnectionsReport(Report):
    description = "Validate the minimum physical connections for each device"

    def test_console_connection(self):

        # Check that every console port for every active device has a connection defined.
        for console_port in ConsolePort.objects.prefetch_related('device').filter(device__status=DEVICE_STATUS_ACTIVE):
            if console_port.connected_endpoint is None:
                self.log_failure(
                    console_port.device,
                    "No console connection defined for {}".format(console_port.name)
                )
            elif console_port.connection_status == CONNECTION_STATUS_PLANNED:
                self.log_warning(
                    console_port.device,
                    "Console connection for {} marked as planned".format(console_port.name)
                )
            else:
                self.log_success(console_port.device)

    def test_power_connections(self):

        # Check that every active device has at least two connected power supplies.
        for device in Device.objects.filter(status=DEVICE_STATUS_ACTIVE):
            connected_ports = 0
            for power_port in PowerPort.objects.filter(device=device):
                if power_port.connected_endpoint is not None:
                    connected_ports += 1
                    if power_port.connection_status == CONNECTION_STATUS_PLANNED:
                        self.log_warning(
                            device,
                            "Power connection for {} marked as planned".format(power_port.name)
                        )
            if connected_ports < 2:
                self.log_failure(
                    device,
                    "{} connected power supplies found (2 needed)".format(connected_ports)
                )
            else:
                self.log_success(device)

Expected Behavior

We should see the list of class defined in the report file as reports instead of the following error.

Observed Behavior

<class 'ImportError'>
cannot import name 'DEVICE_STATUS_ACTIVE'
Originally created by @keliansb on GitHub (Feb 19, 2020). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, DO NOT open an issue. Instead, post to our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.6.8 <!-- Example: 3.6.9 --> * NetBox version: 2.7.6 <!-- Example: 2.7.3 --> <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox (or the current beta release where applicable). Begin with the creation of any necessary database objects and call out every operation being performed explicitly. If reporting a bug in the REST API, be sure to reconstruct the raw HTTP request(s) being made: Don't rely on a wrapper like pynetbox. --> ### Steps to Reproduce Create a report with given example from [the documentation](https://netbox.readthedocs.io/en/stable/additional-features/reports/) in /opt/netbox/netbox/reports then visit the uri /extras/reports. ```python from dcim.constants import CONNECTION_STATUS_PLANNED, DEVICE_STATUS_ACTIVE from dcim.models import ConsolePort, Device, PowerPort from extras.reports import Report class DeviceConnectionsReport(Report): description = "Validate the minimum physical connections for each device" def test_console_connection(self): # Check that every console port for every active device has a connection defined. for console_port in ConsolePort.objects.prefetch_related('device').filter(device__status=DEVICE_STATUS_ACTIVE): if console_port.connected_endpoint is None: self.log_failure( console_port.device, "No console connection defined for {}".format(console_port.name) ) elif console_port.connection_status == CONNECTION_STATUS_PLANNED: self.log_warning( console_port.device, "Console connection for {} marked as planned".format(console_port.name) ) else: self.log_success(console_port.device) def test_power_connections(self): # Check that every active device has at least two connected power supplies. for device in Device.objects.filter(status=DEVICE_STATUS_ACTIVE): connected_ports = 0 for power_port in PowerPort.objects.filter(device=device): if power_port.connected_endpoint is not None: connected_ports += 1 if power_port.connection_status == CONNECTION_STATUS_PLANNED: self.log_warning( device, "Power connection for {} marked as planned".format(power_port.name) ) if connected_ports < 2: self.log_failure( device, "{} connected power supplies found (2 needed)".format(connected_ports) ) else: self.log_success(device) ``` <!-- What did you expect to happen? --> ### Expected Behavior We should see the list of class defined in the report file as reports instead of the following error. <!-- What happened instead? --> ### Observed Behavior ```python <class 'ImportError'> cannot import name 'DEVICE_STATUS_ACTIVE' ```
adam closed this issue 2025-12-29 18:28:05 +01:00
Author
Owner

@hSaria commented on GitHub (Feb 19, 2020):

This has been replaced with DeviceStatusChoices. The documentation needs updating, but you can use this until then:

from dcim.choices import DeviceStatusChoices

DeviceStatusChoices.STATUS_ACTIVE
@hSaria commented on GitHub (Feb 19, 2020): This has been replaced with [`DeviceStatusChoices`](https://github.com/netbox-community/netbox/blob/8cfb5ac5c627504cf4199d77633cea66cb17457e/netbox/dcim/choices.py#L159). The documentation needs updating, but you can use this until then: ```python from dcim.choices import DeviceStatusChoices DeviceStatusChoices.STATUS_ACTIVE
Author
Owner

@jeremystretch commented on GitHub (Feb 19, 2020):

Updated the example report in 58716407

@jeremystretch commented on GitHub (Feb 19, 2020): Updated the example report in 58716407
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3347