From a328e1264231beed3b1613015261bebdd3b8d3db Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 25 Sep 2017 12:14:55 -0400 Subject: [PATCH 1/7] Closes #1536: Improved formatting of aggregate prefix statistics --- netbox/ipam/views.py | 11 ++++++----- netbox/templates/ipam/aggregate_list.html | 11 +++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index 89eecd352..29757a530 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -286,11 +286,12 @@ class AggregateListView(ObjectListView): ipv4_total = 0 ipv6_total = 0 - for a in self.queryset: - if a.prefix.version == 4: - ipv4_total += a.prefix.size - elif a.prefix.version == 6: - ipv6_total += a.prefix.size / 2 ** 64 + for aggregate in self.queryset: + if aggregate.prefix.version == 6: + # Report equivalent /64s for IPv6 to keep things sane + ipv6_total += int(aggregate.prefix.size / 2 ** 64) + else: + ipv4_total += aggregate.prefix.size return { 'ipv4_total': ipv4_total, diff --git a/netbox/templates/ipam/aggregate_list.html b/netbox/templates/ipam/aggregate_list.html index fde53a790..df8e4772e 100644 --- a/netbox/templates/ipam/aggregate_list.html +++ b/netbox/templates/ipam/aggregate_list.html @@ -20,11 +20,18 @@
{% include 'utilities/obj_table.html' with bulk_edit_url='ipam:aggregate_bulk_edit' bulk_delete_url='ipam:aggregate_bulk_delete' %} -

IPv4 total: {{ ipv4_total|intcomma }} /32s

-

IPv6 total: {{ ipv6_total|intcomma }} /64s

{% include 'inc/search_panel.html' %} +
+
+ Statistics +
+
    +
  • Total IPv4 IPs {{ ipv4_total|intcomma }}
  • +
  • Total IPv6 /64s {{ ipv6_total|intcomma }}
  • +
+
{% endblock %} From 2badb04a03fea93e39e127f82c8ecc6916ea5e52 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 25 Sep 2017 13:37:11 -0400 Subject: [PATCH 2/7] Fixes #1489: Corrected server error on validation of empty required custom field --- netbox/extras/api/customfields.py | 61 +++++++++++++++++++------------ 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/netbox/extras/api/customfields.py b/netbox/extras/api/customfields.py index fc83b33e5..5554df924 100644 --- a/netbox/extras/api/customfields.py +++ b/netbox/extras/api/customfields.py @@ -29,34 +29,47 @@ class CustomFieldsSerializer(serializers.BaseSerializer): for field_name, value in data.items(): - cf = custom_fields[field_name] + try: + cf = custom_fields[field_name] + except KeyError: + raise ValidationError( + "Invalid custom field for {} objects: {}".format(content_type, field_name) + ) - # Validate custom field name - if field_name not in custom_fields: - raise ValidationError("Invalid custom field for {} objects: {}".format(content_type, field_name)) + # Data validation + if value not in [None, '']: - # Validate boolean - if cf.type == CF_TYPE_BOOLEAN and value not in [True, False, 1, 0]: - raise ValidationError("Invalid value for boolean field {}: {}".format(field_name, value)) + # Validate boolean + if cf.type == CF_TYPE_BOOLEAN and value not in [True, False, 1, 0]: + raise ValidationError( + "Invalid value for boolean field {}: {}".format(field_name, value) + ) - # Validate date - if cf.type == CF_TYPE_DATE: - try: - datetime.strptime(value, '%Y-%m-%d') - except ValueError: - raise ValidationError("Invalid date for field {}: {}. (Required format is YYYY-MM-DD.)".format( - field_name, value - )) + # Validate date + if cf.type == CF_TYPE_DATE: + try: + datetime.strptime(value, '%Y-%m-%d') + except ValueError: + raise ValidationError( + "Invalid date for field {}: {}. (Required format is YYYY-MM-DD.)".format(field_name, value) + ) - # Validate selected choice - if cf.type == CF_TYPE_SELECT: - try: - value = int(value) - except ValueError: - raise ValidationError("{}: Choice selections must be passed as integers.".format(field_name)) - valid_choices = [c.pk for c in cf.choices.all()] - if value not in valid_choices: - raise ValidationError("Invalid choice for field {}: {}".format(field_name, value)) + # Validate selected choice + if cf.type == CF_TYPE_SELECT: + try: + value = int(value) + except ValueError: + raise ValidationError( + "{}: Choice selections must be passed as integers.".format(field_name) + ) + valid_choices = [c.pk for c in cf.choices.all()] + if value not in valid_choices: + raise ValidationError( + "Invalid choice for field {}: {}".format(field_name, value) + ) + + elif cf.required: + raise ValidationError("Required field {} cannot be empty.".format(field_name)) # Check for missing required fields missing_fields = [] From 63a4a704201aa1a59d3904e957f424ab126ec830 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 25 Sep 2017 13:39:03 -0400 Subject: [PATCH 3/7] #1499: Added utilization graph to child prefixes for aggregates --- netbox/ipam/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index 29757a530..4152cae91 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -315,7 +315,7 @@ class AggregateView(View): ) child_prefixes = add_available_prefixes(aggregate.prefix, child_prefixes) - prefix_table = tables.PrefixTable(child_prefixes) + prefix_table = tables.PrefixDetailTable(child_prefixes) if request.user.has_perm('ipam.change_prefix') or request.user.has_perm('ipam.delete_prefix'): prefix_table.base_columns['pk'].visible = True From aa5aa798e61689adf18d35d101425a3a304c620b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 25 Sep 2017 14:33:32 -0400 Subject: [PATCH 4/7] Fixes #1486: Ignore subinterface IDs when validating LLDP neighbor connections --- netbox/templates/dcim/device_lldp_neighbors.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox/templates/dcim/device_lldp_neighbors.html b/netbox/templates/dcim/device_lldp_neighbors.html index a7346c5db..99c1f6465 100644 --- a/netbox/templates/dcim/device_lldp_neighbors.html +++ b/netbox/templates/dcim/device_lldp_neighbors.html @@ -53,7 +53,7 @@ $(document).ready(function() { success: function(json) { $.each(json['get_lldp_neighbors'], function(iface, neighbors) { var neighbor = neighbors[0]; - var row = $('#' + iface.replace(/(\/)/g, "\\$1")); + var row = $('#' + iface.split(".")[0].replace(/(\/)/g, "\\$1")); var configured_device = row.children('td.configured_device').attr('data'); var configured_interface = row.children('td.configured_interface').attr('data'); // Add LLDP neighbors to table @@ -62,7 +62,7 @@ $(document).ready(function() { // Apply colors to rows if (!configured_device && neighbor['hostname']) { row.addClass('info'); - } else if (configured_device == neighbor['hostname'] && configured_interface == neighbor['port']) { + } else if (configured_device == neighbor['hostname'] && configured_interface == neighbor['port'].split(".")[0]) { row.addClass('success'); } else { row.addClass('danger'); From 00154846798ff4d10375f07a6cdb0d0dfe5085ad Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 25 Sep 2017 14:50:45 -0400 Subject: [PATCH 5/7] Release v2.1.5 --- netbox/netbox/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 13ae4c48b..6d944d4db 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -13,7 +13,7 @@ except ImportError: ) -VERSION = '2.1.5-dev' +VERSION = '2.1.5' BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) From 02d8897b82d47ee88fb6a6647be33babf30792ef Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 25 Sep 2017 14:53:10 -0400 Subject: [PATCH 6/7] Post-release version bump --- netbox/netbox/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 6d944d4db..735372b61 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -13,7 +13,7 @@ except ImportError: ) -VERSION = '2.1.5' +VERSION = '2.1.6-dev' BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) From 97188ad85ba77d361ae38d8b5e01ae9109e5e9bd Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 6 Oct 2017 15:26:56 -0400 Subject: [PATCH 7/7] Fixes #1563: Made necessary updates for Django REST Framework v3.7.0 --- netbox/netbox/settings.py | 2 +- netbox/utilities/api.py | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 735372b61..437a3ea23 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -228,7 +228,7 @@ REST_FRAMEWORK = { 'utilities.api.TokenAuthentication', ), 'DEFAULT_FILTER_BACKENDS': ( - 'rest_framework.filters.DjangoFilterBackend', + 'django_filters.rest_framework.DjangoFilterBackend', ), 'DEFAULT_PAGINATION_CLASS': 'utilities.api.OptionalLimitOffsetPagination', 'DEFAULT_PERMISSION_CLASSES': ( diff --git a/netbox/utilities/api.py b/netbox/utilities/api.py index 3698bc47c..584b00fc3 100644 --- a/netbox/utilities/api.py +++ b/netbox/utilities/api.py @@ -4,13 +4,12 @@ from django.conf import settings from django.contrib.contenttypes.models import ContentType from rest_framework import authentication, exceptions -from rest_framework.compat import is_authenticated from rest_framework.exceptions import APIException from rest_framework.pagination import LimitOffsetPagination from rest_framework.permissions import BasePermission, DjangoModelPermissions, SAFE_METHODS from rest_framework.renderers import BrowsableAPIRenderer from rest_framework.serializers import Field, ModelSerializer, ValidationError -from rest_framework.views import get_view_name as drf_get_view_name +from rest_framework.utils import formatting from users.models import Token @@ -75,7 +74,7 @@ class IsAuthenticatedOrLoginNotRequired(BasePermission): def has_permission(self, request, view): if not settings.LOGIN_REQUIRED: return True - return request.user and is_authenticated(request.user) + return request.user.is_authenticated() # @@ -228,10 +227,18 @@ def get_view_name(view_cls, suffix=None): Derive the view name from its associated model, if it has one. Fall back to DRF's built-in `get_view_name`. """ if hasattr(view_cls, 'queryset'): + # Determine the model name from the queryset. name = view_cls.queryset.model._meta.verbose_name name = ' '.join([w[0].upper() + w[1:] for w in name.split()]) # Capitalize each word - if suffix: - name = "{} {}".format(name, suffix) - return name - return drf_get_view_name(view_cls, suffix) + else: + # Replicate DRF's built-in behavior. + name = view_cls.__name__ + name = formatting.remove_trailing_string(name, 'View') + name = formatting.remove_trailing_string(name, 'ViewSet') + name = formatting.camelcase_to_spaces(name) + + if suffix: + name += ' ' + suffix + + return name