Преглед изворни кода

Fixes #3944: Fix AttributeError exception when viewing prefixes list

Jeremy Stretch пре 6 година
родитељ
комит
5369aef971
3 измењених фајлова са 16 додато и 6 уклоњено
  1. 8 0
      docs/release-notes/version-2.7.md
  2. 4 6
      netbox/ipam/fields.py
  3. 4 0
      netbox/ipam/lookups.py

+ 8 - 0
docs/release-notes/version-2.7.md

@@ -1,3 +1,11 @@
+# v2.7.1 (FUTURE)
+
+# Bug Fixes
+
+* [#3944](https://github.com/netbox-community/netbox/issues/3944) - Fix AttributeError exception when viewing prefixes list
+
+---
+
 # v2.7.0 (2020-01-16)
 # v2.7.0 (2020-01-16)
 
 
 **Note:** This release completely removes the topology map feature ([#2745](https://github.com/netbox-community/netbox/issues/2745)).
 **Note:** This release completely removes the topology map feature ([#2745](https://github.com/netbox-community/netbox/issues/2745)).

+ 4 - 6
netbox/ipam/fields.py

@@ -1,6 +1,6 @@
 from django.core.exceptions import ValidationError
 from django.core.exceptions import ValidationError
 from django.db import models
 from django.db import models
-from netaddr import AddrFormatError, IPNetwork, IPAddress
+from netaddr import AddrFormatError, IPNetwork
 
 
 from . import lookups
 from . import lookups
 from .formfields import IPFormField
 from .formfields import IPFormField
@@ -23,11 +23,9 @@ class BaseIPField(models.Field):
         if not value:
         if not value:
             return value
             return value
         try:
         try:
-            if '/' in str(value):
-                return IPNetwork(value)
-            else:
-                return IPAddress(value)
-        except AddrFormatError as e:
+            # Always return a netaddr.IPNetwork object. (netaddr.IPAddress does not provide a mask.)
+            return IPNetwork(value)
+        except AddrFormatError:
             raise ValidationError("Invalid IP address format: {}".format(value))
             raise ValidationError("Invalid IP address format: {}".format(value))
         except (TypeError, ValueError) as e:
         except (TypeError, ValueError) as e:
             raise ValidationError(e)
             raise ValidationError(e)

+ 4 - 0
netbox/ipam/lookups.py

@@ -103,6 +103,10 @@ class NetHost(Lookup):
 class NetIn(Lookup):
 class NetIn(Lookup):
     lookup_name = 'net_in'
     lookup_name = 'net_in'
 
 
+    def get_prep_lookup(self):
+        # Don't cast the query value to a netaddr object, since it may or may not include a mask.
+        return self.rhs
+
     def as_sql(self, qn, connection):
     def as_sql(self, qn, connection):
         lhs, lhs_params = self.process_lhs(qn, connection)
         lhs, lhs_params = self.process_lhs(qn, connection)
         rhs, rhs_params = self.process_rhs(qn, connection)
         rhs, rhs_params = self.process_rhs(qn, connection)