mirror of
https://github.com/dnlbauer/django-signposting.git
synced 2026-09-10 14:05:31 +00:00
add utility method to add signposts from view
This commit is contained in:
23
django_signposting/utils.py
Normal file
23
django_signposting/utils.py
Normal 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
|
||||||
54
tests/test_utils.py
Normal file
54
tests/test_utils.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
from django.http import HttpResponse
|
||||||
|
from django.conf import settings
|
||||||
|
from django_signposting.utils import add_signposts
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module", autouse=True)
|
||||||
|
def configure_django_settings():
|
||||||
|
settings.configure()
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_signpost():
|
||||||
|
response = HttpResponse()
|
||||||
|
add_signposts(response, item="http://example.com")
|
||||||
|
|
||||||
|
assert response._signposts["item"] == ["http://example.com"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_multiple_signposts():
|
||||||
|
response = HttpResponse()
|
||||||
|
add_signposts(response,
|
||||||
|
item="http://example.com",
|
||||||
|
author=["https://example2.com", "https://example3.com"]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response._signposts == {
|
||||||
|
"item": ["http://example.com"],
|
||||||
|
"author": [
|
||||||
|
"https://example2.com",
|
||||||
|
"https://example3.com",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_signposts_with_content_type():
|
||||||
|
response = HttpResponse()
|
||||||
|
add_signposts(response,
|
||||||
|
item=("http://example.com", "text/json"),
|
||||||
|
author=["https://example2.com", "https://example3.com"]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response._signposts == {
|
||||||
|
"item": [("http://example.com", "text/json")],
|
||||||
|
"author": [
|
||||||
|
"https://example2.com",
|
||||||
|
"https://example3.com",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_signposts_from_dict():
|
||||||
|
response = HttpResponse()
|
||||||
|
add_signposts(response, **{"cite-as": ["https://example.com"]})
|
||||||
|
assert response._signposts["cite-as"] == ["https://example.com"]
|
||||||
Reference in New Issue
Block a user