Skip to content

nautobot.apps.choices

Utilities for implementing choices.

nautobot.apps.choices.BannerClassChoices

Bases: ChoiceSet

Styling choices for custom banners.

Source code in nautobot/extras/choices.py
class BannerClassChoices(ChoiceSet):
    """Styling choices for custom banners."""

    CLASS_SUCCESS = "success"
    CLASS_INFO = "info"
    CLASS_WARNING = "warning"
    CLASS_DANGER = "danger"

    CHOICES = (
        (CLASS_SUCCESS, "Success"),
        (CLASS_INFO, "Info"),
        (CLASS_WARNING, "Warning"),
        (CLASS_DANGER, "Danger"),
    )

nautobot.apps.choices.ButtonActionColorChoices

Bases: ChoiceSet

Map standard button actions to Bootstrap color classes.

Source code in nautobot/core/choices.py
class ButtonActionColorChoices(ChoiceSet):
    """
    Map standard button actions to Bootstrap color classes.
    """

    ADD = "success"
    CANCEL = "default"
    CLONE = "success"
    CONFIGURE = "default"
    CONNECT = "success"
    DEFAULT = "default"
    DELETE = "danger"
    DISCONNECT = "info"
    EDIT = "warning"
    EXPORT = "success"
    IMPORT = "primary"
    INFO = "info"
    SUBMIT = "primary"
    SWAP = "primary"

    CHOICES = (
        (ADD, "Add"),
        (CANCEL, "Cancel"),
        (CLONE, "Clone"),
        (CONFIGURE, "Configure"),
        (CONNECT, "Connect"),
        (DEFAULT, "Default"),
        (DELETE, "Delete"),
        (DISCONNECT, "Disconnect"),
        (EDIT, "Edit"),
        (EXPORT, "Export"),
        (IMPORT, "Import"),
        (INFO, "Info"),
        (SUBMIT, "Submit"),
        (SWAP, "Swap"),
    )

nautobot.apps.choices.ButtonActionIconChoices

Bases: ChoiceSet

Map standard button actions to Material Design Icons classes.

Source code in nautobot/core/choices.py
class ButtonActionIconChoices(ChoiceSet):
    """
    Map standard button actions to Material Design Icons classes.
    """

    ADD = "mdi-plus-thick"
    ALERT = "mdi-alert"
    ARROW_DOWN = "mdi-arrow-down-bold"
    ARROW_UP = "mdi-arrow-up-bold"
    CONFIGURE = "mdi-cogs"
    CONNECT = "mdi-ethernet-cable"
    DELETE = "mdi-trash-can-outline"
    DISCONNECT = "mdi-ethernet-cable-off"
    EDIT = "mdi-pencil"
    EXPORT = "mdi-database-export-outline"
    HELP = "mdi-help-circle"
    INFO = "mdi-help-circle"
    IMPORT = "mdi-database-import-outline"
    LOCK = "mdi-lock"
    MAGNIFY = "mdi-magnify"
    NOTE = "mdi-note-text"
    SWAP = "mdi-swap-vertical"
    TRASH = "mdi-trash-can-outline"

    CHOICES = (
        (ADD, "Add"),
        (ALERT, "Alert"),
        (ARROW_DOWN, "Arrow Down"),
        (ARROW_UP, "Arrow Up"),
        (CONFIGURE, "Configure"),
        (CONNECT, "Connect"),
        (DELETE, "Delete"),
        (DISCONNECT, "Disconnect"),
        (EDIT, "Edit"),
        (EXPORT, "Export"),
        (HELP, "Help"),
        (INFO, "Info"),
        (IMPORT, "Import"),
        (LOCK, "Lock"),
        (MAGNIFY, "Magnify"),
        (NOTE, "Note"),
        (SWAP, "Swap"),
        (TRASH, "Trash"),
    )

nautobot.apps.choices.ChoiceSet

Base class for defining choices for a model and/or menu.

Subclasses should define a CHOICES constant which consists of a list of tuples of the form (value, display_str), or optionally as tuples of the form (grouping, ((value, display_str), (value, display_str), ...)).

Example
class GreekCapitalLetterChoices(ChoiceSet):
    ALPHA = "Α"
    BETA = "Β"
    GAMMA = "Γ"

    CHOICES = (
        (ALPHA, "alpha"),
        (BETA, "beta"),
        (GAMMA, "gamma"),
    )
Source code in nautobot/core/choices.py
class ChoiceSet(metaclass=ChoiceSetMeta):
    """
    Base class for defining choices for a model and/or menu.

    Subclasses should define a CHOICES constant which consists of a list of tuples of the form `(value, display_str)`,
    or optionally as tuples of the form `(grouping, ((value, display_str), (value, display_str), ...))`.

    Example:
        ```python
        class GreekCapitalLetterChoices(ChoiceSet):
            ALPHA = "Α"
            BETA = "Β"
            GAMMA = "Γ"

            CHOICES = (
                (ALPHA, "alpha"),
                (BETA, "beta"),
                (GAMMA, "gamma"),
            )
        ```
    """

    CHOICES = []

    @classmethod
    def values(cls):
        """Get a flat list of all values defined in this ChoiceSet's `CHOICES`."""
        return [c[0] for c in unpack_grouped_choices(cls.CHOICES)]

    @classmethod
    def as_dict(cls):
        """Get a dictionary representation of this ChoiceSet's `CHOICES`."""
        # Unpack grouped choices before casting as a dict
        return dict(unpack_grouped_choices(cls.CHOICES))

as_dict() classmethod

Get a dictionary representation of this ChoiceSet's CHOICES.

Source code in nautobot/core/choices.py
@classmethod
def as_dict(cls):
    """Get a dictionary representation of this ChoiceSet's `CHOICES`."""
    # Unpack grouped choices before casting as a dict
    return dict(unpack_grouped_choices(cls.CHOICES))

values() classmethod

Get a flat list of all values defined in this ChoiceSet's CHOICES.

Source code in nautobot/core/choices.py
@classmethod
def values(cls):
    """Get a flat list of all values defined in this ChoiceSet's `CHOICES`."""
    return [c[0] for c in unpack_grouped_choices(cls.CHOICES)]

nautobot.apps.choices.JobResultStatusChoices

Bases: ChoiceSet

These status choices are using the same taxonomy as within Celery core. A Nautobot Job status is equivalent to a Celery task state.

Source code in nautobot/extras/choices.py
class JobResultStatusChoices(ChoiceSet):
    """
    These status choices are using the same taxonomy as within Celery core. A Nautobot Job status
    is equivalent to a Celery task state.
    """

    STATUS_FAILURE = states.FAILURE
    STATUS_PENDING = states.PENDING
    STATUS_RECEIVED = states.RECEIVED
    STATUS_RETRY = states.RETRY
    STATUS_REVOKED = states.REVOKED
    STATUS_STARTED = states.STARTED
    STATUS_SUCCESS = states.SUCCESS

    CHOICES = sorted(zip(states.ALL_STATES, states.ALL_STATES))

    #: Set of all possible states.
    ALL_STATES = states.ALL_STATES
    #: Set of states meaning the task returned an exception.
    EXCEPTION_STATES = states.EXCEPTION_STATES
    #: State precedence.
    #: None represents the precedence of an unknown state.
    #: Lower index means higher precedence.
    PRECEDENCE = states.PRECEDENCE
    #: Set of exception states that should propagate exceptions to the user.
    PROPAGATE_STATES = states.PROPAGATE_STATES
    #: Set of states meaning the task result is ready (has been executed).
    READY_STATES = states.READY_STATES
    #: Set of states meaning the task result is not ready (hasn't been executed).
    UNREADY_STATES = states.UNREADY_STATES

    @staticmethod
    def precedence(state):
        """
        Get the precedence for a state. Lower index means higher precedence.

        Args:
            state (str): One of the status choices.

        Returns:
            (int): Precedence value.

        Examples:
            >>> JobResultStatusChoices.precedence(JobResultStatusChoices.STATUS_SUCCESS)
            0

        """
        return states.precedence(state)

precedence(state) staticmethod

Get the precedence for a state. Lower index means higher precedence.

Parameters:

Name Type Description Default
state str

One of the status choices.

required

Returns:

Type Description
int

Precedence value.

Examples:

>>> JobResultStatusChoices.precedence(JobResultStatusChoices.STATUS_SUCCESS)
0
Source code in nautobot/extras/choices.py
@staticmethod
def precedence(state):
    """
    Get the precedence for a state. Lower index means higher precedence.

    Args:
        state (str): One of the status choices.

    Returns:
        (int): Precedence value.

    Examples:
        >>> JobResultStatusChoices.precedence(JobResultStatusChoices.STATUS_SUCCESS)
        0

    """
    return states.precedence(state)

nautobot.apps.choices.unpack_grouped_choices(choices)

Unpack a grouped choices hierarchy into a flat list of two-tuples. For example:

choices = ( ('Foo', ( (1, 'A'), (2, 'B') )), ('Bar', ( (3, 'C'), (4, 'D') )) )

becomes:

choices = ( (1, 'A'), (2, 'B'), (3, 'C'), (4, 'D') )

Source code in nautobot/core/choices.py
def unpack_grouped_choices(choices):
    """
    Unpack a grouped choices hierarchy into a flat list of two-tuples. For example:

    choices = (
        ('Foo', (
            (1, 'A'),
            (2, 'B')
        )),
        ('Bar', (
            (3, 'C'),
            (4, 'D')
        ))
    )

    becomes:

    choices = (
        (1, 'A'),
        (2, 'B'),
        (3, 'C'),
        (4, 'D')
    )
    """
    unpacked_choices = []
    for key, value in choices:
        if isinstance(value, (list, tuple)):
            # Entered an optgroup
            for optgroup_key, optgroup_value in value:
                unpacked_choices.append((optgroup_key, optgroup_value))
        else:
            unpacked_choices.append((key, value))
    return unpacked_choices