Skip to content

Commit

Permalink
Documents: Fix file not found error
Browse files Browse the repository at this point in the history
Use 'staticfiles_storage.open' if no file is found.
  • Loading branch information
3j14 committed Jul 22, 2023
1 parent 549c6b4 commit 3f3f6f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gcampus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

from django.utils.version import get_version

VERSION = (0, 8, 0, "rc", 3)
VERSION = (0, 8, 0, "rc", 4)
__version__ = get_version(VERSION)
18 changes: 11 additions & 7 deletions gcampus/documents/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

from django.conf import settings
from django.contrib.staticfiles import finders
from django.contrib.staticfiles.storage import staticfiles_storage
from django.template.loader import render_to_string
from weasyprint import HTML, Document
from weasyprint.urls import URLFetchingError, default_url_fetcher
Expand All @@ -49,13 +50,16 @@ def url_fetcher(url: str, **kwargs):
normalized_path = posixpath.normpath(path).lstrip("/")
mime_type, encoding = mimetypes.guess_type(path, strict=True)
absolute_path = finders.find(normalized_path)
if not absolute_path:
raise FileNotFoundError()
if not os.path.isfile(absolute_path):
raise URLFetchingError(
f"File '{url}' (resolved to '{absolute_path}') not found!"
)
file_obj = open(absolute_path, "rb")
try:
if not absolute_path:
raise FileNotFoundError()
if not os.path.isfile(absolute_path) or not os.path.exists(absolute_path):
raise FileNotFoundError(
f"File '{url}' (resolved to '{absolute_path}') not found!"
)
file_obj = open(absolute_path, "rb")
except FileNotFoundError:
file_obj = staticfiles_storage.open(normalized_path, mode="rb")
return {
"mime_type": mime_type,
"encoding": encoding,
Expand Down

0 comments on commit 3f3f6f4

Please sign in to comment.