mirror of
https://github.com/dnlbauer/django-signposting.git
synced 2026-09-10 22:15:30 +00:00
make linter happy
This commit is contained in:
@@ -41,6 +41,7 @@ class SignpostingMiddleware:
|
|||||||
|
|
||||||
response["Link"] = " , ".join(link_snippets)
|
response["Link"] = " , ".join(link_snippets)
|
||||||
|
|
||||||
|
|
||||||
class HtmlSignpostingMiddleware(SignpostingMiddleware):
|
class HtmlSignpostingMiddleware(SignpostingMiddleware):
|
||||||
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]):
|
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
@@ -50,15 +51,15 @@ class HtmlSignpostingMiddleware(SignpostingMiddleware):
|
|||||||
|
|
||||||
if not hasattr(response, "_signposts"):
|
if not hasattr(response, "_signposts"):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
# Adding Signposts via HTML is only supported for HTML responses
|
# Adding Signposts via HTML is only supported for HTML responses
|
||||||
if not response.headers["Content-Type"].startswith("text/html"):
|
if not response.headers["Content-Type"].startswith("text/html"):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
content = response.content.decode("utf-8")
|
content = response.content.decode("utf-8")
|
||||||
soup = BeautifulSoup(content, "html.parser")
|
soup = BeautifulSoup(content, "html.parser")
|
||||||
|
|
||||||
# Html should have a root element
|
# Html should have a root element
|
||||||
if not soup.html:
|
if not soup.html:
|
||||||
raise Exception("Could not find HTML root element")
|
raise Exception("Could not find HTML root element")
|
||||||
|
|
||||||
@@ -67,7 +68,6 @@ class HtmlSignpostingMiddleware(SignpostingMiddleware):
|
|||||||
head_tag = soup.new_tag("head")
|
head_tag = soup.new_tag("head")
|
||||||
soup.html.insert(0, head_tag)
|
soup.html.insert(0, head_tag)
|
||||||
|
|
||||||
|
|
||||||
# BUild links for each signpost and add them to the html
|
# BUild links for each signpost and add them to the html
|
||||||
for signpost in response._signposts:
|
for signpost in response._signposts:
|
||||||
link_tag = soup.new_tag("link")
|
link_tag = soup.new_tag("link")
|
||||||
@@ -79,13 +79,12 @@ class HtmlSignpostingMiddleware(SignpostingMiddleware):
|
|||||||
|
|
||||||
# Override the original content with the new HTML
|
# Override the original content with the new HTML
|
||||||
response.content = soup.prettify().encode("utf-8")
|
response.content = soup.prettify().encode("utf-8")
|
||||||
response['Content-Length'] = len(response.content)
|
response["Content-Length"] = len(response.content)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
class JsonLdSignpostingParserMiddleware(MiddlewareMixin):
|
class JsonLdSignpostingParserMiddleware(MiddlewareMixin):
|
||||||
|
|
||||||
def process_response(
|
def process_response(
|
||||||
self, request: HttpRequest, response: HttpResponse
|
self, request: HttpRequest, response: HttpResponse
|
||||||
) -> HttpResponse:
|
) -> HttpResponse:
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ from django_signposting.middleware import HtmlSignpostingMiddleware
|
|||||||
from signposting import LinkRel, Signpost
|
from signposting import LinkRel, Signpost
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
def assert_links_exist(response: HttpResponse, signposts: list[Signpost]=None):
|
|
||||||
|
def assert_links_exist(response: HttpResponse, signposts: list[Signpost] = None):
|
||||||
if signposts is None:
|
if signposts is None:
|
||||||
signposts = response._signposts
|
signposts = response._signposts
|
||||||
|
|
||||||
|
|
||||||
soup = BeautifulSoup(response.content.decode("utf-8"), "html.parser")
|
soup = BeautifulSoup(response.content.decode("utf-8"), "html.parser")
|
||||||
for signpost in signposts:
|
for signpost in signposts:
|
||||||
@@ -16,61 +16,58 @@ def assert_links_exist(response: HttpResponse, signposts: list[Signpost]=None):
|
|||||||
|
|
||||||
assert len(soup.find_all("link")) == len(signposts)
|
assert len(soup.find_all("link")) == len(signposts)
|
||||||
|
|
||||||
|
|
||||||
def test_middleware_no_signposting():
|
def test_middleware_no_signposting():
|
||||||
response = HttpResponse("<html><head></head><body></body></html>")
|
response = HttpResponse("<html><head></head><body></body></html>")
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
|
|
||||||
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
||||||
response = middleware(None)
|
response = middleware(None)
|
||||||
|
|
||||||
assert_links_exist(response, [])
|
assert_links_exist(response, [])
|
||||||
|
|
||||||
|
|
||||||
def test_middleware_no_html():
|
def test_middleware_no_html():
|
||||||
response = JsonResponse({"hello": "world"})
|
response = JsonResponse({"hello": "world"})
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
response._signpost = [
|
response._signpost = [Signpost(LinkRel.author, "http://example.com")]
|
||||||
Signpost(LinkRel.author, "http://example.com")
|
|
||||||
]
|
|
||||||
|
|
||||||
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
||||||
middleware(None)
|
middleware(None)
|
||||||
assert_links_exist(response, [])
|
assert_links_exist(response, [])
|
||||||
|
|
||||||
|
|
||||||
def test_middleware_malformed_html():
|
def test_middleware_malformed_html():
|
||||||
response = HttpResponse("Hello world")
|
response = HttpResponse("Hello world")
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
response.content_type = "text/html"
|
response.content_type = "text/html"
|
||||||
response._signposts = [
|
response._signposts = [Signpost(LinkRel.author, "http://example.com")]
|
||||||
Signpost(LinkRel.author, "http://example.com")
|
|
||||||
]
|
|
||||||
|
|
||||||
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(Exception):
|
||||||
middleware(None)
|
middleware(None)
|
||||||
|
|
||||||
|
|
||||||
def test_middleware_signposting_without_head():
|
def test_middleware_signposting_without_head():
|
||||||
response = HttpResponse("<html><body></body></html>")
|
response = HttpResponse("<html><body></body></html>")
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
response._signposts = [
|
response._signposts = [Signpost(LinkRel.author, "http://example.com")]
|
||||||
Signpost(LinkRel.author, "http://example.com")
|
|
||||||
]
|
|
||||||
|
|
||||||
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
||||||
middleware(None)
|
middleware(None)
|
||||||
assert_links_exist(response)
|
assert_links_exist(response)
|
||||||
|
|
||||||
|
|
||||||
def test_middleware_signposting():
|
def test_middleware_signposting():
|
||||||
response = HttpResponse("<html><head></head><body></body></html>")
|
response = HttpResponse("<html><head></head><body></body></html>")
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
response._signposts = [
|
response._signposts = [Signpost(LinkRel.author, "http://example.com")]
|
||||||
Signpost(LinkRel.author, "http://example.com")
|
|
||||||
]
|
|
||||||
|
|
||||||
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
||||||
middleware(None)
|
middleware(None)
|
||||||
assert_links_exist(response)
|
assert_links_exist(response)
|
||||||
|
|
||||||
|
|
||||||
def test_middleware_multiple_signposts():
|
def test_middleware_multiple_signposts():
|
||||||
response = HttpResponse("<html><head></head><body></body></html>")
|
response = HttpResponse("<html><head></head><body></body></html>")
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
@@ -88,9 +85,7 @@ def test_middleware_multiple_signposts():
|
|||||||
def test_middleware_signpost_with_content_type():
|
def test_middleware_signpost_with_content_type():
|
||||||
response = HttpResponse("<html><head></head><body></body></html>")
|
response = HttpResponse("<html><head></head><body></body></html>")
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
response._signposts = [
|
response._signposts = [Signpost(LinkRel.item, "http://example.com", "text/json")]
|
||||||
Signpost(LinkRel.item, "http://example.com", "text/json")
|
|
||||||
]
|
|
||||||
|
|
||||||
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
||||||
response = middleware(None)
|
response = middleware(None)
|
||||||
@@ -100,9 +95,7 @@ def test_middleware_signpost_with_content_type():
|
|||||||
def test_middleware_ignore_error_responses():
|
def test_middleware_ignore_error_responses():
|
||||||
response = HttpResponse("<html><head></head><body></body></html>")
|
response = HttpResponse("<html><head></head><body></body></html>")
|
||||||
response.status_code = 400
|
response.status_code = 400
|
||||||
response._signposts = [
|
response._signposts = [Signpost(LinkRel.author, "http://example.com")]
|
||||||
Signpost(LinkRel.author, "http://example.com")
|
|
||||||
]
|
|
||||||
|
|
||||||
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
||||||
response = middleware(None)
|
response = middleware(None)
|
||||||
@@ -112,11 +105,8 @@ def test_middleware_ignore_error_responses():
|
|||||||
def test_middleware_type_link():
|
def test_middleware_type_link():
|
||||||
response = HttpResponse("<html><head></head><body></body></html>")
|
response = HttpResponse("<html><head></head><body></body></html>")
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
response._signposts = [
|
response._signposts = [Signpost(LinkRel.type, "http://schema.org/Dataset")]
|
||||||
Signpost(LinkRel.type, "http://schema.org/Dataset")
|
|
||||||
]
|
|
||||||
|
|
||||||
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
middleware = HtmlSignpostingMiddleware(lambda request: response)
|
||||||
response = middleware(None)
|
response = middleware(None)
|
||||||
assert_links_exist(response)
|
assert_links_exist(response)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user