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 = [] diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index 89eecd352..4152cae91 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, @@ -314,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 diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 13ae4c48b..437a3ea23 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.6-dev' BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -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/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'); 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 %} 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