-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.py
30 lines (23 loc) · 1.11 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import re
class AppendMetadataMiddleware:
"""
Middleware class that appends the 'metadata' include parameter to requests for specific API paths.
This middleware checks if the request path starts with one of the specified prefixes, and if so,
appends the 'metadata' include parameter to the request GET parameters.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
"""
Process the request and add the 'metadata' include parameter if the request path starts with a specified prefix.
:param request: The incoming request object.
:return: The response generated by the get_response function.
"""
path_regex = re.compile(r'^/api/v2/(resources|datasets|maps|documents|geoapps)/\d+/?')
if path_regex.match(request.path):
includes = request.GET.get("include[]")
request.GET = request.GET.copy()
request.GET["include[]"] = "metadata"
if includes:
request.GET.appendlist("include[]", includes)
return self.get_response(request)