Skip to content

Commit

Permalink
Adds prefixes to current context after route resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Natan16 committed Dec 6, 2023
1 parent 5722f7e commit 787050a
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions django_query_prefixer/middlewares/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
from django.urls import resolve
from django_query_prefixer import sql_prefixes
from django_query_prefixer import set_prefix, remove_prefix


class RequestRouteMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = request.get_response(request)
remove_prefix("view_name")
remove_prefix("route")
return response

def process_view(self, request, view_func, view_args, view_kwargs):
set_prefix(key="view_name", value=f"{view_func.__module__}.{view_func.__name__}")
set_prefix(
key="route",
value=escape_comment_markers(request.resolver_match.route.route)
)
return view_func


def request_route(get_response):
def middleware(request):
if request.resolver_match is not None:
route = request.resolver_match.route
else:
route = resolve(request.path_info)

with sql_prefixes(
view_name=route.view_name,
route=escape_comment_markers(route.route),
):
return get_response(request)

return middleware
return RequestRouteMiddleware(get_response)


def escape_comment_markers(value):
Expand Down

0 comments on commit 787050a

Please sign in to comment.