Skip to content

Commit

Permalink
Regex support added for ignore URL list in settings.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurav Sharma authored and Saurav Sharma committed Feb 14, 2024
1 parent 9a9b00a commit 1fbb4c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions django_guid/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.exceptions import ImproperlyConfigured

from django_guid.context import guid
from django_guid.utils import get_id_from_header, ignored_url
from django_guid.utils import get_id_from_header, ignored_url, is_url_in_ignored_list

try:
from django.utils.decorators import sync_and_async_middleware
Expand All @@ -28,7 +28,8 @@ def process_incoming_request(request: 'HttpRequest') -> None:
Processes an incoming request. This function is called before the view and later middleware.
Same logic for both async and sync views.
"""
if not ignored_url(request=request):
# if not ignored_url(request=request):
if not is_url_in_ignored_list(request=request):
# Process request and store the GUID in a contextvar
guid.set(get_id_from_header(request))

Expand All @@ -42,7 +43,8 @@ def process_outgoing_request(response: 'HttpResponse', request: 'HttpRequest') -
"""
Process an outgoing request. This function is called after the view and before later middleware.
"""
if not ignored_url(request=request):
# if not ignored_url(request=request):
if not is_url_in_ignored_list(request=request):
if settings.return_header:
response[settings.guid_header_name] = guid.get() # Adds the GUID to the response header
if settings.expose_header:
Expand Down
20 changes: 20 additions & 0 deletions django_guid/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import uuid
from typing import TYPE_CHECKING, Optional, Union
import re

from django_guid.config import settings

Expand Down Expand Up @@ -91,3 +92,22 @@ def validate_guid(original_guid: str) -> bool:
return bool(uuid.UUID(original_guid, version=4).hex)
except ValueError:
return False


def is_url_in_ignored_list(request: Union['HttpRequest', 'HttpResponse']) -> bool:
"""
Support for Regex added
Checks if the current URL is defined in the `IGNORE_URLS` setting.
:return: Boolean
"""
endpoint = request.path.strip('/')
for ignore_url in settings.ignore_urls:
pattern = ignore_url.replace("*", "[\s\S]+")
pattern = "^" + pattern + "$"
search = re.search(pattern, endpoint)
if search:
logger.info("URL Ignored")
return True
logger.info("URL not Ignored")
return False

0 comments on commit 1fbb4c6

Please sign in to comment.