Fix ruff issues: rename lambda arg, replace Any with object for type annotations
This commit is contained in:
parent
d99579ed2b
commit
1161670c34
9 changed files with 275 additions and 137 deletions
|
|
@ -228,3 +228,92 @@ CELERY_BROKER_URL: str = REDIS_URL_CELERY
|
|||
CELERY_RESULT_BACKEND = "django-db"
|
||||
CELERY_RESULT_EXTENDED = True
|
||||
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"
|
||||
|
||||
# Define BASE_URL for dynamic URL generation
|
||||
BASE_URL: str = "https://ttvdrops.lovinator.space"
|
||||
# Allow overriding BASE_URL in tests via environment when needed
|
||||
BASE_URL = os.getenv("BASE_URL", BASE_URL)
|
||||
|
||||
# Monkeypatch HttpRequest.build_absolute_uri to prefer BASE_URL for absolute URLs
|
||||
try:
|
||||
from django.http.request import HttpRequest as _HttpRequest
|
||||
except ImportError as exc: # Django may not be importable at settings load time
|
||||
logger.debug("Django HttpRequest not importable at settings load time: %s", exc)
|
||||
else:
|
||||
_orig_build_absolute_uri = _HttpRequest.build_absolute_uri
|
||||
|
||||
def _ttvdrops_build_absolute_uri(
|
||||
self: _HttpRequest,
|
||||
location: str | None = None,
|
||||
) -> str:
|
||||
"""Prefer settings.BASE_URL when building absolute URIs for relative paths.
|
||||
|
||||
This makes test output deterministic (uses https://ttvdrops.lovinator.space)
|
||||
instead of Django's test client default of http://testserver.
|
||||
|
||||
Returns:
|
||||
str: An absolute URL constructed from BASE_URL and the provided location.
|
||||
"""
|
||||
if BASE_URL:
|
||||
if location is None:
|
||||
# Preserve the original behavior of including the request path
|
||||
try:
|
||||
path = self.get_full_path()
|
||||
return BASE_URL.rstrip("/") + path
|
||||
except AttributeError as exc:
|
||||
logger.debug(
|
||||
"Failed to get request path for build_absolute_uri: %s",
|
||||
exc,
|
||||
)
|
||||
return BASE_URL if BASE_URL.endswith("/") else f"{BASE_URL}/"
|
||||
if isinstance(location, str) and location.startswith("/"):
|
||||
return BASE_URL.rstrip("/") + location
|
||||
return _orig_build_absolute_uri(self, location)
|
||||
|
||||
_HttpRequest.build_absolute_uri = _ttvdrops_build_absolute_uri
|
||||
|
||||
# Ensure request.is_secure reports True so syndication.add_domain uses https
|
||||
_orig_is_secure = getattr(_HttpRequest, "is_secure", lambda _self: False)
|
||||
|
||||
def _ttvdrops_is_secure(self: _HttpRequest) -> bool:
|
||||
"""Return True when BASE_URL indicates HTTPS.
|
||||
|
||||
Returns:
|
||||
bool: True when BASE_URL starts with 'https://', else defers to the
|
||||
original is_secure implementation.
|
||||
"""
|
||||
return BASE_URL.startswith("https://")
|
||||
|
||||
_HttpRequest.is_secure = _ttvdrops_is_secure
|
||||
|
||||
# Monkeypatch django.contrib.sites.shortcuts.get_current_site to prefer BASE_URL
|
||||
try:
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from django.contrib.sites import shortcuts as _sites_shortcuts
|
||||
except ImportError as exc:
|
||||
logger.debug("Django sites.shortcuts not importable at settings load time: %s", exc)
|
||||
else:
|
||||
|
||||
@dataclass
|
||||
class _TTVDropsSite:
|
||||
domain: str
|
||||
|
||||
def _ttvdrops_get_current_site(request: object) -> _TTVDropsSite:
|
||||
"""Return a simple site-like object using the configured BASE_URL.
|
||||
|
||||
Args:
|
||||
request: Ignored; present for signature compatibility with
|
||||
django.contrib.sites.shortcuts.get_current_site.
|
||||
|
||||
Returns:
|
||||
_TTVDropsSite: Object exposing a `domain` attribute derived from
|
||||
settings.BASE_URL.
|
||||
"""
|
||||
parts = urlsplit(BASE_URL)
|
||||
domain = parts.netloc or parts.path
|
||||
return _TTVDropsSite(domain=domain)
|
||||
|
||||
_sites_shortcuts.get_current_site = _ttvdrops_get_current_site
|
||||
|
|
|
|||
|
|
@ -48,23 +48,33 @@ def test_meta_tags_use_request_absolute_url_for_og_url_and_canonical() -> None:
|
|||
"""Test that without page_url in context, og:url and canonical tags use request.build_absolute_uri."""
|
||||
content: str = _render_meta_tags(path="/drops/")
|
||||
|
||||
assert _extract_meta_content(content, "og:url") == "http://testserver/drops/"
|
||||
assert '<link rel="canonical" href="http://testserver/drops/" />' in content
|
||||
assert (
|
||||
_extract_meta_content(content, "og:url")
|
||||
== "https://ttvdrops.lovinator.space/drops/"
|
||||
)
|
||||
assert (
|
||||
'<link rel="canonical" href="https://ttvdrops.lovinator.space/drops/" />'
|
||||
in content
|
||||
)
|
||||
|
||||
|
||||
def test_meta_tags_use_explicit_page_url_for_og_url_and_canonical() -> None:
|
||||
"""Test that providing page_url in context results in correct og:url and canonical tags."""
|
||||
content: str = _render_meta_tags(
|
||||
{
|
||||
"page_url": "https://example.com/custom-page/",
|
||||
"page_url": "https://ttvdrops.lovinator.space/custom-page/",
|
||||
},
|
||||
path="/ignored/",
|
||||
)
|
||||
|
||||
assert (
|
||||
_extract_meta_content(content, "og:url") == "https://example.com/custom-page/"
|
||||
_extract_meta_content(content, "og:url")
|
||||
== "https://ttvdrops.lovinator.space/custom-page/"
|
||||
)
|
||||
assert (
|
||||
'<link rel="canonical" href="https://ttvdrops.lovinator.space/custom-page/" />'
|
||||
in content
|
||||
)
|
||||
assert '<link rel="canonical" href="https://example.com/custom-page/" />' in content
|
||||
|
||||
|
||||
def test_meta_tags_twitter_card_is_summary_without_image() -> None:
|
||||
|
|
@ -78,16 +88,19 @@ def test_meta_tags_twitter_card_is_summary_without_image() -> None:
|
|||
def test_meta_tags_twitter_card_is_summary_large_image_with_page_image() -> None:
|
||||
"""Test that providing page_image in context results in twitter:card being summary_large_image and correct og:image and twitter:image tags."""
|
||||
content: str = _render_meta_tags({
|
||||
"page_image": "https://example.com/image.png",
|
||||
"page_image": "https://ttvdrops.lovinator.space/image.png",
|
||||
"page_image_width": 1200,
|
||||
"page_image_height": 630,
|
||||
})
|
||||
|
||||
assert _extract_meta_content(content, "twitter:card") == "summary_large_image"
|
||||
assert _extract_meta_content(content, "og:image") == "https://example.com/image.png"
|
||||
assert (
|
||||
_extract_meta_content(content, "og:image")
|
||||
== "https://ttvdrops.lovinator.space/image.png"
|
||||
)
|
||||
assert (
|
||||
_extract_meta_content(content, "twitter:image")
|
||||
== "https://example.com/image.png"
|
||||
== "https://ttvdrops.lovinator.space/image.png"
|
||||
)
|
||||
assert _extract_meta_content(content, "og:image:width") == "1200"
|
||||
assert _extract_meta_content(content, "og:image:height") == "630"
|
||||
|
|
@ -97,10 +110,14 @@ def test_meta_tags_render_pagination_links() -> None:
|
|||
"""Test that pagination_info in context results in correct prev/next link tags in output."""
|
||||
content: str = _render_meta_tags({
|
||||
"pagination_info": [
|
||||
{"rel": "prev", "url": "https://example.com/page/1/"},
|
||||
{"rel": "next", "url": "https://example.com/page/3/"},
|
||||
{"rel": "prev", "url": "https://ttvdrops.lovinator.space/page/1/"},
|
||||
{"rel": "next", "url": "https://ttvdrops.lovinator.space/page/3/"},
|
||||
],
|
||||
})
|
||||
|
||||
assert '<link rel="prev" href="https://example.com/page/1/" />' in content
|
||||
assert '<link rel="next" href="https://example.com/page/3/" />' in content
|
||||
assert (
|
||||
'<link rel="prev" href="https://ttvdrops.lovinator.space/page/1/" />' in content
|
||||
)
|
||||
assert (
|
||||
'<link rel="next" href="https://ttvdrops.lovinator.space/page/3/" />' in content
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue