add utility method to add signposts from view

This commit is contained in:
Daniel Bauer
2024-08-15 21:58:42 +02:00
parent 370d4d8336
commit cacba71caa
2 changed files with 77 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
from django.http import HttpResponse
def add_signposts(response: HttpResponse, **kwargs):
""" Adds signposting headers to the responses.
params:
response - the response object
kwargs - a map of relation types to a list of corresponding links. Each link can be a link or a tuple of link and media type.
"""
if not hasattr(response, '_signposts'):
response._signposts = {}
for key in kwargs.keys():
values = kwargs[key]
if isinstance(values, str) or isinstance(values, tuple):
values = [values]
if key not in response._signposts:
response._signposts[key] = values
else:
response._signposts[key] += values