Fix: better handle favicon with static dir (#10107)

This commit is contained in:
shamoon 2025-06-03 08:05:59 -07:00 committed by GitHub
parent 1512599f4f
commit 4e082f997c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 15 deletions

View File

@ -239,9 +239,6 @@ lint.per-file-ignores."src/paperless/checks.py" = [
lint.per-file-ignores."src/paperless/settings.py" = [ lint.per-file-ignores."src/paperless/settings.py" = [
"PTH", "PTH",
] # TODO Enable & remove ] # TODO Enable & remove
lint.per-file-ignores."src/paperless/views.py" = [
"PTH",
] # TODO Enable & remove
lint.per-file-ignores."src/paperless_mail/mail.py" = [ lint.per-file-ignores."src/paperless_mail/mail.py" = [
"PTH", "PTH",
] # TODO Enable & remove ] # TODO Enable & remove

View File

@ -0,0 +1,25 @@
import tempfile
from pathlib import Path
from django.conf import settings
def test_favicon_view(client):
with tempfile.TemporaryDirectory() as tmpdir:
static_dir = Path(tmpdir)
favicon_path = static_dir / "paperless" / "img" / "favicon.ico"
favicon_path.parent.mkdir(parents=True, exist_ok=True)
favicon_path.write_bytes(b"FAKE ICON DATA")
settings.STATIC_ROOT = static_dir
response = client.get("/favicon.ico")
assert response.status_code == 200
assert response["Content-Type"] == "image/x-icon"
assert b"".join(response.streaming_content) == b"FAKE ICON DATA"
def test_favicon_view_missing_file(client):
settings.STATIC_ROOT = Path(tempfile.mkdtemp())
response = client.get("/favicon.ico")
assert response.status_code == 404

View File

@ -1,5 +1,5 @@
import os
from collections import OrderedDict from collections import OrderedDict
from pathlib import Path
from allauth.mfa import signals from allauth.mfa import signals
from allauth.mfa.adapter import get_adapter as get_mfa_adapter from allauth.mfa.adapter import get_adapter as get_mfa_adapter
@ -11,8 +11,9 @@ from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.models import SocialAccount from allauth.socialaccount.models import SocialAccount
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.staticfiles.storage import staticfiles_storage
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.http import HttpResponse from django.http import FileResponse
from django.http import HttpResponseBadRequest from django.http import HttpResponseBadRequest
from django.http import HttpResponseForbidden from django.http import HttpResponseForbidden
from django.http import HttpResponseNotFound from django.http import HttpResponseNotFound
@ -92,16 +93,12 @@ class StandardPagination(PageNumberPagination):
class FaviconView(View): class FaviconView(View):
def get(self, request, *args, **kwargs): # pragma: no cover def get(self, request, *args, **kwargs):
favicon = os.path.join( try:
os.path.dirname(__file__), path = Path(staticfiles_storage.path("paperless/img/favicon.ico"))
"static", return FileResponse(path.open("rb"), content_type="image/x-icon")
"paperless", except FileNotFoundError:
"img", return HttpResponseNotFound("favicon.ico not found")
"favicon.ico",
)
with open(favicon, "rb") as f:
return HttpResponse(f, content_type="image/x-icon")
class UserViewSet(ModelViewSet): class UserViewSet(ModelViewSet):