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

Move NetBoxFakeRequest and copy_safe_request() from utilities.utils to utilities.request

Jeremy Stretch пре 1 година
родитељ
комит
ef774319f4

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

@@ -20,7 +20,7 @@ from netbox.api.metadata import ContentTypeMetadata
 from netbox.api.renderers import TextRenderer
 from netbox.api.viewsets import NetBoxModelViewSet
 from utilities.exceptions import RQWorkerNotRunningException
-from utilities.utils import copy_safe_request
+from utilities.request import copy_safe_request
 from . import serializers
 from .mixins import ConfigTemplateRenderMixin
 

+ 1 - 1
netbox/extras/management/commands/runscript.py

@@ -14,7 +14,7 @@ from extras.context_managers import event_tracking
 from extras.scripts import get_module_and_script
 from extras.signals import clear_events
 from utilities.exceptions import AbortTransaction
-from utilities.utils import NetBoxFakeRequest
+from utilities.request import NetBoxFakeRequest
 
 
 class Command(BaseCommand):

+ 2 - 1
netbox/extras/views.py

@@ -20,9 +20,10 @@ from netbox.views import generic
 from netbox.views.generic.mixins import TableMixin
 from utilities.forms import ConfirmationForm, get_field_value
 from utilities.paginator import EnhancedPaginator, get_paginate_count
+from utilities.request import copy_safe_request
 from utilities.rqworker import get_workers_for_queue
 from utilities.templatetags.builtins.filters import render_markdown
-from utilities.utils import copy_safe_request, count_related, normalize_querydict, shallow_compare_dict
+from utilities.utils import count_related, normalize_querydict, shallow_compare_dict
 from utilities.views import ContentTypePermissionRequiredMixin, get_viewname, register_model_view
 from . import filtersets, forms, tables
 from .models import *

+ 43 - 0
netbox/utilities/request.py

@@ -2,11 +2,54 @@ from django.utils.translation import gettext_lazy as _
 from netaddr import AddrFormatError, IPAddress
 from urllib.parse import urlparse
 
+from .constants import HTTP_REQUEST_META_SAFE_COPY
+
 __all__ = (
+    'NetBoxFakeRequest',
+    'copy_safe_request',
     'get_client_ip',
 )
 
 
+#
+# Fake request object
+#
+
+class NetBoxFakeRequest:
+    """
+    A fake request object which is explicitly defined at the module level so it is able to be pickled. It simply
+    takes what is passed to it as kwargs on init and sets them as instance variables.
+    """
+    def __init__(self, _dict):
+        self.__dict__ = _dict
+
+
+#
+# Utility functions
+#
+
+def copy_safe_request(request):
+    """
+    Copy selected attributes from a request object into a new fake request object. This is needed in places where
+    thread safe pickling of the useful request data is needed.
+    """
+    meta = {
+        k: request.META[k]
+        for k in HTTP_REQUEST_META_SAFE_COPY
+        if k in request.META and isinstance(request.META[k], str)
+    }
+    return NetBoxFakeRequest({
+        'META': meta,
+        'COOKIES': request.COOKIES,
+        'POST': request.POST,
+        'GET': request.GET,
+        'FILES': request.FILES,
+        'user': request.user,
+        'path': request.path,
+        'id': getattr(request, 'id', None),  # UUID assigned by middleware
+    })
+
+
 def get_client_ip(request, additional_headers=()):
     """
     Return the client (source) IP address of the given request.

+ 0 - 36
netbox/utilities/utils.py

@@ -21,7 +21,6 @@ from mptt.models import MPTTModel
 from dcim.choices import CableLengthUnitChoices, WeightUnitChoices
 from extras.utils import is_taggable
 from netbox.config import get_config
-from utilities.constants import HTTP_REQUEST_META_SAFE_COPY
 from .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
 from .string import title
 
@@ -435,41 +434,6 @@ def content_type_identifier(ct):
     return f'{ct.app_label}.{ct.model}'
 
 
-#
-# Fake request object
-#
-
-class NetBoxFakeRequest:
-    """
-    A fake request object which is explicitly defined at the module level so it is able to be pickled. It simply
-    takes what is passed to it as kwargs on init and sets them as instance variables.
-    """
-    def __init__(self, _dict):
-        self.__dict__ = _dict
-
-
-def copy_safe_request(request):
-    """
-    Copy selected attributes from a request object into a new fake request object. This is needed in places where
-    thread safe pickling of the useful request data is needed.
-    """
-    meta = {
-        k: request.META[k]
-        for k in HTTP_REQUEST_META_SAFE_COPY
-        if k in request.META and isinstance(request.META[k], str)
-    }
-    return NetBoxFakeRequest({
-        'META': meta,
-        'COOKIES': request.COOKIES,
-        'POST': request.POST,
-        'GET': request.GET,
-        'FILES': request.FILES,
-        'user': request.user,
-        'path': request.path,
-        'id': getattr(request, 'id', None),  # UUID assigned by middleware
-    })
-
-
 def clean_html(html, schemes):
     """
     Sanitizes HTML based on a whitelist of allowed tags and attributes.