From f1b6f0cfee97b13e3f8a28a56f28cf3dae23b1c9 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 13 Jul 2016 11:15:37 -0400 Subject: [PATCH] Fixes #285: Added PREFER_IPV4 configuration setting --- docs/configuration/optional-settings.md | 8 ++++++++ netbox/dcim/models.py | 5 ++++- netbox/netbox/configuration.example.py | 4 ++++ netbox/netbox/settings.py | 1 + 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/configuration/optional-settings.md b/docs/configuration/optional-settings.md index 0aa59a196..6258a16fd 100644 --- a/docs/configuration/optional-settings.md +++ b/docs/configuration/optional-settings.md @@ -66,6 +66,14 @@ Determine how many objects to display per page within each list of objects. --- +## PREFER_IPV4 + +Default: False + +When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to prefer IPv4 instead. + +--- + ## TIME_ZONE Default: UTC diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index df07fa748..c51714a71 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -1,5 +1,6 @@ from collections import OrderedDict +from django.conf import settings from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse from django.core.validators import MinValueValidator @@ -713,7 +714,9 @@ class Device(CreatedUpdatedModel): @property def primary_ip(self): - if self.primary_ip6: + if settings.PREFER_IPV4 and self.primary_ip4: + return self.primary_ip4 + elif self.primary_ip6: return self.primary_ip6 elif self.primary_ip4: return self.primary_ip4 diff --git a/netbox/netbox/configuration.example.py b/netbox/netbox/configuration.example.py index aba0eb3f5..745bdfaaf 100644 --- a/netbox/netbox/configuration.example.py +++ b/netbox/netbox/configuration.example.py @@ -78,3 +78,7 @@ SHORT_DATETIME_FORMAT = 'Y-m-d H:i' # banners, define BANNER_TOP and set BANNER_BOTTOM = BANNER_TOP. BANNER_TOP = '' BANNER_BOTTOM = '' + +# When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to +# prefer IPv4 instead. +PREFER_IPV4 = False diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 917b2ee4d..ac0eb6836 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -40,6 +40,7 @@ DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a') SHORT_DATETIME_FORMAT = getattr(configuration, 'SHORT_DATETIME_FORMAT', 'Y-m-d H:i') BANNER_TOP = getattr(configuration, 'BANNER_TOP', False) BANNER_BOTTOM = getattr(configuration, 'BANNER_BOTTOM', False) +PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False) CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS # Attempt to import LDAP configuration if it has been defined