mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Move NetBoxFakeRequest and copy_safe_request() from utilities.utils to utilities.request
This commit is contained in:
@ -20,7 +20,7 @@ from netbox.api.metadata import ContentTypeMetadata
|
|||||||
from netbox.api.renderers import TextRenderer
|
from netbox.api.renderers import TextRenderer
|
||||||
from netbox.api.viewsets import NetBoxModelViewSet
|
from netbox.api.viewsets import NetBoxModelViewSet
|
||||||
from utilities.exceptions import RQWorkerNotRunningException
|
from utilities.exceptions import RQWorkerNotRunningException
|
||||||
from utilities.utils import copy_safe_request
|
from utilities.request import copy_safe_request
|
||||||
from . import serializers
|
from . import serializers
|
||||||
from .mixins import ConfigTemplateRenderMixin
|
from .mixins import ConfigTemplateRenderMixin
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ from extras.context_managers import event_tracking
|
|||||||
from extras.scripts import get_module_and_script
|
from extras.scripts import get_module_and_script
|
||||||
from extras.signals import clear_events
|
from extras.signals import clear_events
|
||||||
from utilities.exceptions import AbortTransaction
|
from utilities.exceptions import AbortTransaction
|
||||||
from utilities.utils import NetBoxFakeRequest
|
from utilities.request import NetBoxFakeRequest
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
|
@ -20,9 +20,10 @@ from netbox.views import generic
|
|||||||
from netbox.views.generic.mixins import TableMixin
|
from netbox.views.generic.mixins import TableMixin
|
||||||
from utilities.forms import ConfirmationForm, get_field_value
|
from utilities.forms import ConfirmationForm, get_field_value
|
||||||
from utilities.paginator import EnhancedPaginator, get_paginate_count
|
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.rqworker import get_workers_for_queue
|
||||||
from utilities.templatetags.builtins.filters import render_markdown
|
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 utilities.views import ContentTypePermissionRequiredMixin, get_viewname, register_model_view
|
||||||
from . import filtersets, forms, tables
|
from . import filtersets, forms, tables
|
||||||
from .models import *
|
from .models import *
|
||||||
|
@ -2,11 +2,54 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
from netaddr import AddrFormatError, IPAddress
|
from netaddr import AddrFormatError, IPAddress
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from .constants import HTTP_REQUEST_META_SAFE_COPY
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
'NetBoxFakeRequest',
|
||||||
|
'copy_safe_request',
|
||||||
'get_client_ip',
|
'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=()):
|
def get_client_ip(request, additional_headers=()):
|
||||||
"""
|
"""
|
||||||
Return the client (source) IP address of the given request.
|
Return the client (source) IP address of the given request.
|
||||||
|
@ -21,7 +21,6 @@ from mptt.models import MPTTModel
|
|||||||
from dcim.choices import CableLengthUnitChoices, WeightUnitChoices
|
from dcim.choices import CableLengthUnitChoices, WeightUnitChoices
|
||||||
from extras.utils import is_taggable
|
from extras.utils import is_taggable
|
||||||
from netbox.config import get_config
|
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 .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
|
||||||
from .string import title
|
from .string import title
|
||||||
|
|
||||||
@ -435,41 +434,6 @@ def content_type_identifier(ct):
|
|||||||
return f'{ct.app_label}.{ct.model}'
|
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):
|
def clean_html(html, schemes):
|
||||||
"""
|
"""
|
||||||
Sanitizes HTML based on a whitelist of allowed tags and attributes.
|
Sanitizes HTML based on a whitelist of allowed tags and attributes.
|
||||||
|
Reference in New Issue
Block a user