Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/configuration/optional-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,41 @@ EXEMPT_VIEW_PERMISSIONS = ['*']

---

## FIELD_CHOICES

Default: Empty dictionary

Some static choice fields on models can be configured with custom values. This is done by defining `FIELD_CHOICES` as a dictionary mapping model fields to their choices list. Each choice in the list must have a database value and a human-friendly label, and may optionally specify a color.

For example, to specify a custom set of choices for the site status field:

```python
FIELD_CHOICES = {
'dcim.Site.status': (
('foo', 'Foo'),
('bar', 'Bar'),
('baz', 'Baz'),
)
}
```

These will be appended to the stock choices for the field.

The following model field support configurable choices:

* `circuits.Circuit.status`
* `dcim.Device.status`
* `dcim.PowerFeed.status`
* `dcim.Rack.status`
* `dcim.Site.status`
* `ipam.IPAddress.status`
* `ipam.IPRange.status`
* `ipam.Prefix.status`
* `ipam.VLAN.status`
* `virtualization.VirtualMachine.status`

---

## HTTP_PROXIES

Default: None
Expand Down
26 changes: 9 additions & 17 deletions netbox/circuits/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#

class CircuitStatusChoices(ChoiceSet):
key = 'circuits.Circuit.status'

STATUS_DEPROVISIONING = 'deprovisioning'
STATUS_ACTIVE = 'active'
Expand All @@ -14,23 +15,14 @@ class CircuitStatusChoices(ChoiceSet):
STATUS_OFFLINE = 'offline'
STATUS_DECOMMISSIONED = 'decommissioned'

CHOICES = (
(STATUS_PLANNED, 'Planned'),
(STATUS_PROVISIONING, 'Provisioning'),
(STATUS_ACTIVE, 'Active'),
(STATUS_OFFLINE, 'Offline'),
(STATUS_DEPROVISIONING, 'Deprovisioning'),
(STATUS_DECOMMISSIONED, 'Decommissioned'),
)

CSS_CLASSES = {
STATUS_DEPROVISIONING: 'warning',
STATUS_ACTIVE: 'success',
STATUS_PLANNED: 'info',
STATUS_PROVISIONING: 'primary',
STATUS_OFFLINE: 'danger',
STATUS_DECOMMISSIONED: 'secondary',
}
CHOICES = [
(STATUS_PLANNED, 'Planned', 'info'),
(STATUS_PROVISIONING, 'Provisioning', 'primary'),
(STATUS_ACTIVE, 'Active', 'success'),
(STATUS_OFFLINE, 'Offline', 'danger'),
(STATUS_DEPROVISIONING, 'Deprovisioning', 'warning'),
(STATUS_DECOMMISSIONED, 'Decommissioned', 'secondary'),
]


#
Expand Down
2 changes: 1 addition & 1 deletion netbox/circuits/models/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get_absolute_url(self):
return reverse('circuits:circuit', args=[self.pk])

def get_status_class(self):
return CircuitStatusChoices.CSS_CLASSES.get(self.status)
return CircuitStatusChoices.colors.get(self.status, 'secondary')


@extras_features('webhooks')
Expand Down
116 changes: 38 additions & 78 deletions netbox/dcim/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,21 @@
#

class SiteStatusChoices(ChoiceSet):
key = 'dcim.Site.status'

STATUS_PLANNED = 'planned'
STATUS_STAGING = 'staging'
STATUS_ACTIVE = 'active'
STATUS_DECOMMISSIONING = 'decommissioning'
STATUS_RETIRED = 'retired'

CHOICES = (
(STATUS_PLANNED, 'Planned'),
(STATUS_STAGING, 'Staging'),
(STATUS_ACTIVE, 'Active'),
(STATUS_DECOMMISSIONING, 'Decommissioning'),
(STATUS_RETIRED, 'Retired'),
)

CSS_CLASSES = {
STATUS_PLANNED: 'info',
STATUS_STAGING: 'primary',
STATUS_ACTIVE: 'success',
STATUS_DECOMMISSIONING: 'warning',
STATUS_RETIRED: 'danger',
}
CHOICES = [
(STATUS_PLANNED, 'Planned', 'info'),
(STATUS_STAGING, 'Staging', 'primary'),
(STATUS_ACTIVE, 'Active', 'primary'),
(STATUS_DECOMMISSIONING, 'Decommissioning', 'warning'),
(STATUS_RETIRED, 'Retired', 'danger'),
]


#
Expand Down Expand Up @@ -67,28 +60,21 @@ class RackWidthChoices(ChoiceSet):


class RackStatusChoices(ChoiceSet):
key = 'dcim.Rack.status'

STATUS_RESERVED = 'reserved'
STATUS_AVAILABLE = 'available'
STATUS_PLANNED = 'planned'
STATUS_ACTIVE = 'active'
STATUS_DEPRECATED = 'deprecated'

CHOICES = (
(STATUS_RESERVED, 'Reserved'),
(STATUS_AVAILABLE, 'Available'),
(STATUS_PLANNED, 'Planned'),
(STATUS_ACTIVE, 'Active'),
(STATUS_DEPRECATED, 'Deprecated'),
)

CSS_CLASSES = {
STATUS_RESERVED: 'warning',
STATUS_AVAILABLE: 'success',
STATUS_PLANNED: 'info',
STATUS_ACTIVE: 'primary',
STATUS_DEPRECATED: 'danger',
}
CHOICES = [
(STATUS_RESERVED, 'Reserved', 'warning'),
(STATUS_AVAILABLE, 'Available', 'success'),
(STATUS_PLANNED, 'Planned', 'info'),
(STATUS_ACTIVE, 'Active', 'primary'),
(STATUS_DEPRECATED, 'Deprecated', 'danger'),
]


class RackDimensionUnitChoices(ChoiceSet):
Expand Down Expand Up @@ -144,6 +130,7 @@ class DeviceFaceChoices(ChoiceSet):


class DeviceStatusChoices(ChoiceSet):
key = 'dcim.Device.status'

STATUS_OFFLINE = 'offline'
STATUS_ACTIVE = 'active'
Expand All @@ -153,25 +140,15 @@ class DeviceStatusChoices(ChoiceSet):
STATUS_INVENTORY = 'inventory'
STATUS_DECOMMISSIONING = 'decommissioning'

CHOICES = (
(STATUS_OFFLINE, 'Offline'),
(STATUS_ACTIVE, 'Active'),
(STATUS_PLANNED, 'Planned'),
(STATUS_STAGED, 'Staged'),
(STATUS_FAILED, 'Failed'),
(STATUS_INVENTORY, 'Inventory'),
(STATUS_DECOMMISSIONING, 'Decommissioning'),
)

CSS_CLASSES = {
STATUS_OFFLINE: 'warning',
STATUS_ACTIVE: 'success',
STATUS_PLANNED: 'info',
STATUS_STAGED: 'primary',
STATUS_FAILED: 'danger',
STATUS_INVENTORY: 'secondary',
STATUS_DECOMMISSIONING: 'warning',
}
CHOICES = [
(STATUS_OFFLINE, 'Offline', 'warning'),
(STATUS_ACTIVE, 'Active', 'success'),
(STATUS_PLANNED, 'Planned', 'info'),
(STATUS_STAGED, 'Staged', 'primary'),
(STATUS_FAILED, 'Failed', 'danger'),
(STATUS_INVENTORY, 'Inventory', 'secondary'),
(STATUS_DECOMMISSIONING, 'Decommissioning', 'warning'),
]


class DeviceAirflowChoices(ChoiceSet):
Expand Down Expand Up @@ -1144,17 +1121,11 @@ class LinkStatusChoices(ChoiceSet):
STATUS_DECOMMISSIONING = 'decommissioning'

CHOICES = (
(STATUS_CONNECTED, 'Connected'),
(STATUS_PLANNED, 'Planned'),
(STATUS_DECOMMISSIONING, 'Decommissioning'),
(STATUS_CONNECTED, 'Connected', 'success'),
(STATUS_PLANNED, 'Planned', 'info'),
(STATUS_DECOMMISSIONING, 'Decommissioning', 'warning'),
)

CSS_CLASSES = {
STATUS_CONNECTED: 'success',
STATUS_PLANNED: 'info',
STATUS_DECOMMISSIONING: 'warning',
}


class CableLengthUnitChoices(ChoiceSet):

Expand Down Expand Up @@ -1183,25 +1154,19 @@ class CableLengthUnitChoices(ChoiceSet):
#

class PowerFeedStatusChoices(ChoiceSet):
key = 'dcim.PowerFeed.status'

STATUS_OFFLINE = 'offline'
STATUS_ACTIVE = 'active'
STATUS_PLANNED = 'planned'
STATUS_FAILED = 'failed'

CHOICES = (
(STATUS_OFFLINE, 'Offline'),
(STATUS_ACTIVE, 'Active'),
(STATUS_PLANNED, 'Planned'),
(STATUS_FAILED, 'Failed'),
)

CSS_CLASSES = {
STATUS_OFFLINE: 'warning',
STATUS_ACTIVE: 'success',
STATUS_PLANNED: 'info',
STATUS_FAILED: 'danger',
}
CHOICES = [
(STATUS_OFFLINE, 'Offline', 'warning'),
(STATUS_ACTIVE, 'Active', 'success'),
(STATUS_PLANNED, 'Planned', 'info'),
(STATUS_FAILED, 'Failed', 'danger'),
]


class PowerFeedTypeChoices(ChoiceSet):
Expand All @@ -1210,15 +1175,10 @@ class PowerFeedTypeChoices(ChoiceSet):
TYPE_REDUNDANT = 'redundant'

CHOICES = (
(TYPE_PRIMARY, 'Primary'),
(TYPE_REDUNDANT, 'Redundant'),
(TYPE_PRIMARY, 'Primary', 'success'),
(TYPE_REDUNDANT, 'Redundant', 'info'),
)

CSS_CLASSES = {
TYPE_PRIMARY: 'success',
TYPE_REDUNDANT: 'info',
}


class PowerFeedSupplyChoices(ChoiceSet):

Expand Down
2 changes: 1 addition & 1 deletion netbox/dcim/models/cables.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def save(self, *args, **kwargs):
self._pk = self.pk

def get_status_class(self):
return LinkStatusChoices.CSS_CLASSES.get(self.status)
return LinkStatusChoices.colors.get(self.status)

def get_compatible_types(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ def get_children(self):
return Device.objects.filter(parent_bay__device=self.pk)

def get_status_class(self):
return DeviceStatusChoices.CSS_CLASSES.get(self.status)
return DeviceStatusChoices.colors.get(self.status, 'secondary')


#
Expand Down
4 changes: 2 additions & 2 deletions netbox/dcim/models/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def parent_object(self):
return self.power_panel

def get_type_class(self):
return PowerFeedTypeChoices.CSS_CLASSES.get(self.type)
return PowerFeedTypeChoices.colors.get(self.type)

def get_status_class(self):
return PowerFeedStatusChoices.CSS_CLASSES.get(self.status)
return PowerFeedStatusChoices.colors.get(self.status, 'secondary')
2 changes: 1 addition & 1 deletion netbox/dcim/models/racks.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def units(self):
return reversed(range(1, self.u_height + 1))

def get_status_class(self):
return RackStatusChoices.CSS_CLASSES.get(self.status)
return RackStatusChoices.colors.get(self.status, 'secondary')

def get_rack_units(self, user=None, face=DeviceFaceChoices.FACE_FRONT, exclude=None, expand_devices=True):
"""
Expand Down
2 changes: 1 addition & 1 deletion netbox/dcim/models/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def get_absolute_url(self):
return reverse('dcim:site', args=[self.pk])

def get_status_class(self):
return SiteStatusChoices.CSS_CLASSES.get(self.status)
return SiteStatusChoices.colors.get(self.status, 'secondary')


#
Expand Down
45 changes: 12 additions & 33 deletions netbox/extras/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,11 @@ class ObjectChangeActionChoices(ChoiceSet):
ACTION_DELETE = 'delete'

CHOICES = (
(ACTION_CREATE, 'Created'),
(ACTION_UPDATE, 'Updated'),
(ACTION_DELETE, 'Deleted'),
(ACTION_CREATE, 'Created', 'success'),
(ACTION_UPDATE, 'Updated', 'primary'),
(ACTION_DELETE, 'Deleted', 'danger'),
)

CSS_CLASSES = {
ACTION_CREATE: 'success',
ACTION_UPDATE: 'primary',
ACTION_DELETE: 'danger',
}


#
# Jounral entries
Expand All @@ -115,19 +109,12 @@ class JournalEntryKindChoices(ChoiceSet):
KIND_DANGER = 'danger'

CHOICES = (
(KIND_INFO, 'Info'),
(KIND_SUCCESS, 'Success'),
(KIND_WARNING, 'Warning'),
(KIND_DANGER, 'Danger'),
(KIND_INFO, 'Info', 'info'),
(KIND_SUCCESS, 'Success', 'success'),
(KIND_WARNING, 'Warning', 'warning'),
(KIND_DANGER, 'Danger', 'danger'),
)

CSS_CLASSES = {
KIND_INFO: 'info',
KIND_SUCCESS: 'success',
KIND_WARNING: 'warning',
KIND_DANGER: 'danger',
}


#
# Log Levels for Reports and Scripts
Expand All @@ -142,21 +129,13 @@ class LogLevelChoices(ChoiceSet):
LOG_FAILURE = 'failure'

CHOICES = (
(LOG_DEFAULT, 'Default'),
(LOG_SUCCESS, 'Success'),
(LOG_INFO, 'Info'),
(LOG_WARNING, 'Warning'),
(LOG_FAILURE, 'Failure'),
(LOG_DEFAULT, 'Default', 'secondary'),
(LOG_SUCCESS, 'Success', 'success'),
(LOG_INFO, 'Info', 'info'),
(LOG_WARNING, 'Warning', 'warning'),
(LOG_FAILURE, 'Failure', 'danger'),
)

CSS_CLASSES = {
LOG_DEFAULT: 'secondary',
LOG_SUCCESS: 'success',
LOG_INFO: 'info',
LOG_WARNING: 'warning',
LOG_FAILURE: 'danger',
}


#
# Job results
Expand Down
Loading