Przeglądaj źródła

Miscellaneous RestrictedQuerySet cleanup

Jeremy Stretch 5 lat temu
rodzic
commit
4f00b5af4a

+ 1 - 1
netbox/dcim/models/__init__.py

@@ -1729,7 +1729,7 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel):
         filter = Q(device=self)
         if self.virtual_chassis and self.virtual_chassis.master == self:
             filter |= Q(device__virtual_chassis=self.virtual_chassis, mgmt_only=False)
-        return Interface.objects.filter(filter)
+        return Interface.objects.unrestricted().filter(filter)
 
     def get_cables(self, pk_list=False):
         """

+ 2 - 2
netbox/dcim/tables.py

@@ -495,13 +495,13 @@ class DeviceRoleTable(BaseTable):
     pk = ToggleColumn()
     device_count = tables.TemplateColumn(
         template_code=DEVICEROLE_DEVICE_COUNT,
-        accessor=Accessor('devices.count'),
+        accessor=Accessor('devices.unrestricted.count'),
         orderable=False,
         verbose_name='Devices'
     )
     vm_count = tables.TemplateColumn(
         template_code=DEVICEROLE_VM_COUNT,
-        accessor=Accessor('virtual_machines.count'),
+        accessor=Accessor('virtual_machines.unrestricted.count'),
         orderable=False,
         verbose_name='VMs'
     )

+ 1 - 1
netbox/ipam/api/views.py

@@ -84,7 +84,7 @@ class PrefixViewSet(CustomFieldModelViewSet):
         The advisory lock decorator uses a PostgreSQL advisory lock to prevent this API from being
         invoked in parallel, which results in a race condition where multiple insertions can occur.
         """
-        prefix = get_object_or_404(Prefix, pk=pk)
+        prefix = get_object_or_404(self.queryset, pk=pk)
         available_prefixes = prefix.get_available_prefixes()
 
         if request.method == 'POST':

+ 1 - 0
netbox/ipam/forms.py

@@ -1,4 +1,5 @@
 from django import forms
+from django.contrib.contenttypes.models import ContentType
 from django.core.validators import MaxValueValidator, MinValueValidator
 
 from dcim.models import Device, Interface, Rack, Region, Site

+ 2 - 2
netbox/ipam/models.py

@@ -868,9 +868,9 @@ class VLANGroup(ChangeLoggedModel):
         """
         Return the first available VLAN ID (1-4094) in the group.
         """
-        vids = [vlan['vid'] for vlan in self.vlans.order_by('vid').values('vid')]
+        vlan_ids = VLAN.objects.unrestricted().filter(group=self).values_list('vid', flat=True)
         for i in range(1, 4095):
-            if i not in vids:
+            if i not in vlan_ids:
                 return i
         return None
 

+ 3 - 3
netbox/ipam/tables.py

@@ -103,7 +103,7 @@ VLAN_LINK = """
 """
 
 VLAN_PREFIXES = """
-{% for prefix in record.prefixes.all %}
+{% for prefix in record.prefixes.unrestricted %}
     <a href="{% url 'ipam:prefix' pk=prefix.pk %}">{{ prefix }}</a>{% if not forloop.last %}<br />{% endif %}
 {% empty %}
     &mdash;
@@ -283,13 +283,13 @@ class AggregateDetailTable(AggregateTable):
 class RoleTable(BaseTable):
     pk = ToggleColumn()
     prefix_count = tables.TemplateColumn(
-        accessor=Accessor('prefixes.count'),
+        accessor=Accessor('prefixes.unrestricted.count'),
         template_code=ROLE_PREFIX_COUNT,
         orderable=False,
         verbose_name='Prefixes'
     )
     vlan_count = tables.TemplateColumn(
-        accessor=Accessor('vlans.count'),
+        accessor=Accessor('vlans.unrestricted.count'),
         template_code=ROLE_VLAN_COUNT,
         orderable=False,
         verbose_name='VLANs'

+ 3 - 3
netbox/ipam/tests/test_ordering.py

@@ -86,7 +86,7 @@ class PrefixOrderingTestCase(OrderingTestBase):
         Prefix.objects.bulk_create(prefixes)
 
         # Test
-        self._compare(Prefix.objects.all(), prefixes)
+        self._compare(Prefix.objects.unrestricted(), prefixes)
 
     def test_prefix_complex_ordering(self):
         """
@@ -122,7 +122,7 @@ class PrefixOrderingTestCase(OrderingTestBase):
         Prefix.objects.bulk_create(prefixes)
 
         # Test
-        self._compare(Prefix.objects.all(), prefixes)
+        self._compare(Prefix.objects.unrestricted(), prefixes)
 
 
 class IPAddressOrderingTestCase(OrderingTestBase):
@@ -173,4 +173,4 @@ class IPAddressOrderingTestCase(OrderingTestBase):
         IPAddress.objects.bulk_create(addresses)
 
         # Test
-        self._compare(IPAddress.objects.all(), addresses)
+        self._compare(IPAddress.objects.unrestricted(), addresses)

+ 8 - 2
netbox/ipam/views.py

@@ -827,9 +827,15 @@ class ServiceEditView(ObjectEditView):
 
     def alter_obj(self, obj, request, url_args, url_kwargs):
         if 'device' in url_kwargs:
-            obj.device = get_object_or_404(Device, pk=url_kwargs['device'])
+            obj.device = get_object_or_404(
+                Device.objects.restrict(request.user),
+                pk=url_kwargs['device']
+            )
         elif 'virtualmachine' in url_kwargs:
-            obj.virtual_machine = get_object_or_404(VirtualMachine, pk=url_kwargs['virtualmachine'])
+            obj.virtual_machine = get_object_or_404(
+                VirtualMachine.objects.restrict(request.user),
+                pk=url_kwargs['virtualmachine']
+            )
         return obj
 
     def get_return_url(self, request, service):